Skip to content

Instantly share code, notes, and snippets.

@sjonesyodle
Created October 4, 2013 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjonesyodle/6832108 to your computer and use it in GitHub Desktop.
Save sjonesyodle/6832108 to your computer and use it in GitHub Desktop.
;(function( window, undefined ) {
// jquery closure plugin
;(function(){
var
loadScript = function ( url, callback ) {
var
script, cb;
cb = function () {
if (typeof callback === "function") callback();
};
script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState){ //IE
script.onreadystatechange = function() {
if ( script.readyState == "loaded" || script.readyState == "complete" ) {
script.onreadystatechange = null;
cb();
}
};
}
else {
script.onload = function() {
cb();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild( script );
},
ver_tmpl = "{{version}}",
goog_cdn_jquery = "http://ajax.googleapis.com/ajax/libs/jquery/"+ ver_tmpl +"/jquery.min.js";
window.__$ = function ( version, closure ) {
version = goog_cdn_jquery.replace(ver_tmpl, version);
loadScript(version, function () {
closure( $.noConflict( true ) );
});
};
}());
__$("1.9.1", function ( $ ) {
$(function(){
var
jExtend = (function(){
//:: jQuery extensions :: void
$.fn.nodeListener = function ( o ) {
var hn, i, len, self = this, context;
i = 0;
len = parseInt(o.tries, 10);
hn = setInterval(function(){
var _cn = self.find(o.cn);
if (i >= len) {
if (o.always_exec_cb) o.cb();
clearInterval(hn);
return false;
}
if (_cn.length > 0) {
o.cb();
clearInterval(hn);
return;
}
i += 1;
}, o.interval);
};
$.fn.hasAttr = function(name) {
return this.attr(name) !== undefined;
};
$.fn.normalizeForm = function () {
var
idFlag = this.find("input#submit"),
nameFlag = this.find("input[name='submit']");
if (Util.isNode(idFlag)) idFlag.attr("id","");
if (Util.isNode(nameFlag)) nameFlag.attr("name", "");
return this;
};
}()),
Util = {
isNode : function (jNode) {
return jNode && jNode.length > 0;
},
obj2QueryStr : function (obj) {
var p, res = "";
if ($.isEmptyObject(obj)) return "";
for (p in obj) {
if (!obj.hasOwnProperty(p)) continue;
res += $.trim(p)+"="+escape( $.trim(obj[p]) )+"&";
}
return res.slice(0, -1);
},
queryStrAsObj : (function () {
var
match,
pl = /\+/g,
search = /([^&=]+)=?([^&]*)/g,
decode = function(s) {
return decodeURIComponent(s.replace(pl, " "));
},
query = window.location.search.slice(1),
o = {};
while (match = search.exec(query)) {
o[decode(match[1])] = decode(match[2]);
}
return o;
}()),
baseDomain : (function (){
var domainParts = document.domain.split(/\./);
if ( domainParts.length > 1 ) {
return( domainParts[domainParts.length-2] + "." + domainParts[domainParts.length-1] );
}
return document.domain;
}())
};
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::: YDL_TRK ::::::::::::::::::::::::::
window.YDL_TRK = (function () {
var Sandbox, extend, mods;
Sandbox = function () {
var
args = Array.prototype.slice.call(arguments),
cb = args.pop(),
modules = args[0],
currMod,
addMembers = function (self, member) {
self[member.name] = member.value;
},
i, l, prop, member;
if (!(this instanceof YDL_TRK)) return new YDL_TRK(modules, cb);
i = 0;
l = modules.length;
for (; i < l; i += 1) {
currMod = $.trim(modules[i]);
if (!YDL_TRK.modules.hasOwnProperty(currMod)) continue;
currMod = YDL_TRK.modules[currMod];
for (prop in currMod) {
addMembers(this, {name: prop, value : currMod[prop]});
}
}
cb.call(this);
};
extend = function ( O ) {
var
str = "string", obj = "object", fn = "function",
setup = function (el, type) {
var _type = typeof el;
return _type === type ?
function () {
if (_type === fn) return el();
return el;
}
: false;
},
_o = {
name : setup(O.name, str) || setup(O.name, fn),
members : setup(O.members, str) || setup(O.members, fn) || setup(O.members, obj)
}, p;
if (typeof O !== obj) return false;
for (p in _o) {
if (!_o[p] || !O.hasOwnProperty(p)) continue;
_o[p] = _o[p]();
}
O.extends[_o.name] = _o.members;
};
mods = Sandbox.modules = {};
Sandbox.MOD = function ( o ) {
var includes = {};
if (o.include && o.include.length > 0) {
(function(){
var
i = 0,
len = o.include.length,
inc_obj;
for (; i < len; i += 1) {
inc_obj = $.trim(o.include[i]);
if (!mods.hasOwnProperty(inc_obj)) continue;
$.extend(includes, mods[inc_obj]);
}
}());
}
o.members = $.isEmptyObject(includes) ? o.members : $.extend(includes, o.members);
extend({
extends : mods,
name : o.name,
members : o.members
});
};
//-------------:
return Sandbox;
//-------------:
}());
//:::::::::::::::::::::::::::::::::::::::::::::::::
// ::::::::::::: /YDL_TRK ::::::::::::::::::::::::::::::::
//::::::::: CORE
//::::::::::::::::::::::::::
YDL_TRK.MOD({
name : "Core",
members : {
//::::::::::::::::::::::
readCookie : function (name) {
var res = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return res ? unescape(res[2]) : null;
},
createCookie : function (name,value,days,path,domain,secure) {
var
cookie_string = name + "=" + escape(value),
date = new Date();
if ( days ) {
date.setTime( date.getTime() + ( days*24*60*60*1000 ) );
cookie_string += "; expires=" + date.toGMTString();
}
if ( path ) cookie_string += "; path=" + escape ( path );
if ( domain ) cookie_string += "; domain=" + escape ( domain );
if ( secure ) cookie_string += "; secure";
document.cookie = cookie_string;
},
visitTypeObj : function () {
return { "unpaid" : ( this.readCookie( this.ydlVisitTypeCookieName ) === this.ydlUUT).toString() };
},
ydlVisitTypeCookieTTL : 14,
ydlVisitTypeCookieName : "vt",
paidQueryParam : "provider",
ydlPUT : "p",
ydlUUT : "u",
//::::::::::::::::::::::
//::::::::::::::::::::::
clientDomain : "ClientDomain.com",
URI_Scheme : ( location.protocol === "https:" ) ? "https://" : "http://",
connectorHostname : "labs.natpal.com",
//::::::::::::::::::::::
//::::::::::::::::::::::
transmitPayLoad : function ( transmitter ) {
$("head").append( $.trim(transmitter) );
},
buildTransmitter : function ( o ) {
var
id = 'id="'+ ( o.id || "" ) + '"',
klass = 'class="'+ ( o.class || "" ) + '"',
src = 'src="'+ ( o.src || "" ) + '"',
attrs = id+" "+klass+" "+src+" ";
return "<img width='1' height='1' border='0'" + attrs +" />";
},
addParameter : function (transmitter, param) {
return transmitter.replace("?", "?"+param+"&");
}
//::::::::::::::::::::::
}
});
//::::::::::::::::::::::::::
//::::::::: WEB LEADS
//::::::::::::::::::::::::::
YDL_TRK.MOD({
include : ["Core"],
name : "WebForm",
members : {
//:::::::::::::::::::::::::::::::
ydlLeadUrl : function () {
return this.URI_Scheme + this.connectorHostname + "/trk/lead";
},
//:::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::
setForm : function ( o ) {
var leadForm = $($.trim( o.node )) , i, uniq_id;
if (!Util.isNode(leadForm)) return false;
if (!o.uniq_id || typeof o.uniq_id !== "string") return false;
leadForm.attr(o.uniq_id, "true");
this.leadForm = {
node : leadForm,
uniq_id : o.uniq_id
};
return;
},
getFormData : function () {
var
data = this.leadForm.node.serializeArray(),
res = {}, len = data.length, i = 0;
for (; i < len; i += 1) {
res[$.trim(data[i].name)] = $.trim(data[i].value);
}
return res;
},
//:::::::::::::::::::::::::::::::
//:::::::::::::::::::::::::::::::
evtMatchesLeadForm : function (jEvt) {
return jEvt.hasAttr(this.leadForm.uniq_id);
},
setupEvtCheckPoint : function (rootNode, captureEvt, verifiedTrigger) {
var self = this;
rootNode = $(rootNode);
if (!Util.isNode( rootNode )) return false;
rootNode.on(captureEvt, function ( e ) {
var
$this = $(this),
dispatchNode = $(e.target);
// // Not the desired post.
if (!self.evtMatchesLeadForm( dispatchNode ) || self.isVerified) return true;
$this.trigger( verifiedTrigger );
e.preventDefault();
return false;
});
return {
node : rootNode,
evt : captureEvt
};
},
closeEvtCheckPoint : function ( node, evt) {
this.isVerified = true;
},
buildLeadTransmitter : function ( o ) {
return this.buildTransmitter({
id : o.uniq_id,
src : this.ydlLeadUrl()+"?"+o.payload
});
}
//:::::::::::::::::::::::::::::::
}
});
//::::::::::::::::::::::::::
YDL_TRK(["WebForm"], function () {
//:::::::::::::::::::::::::::::::::::
var
self = this,
postEvent = "submit",
verified = "verified",
rootNode = document,
formData,
visitType,
transmitter,
transmitter_id = "ydl_trk_pxl",
checkpoint;
//:::::::::::::::::::::::::::::::::::
//:: tag form node with an indentifier
//:: set this.leadForm
this.setForm({
node : "#JoinForm_2",
uniq_id : "ydl_trk"
});
if ( !this.leadForm ) return false;
//:: pass in a root node to observe the event from
checkpoint = this.setupEvtCheckPoint( rootNode , postEvent, verified );
if (!checkpoint) return false;
//:: proceed with tracking logic once event is verified at checkpoint.
//:: the checkpoint triggers the event below
(checkpoint.node).on(verified, function () {
var $this = $(this), payLoad;
//:: shut down checkpoint. event and dispatching node have been verified.
self.closeEvtCheckPoint();
//:: get visit type data (obj literal)
visitType = self.visitTypeObj();
//:: get form data as object
formData = ( self.getFormData() );
//:: merge objects.
$.extend(formData, visitType);
//:: build query string
payLoad = Util.obj2QueryStr( formData );
//:: build transmitter (img w/src)
transmitter = self.buildLeadTransmitter({
uniq_id : transmitter_id,
payload : payLoad
});
//:: inject transmitter into the dom. (invokes GET)
self.transmitPayLoad( transmitter );
// :: query dom for injected transmitter.
// :: when found, trigger submit on leadForm
// :: note*: checkpoint is closed. form will now POST normally.
$this.nodeListener({
cn : "#" + transmitter_id,
tries : 40,
interval : 100,
always_exec_cb : true,
cb : function () {
( self.leadForm.node ).normalizeForm().trigger( postEvent );
}
});
});
});
// end sandbox
});
});
}( window ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment