Skip to content

Instantly share code, notes, and snippets.

@omerfarooX
Last active April 8, 2016 23:09
Show Gist options
  • Save omerfarooX/5484e99e555f1c508b12821795dfdcd8 to your computer and use it in GitHub Desktop.
Save omerfarooX/5484e99e555f1c508b12821795dfdcd8 to your computer and use it in GitHub Desktop.
infusionsoft lead source tracking modified
/**
* @author: Tyler Garns http://www.tylergarns.com/infusionsoft/infusionsoft-lead-source-tracking/)
* @modified: 20 Aug 2012
* @notes: Free to use and distribute without altering this comment. Would appreciate a link back :)
*/
// Breaks cookie into an object of keypair cookie values
function crumbleCookie(c)
{
var cookie_array = document.cookie.split(';');
var keyvaluepair = {};
for (var cookie = 0; cookie < cookie_array.length; cookie++)
{
var key = cookie_array[cookie].substring(0, cookie_array[cookie].indexOf('=')).trim();
var value = cookie_array[cookie].substring(cookie_array[cookie].indexOf('=')+1, cookie_array[cookie].length).trim();
keyvaluepair[key] = value;
}
if (c)
return keyvaluepair[c] ? keyvaluepair[c] : null;
return keyvaluepair;
}
/**
* Gets Affiliate Cookie if exist
*/
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
// if cookie exists
if (offset != -1) {
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
/**
* For GA cookie explanation, see http://services.google.com/analytics/breeze/en/ga_cookies/index.html.
*
* @return - <void>
*
* @pre-condition - pageTracker initialised properly
* @post-condition - provides 'get' methods to access specific values in the Google Analytics cookies
*/
function gaCookies()
{
// Cookie syntax: domain-hash.ftime.?.?.utmcsr=X|utmccn=X|utmcmd=X|utmctr=X
var utmz = function() {
var utmz_array, source, medium, name, term, content, gclid;
if (crumbleCookie('__utmz'))
utmz_array = crumbleCookie('__utmz').split('.');
else
return null;
var utms = utmz_array[4].split('|');
for (var i = 0; i < utms.length; i++) {
var key = utms[i].substring(0, utms[i].indexOf('='));
var val = decodeURIComponent(utms[i].substring(utms[i].indexOf('=')+1, utms[i].length));
val = val.replace(/^\(|\)$/g, ''); // strip () brackets
switch(key)
{
case 'utmcsr':
source = val;
break;
case 'utmcmd':
medium = val;
break;
case 'utmccn':
name = val;
break;
case 'utmctr':
term = val;
break;
case 'utmcct':
content = val;
break;
case 'utmgclid':
gclid = val;
break;
}
}
return {
'cookie': utmz_array,
'source': source,
'medium': medium,
'name': name,
'term': term,
'content': content,
'gclid': gclid
};
};
// Establish public methods
// utmz cookies
this.getCampaignSource = function () { return (utmz() && utmz().source) ? utmz().source : null };
this.getCampaignMedium = function () { return (utmz() && utmz().medium) ? utmz().medium : null };
this.getCampaignName = function () { return (utmz() && utmz().name) ? utmz().name : null };
this.getCampaignTerm = function () { return (utmz() && utmz().term) ? utmz().term : null};
this.getCampaignContent = function () { return (utmz() && utmz().content) ? utmz().content : null };
}
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
function getHiddenFields(te){
var gac = new gaCookies();
if (getParameterByName("_utmsource")) {
var leadsource = getParameterByName("_utmsource");
if (getParameterByName("_utm_campaign")) { leadsource = leadsource + "-" + getParameterByName("_utm_campaign"); }
if (getParameterByName("_utm_content")) { leadsource = leadsource + "-" + getParameterByName("_utm_content"); }
var category = getParameterByName("_utm_medium");
var term = getParameterByName("_utm_term");
} else {
var leadsource = gac.getCampaignSource();
if (gac.getCampaignName()) { leadsource = leadsource + "-" + gac.getCampaignName(); }
if (gac.getCampaignContent()) { leadsource = leadsource + "-" + gac.getCampaignContent(); }
var category = gac.getCampaignMedium();
var term = gac.getCampaignTerm();
}
if(leadsource == "Affiliate") {
leadsource = leadsource + " - " + get_cookie("ifaffiliatecode");
}
jQuery('form').each(function(){
//check if the form has hiddenfield 'inf_form_xid'
if(this.inf_form_xid){
// Set Leadsource field
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "inf_field_LeadSourceName");
input.setAttribute("value", leadsource);
this.appendChild(input);
// Set LeadSource Category field
if (category) {
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "inf_field_LeadSourceCategoryName");
input.setAttribute("value", category);
this.appendChild(input);
}
// Set Term field
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "inf_custom"+te);
input.setAttribute("value", term);
this.appendChild(input);
}
//check if the form has hiddenfield 'infusion_xid'
if(this.infusion_xid){
// Set Leadsource field
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "Contact0Leadsource");
input.setAttribute("value", leadsource);
this.appendChild(input);
// Set Term field
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "Contact0"+te);
input.setAttribute("value", term);
this.appendChild(input);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment