Created
July 15, 2014 15:12
-
-
Save Ianfeather/4e3f5204497046fda26d to your computer and use it in GitHub Desktop.
Affil Tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a utility function that breaks a URL into an object | |
// parseUri 1.2.2 | |
// (c) Steven Levithan <stevenlevithan.com> | |
// MIT License | |
function parseUri(d){for(var a=parseUri.options,d=a.parser[a.strictMode?"strict":"loose"].exec(d),c={},b=14;b--;)c[a.key[b]]=d[b]||"";c[a.q.name]={};c[a.key[12]].replace(a.q.parser,function(d,b,e){b&&(c[a.q.name][b]=e)});return c} | |
parseUri.options={strictMode:!1,key:"source protocol authority userInfo user password host port relative path directory file query anchor".split(" "),q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}}; | |
function getCampaignMedium(landinguri, campcode, refuri){ | |
/**********Campaign management plugin *********************/ | |
//setup the different campaign channels available | |
var channels = { | |
"seo":"seo", | |
"ppc":"sem", | |
"sem":"sem", | |
"soc":"social", | |
"soc_other":"social", | |
"eml":"email", | |
"pns":"partnership", | |
"aff":"affiliate", | |
"qr":"qr-code", | |
"pdf":"pdf-code", | |
"pub":"publishing-code", | |
"cs":"cs-microsite-code", | |
"us":"us-paid-other", | |
"uk":"uk-paid-other", | |
"au":"au-paid-other", | |
"other_ref":"other-referring-domain", | |
"other":"unknown-paid", | |
"dem":"dem", | |
"direct":"direct" | |
} | |
//is there a campaign code? | |
if(campcode){ | |
//yes there is | |
//derive channel from campcode | |
var ccparts = [], i=0; | |
//define all possible campaign code separators | |
var separators = ["|", "-", "_", ":"]; | |
//find out which separator is being used and split by it | |
while(ccparts.length < 2 && i < separators.length){ | |
ccparts = campcode.toLowerCase().split(separators[i]); | |
i++; | |
} | |
//check if the first part of the camp code defines the channel | |
if(channels[ccparts[0]]){ | |
//it does so just return its value | |
return channels[ccparts[0]]; | |
}else if(landinguri.queryKey.s_kwcid || landinguri.queryKey.mckv){ | |
//the url contains a paid campaign name | |
//covers the anomoly of mckv starting with dem | |
if(landinguri.queryKey.mckv && landinguri.queryKey.mckv.match(/^dem/i)){ | |
return channels.dem; | |
}else{ | |
return channels.sem; | |
} | |
}else{ | |
//is another unknown paid campaign | |
return channels.other; | |
} | |
}else if(refuri && refuri.host != "" && !refuri.host.match(/lonelyplanet./i)){ | |
//no campaign code so derive channel from referrer etc | |
//look at referrer etc | |
//is this natural search? | |
if(refuri.host.match(/google\.|bing\.|yahoo\.|ask\.|aol\./i)){ | |
//yes | |
return channels.seo; | |
}else if(refuri.host.match(/facebook\.|linkedin\.|twitter\.|myspace\.|youtube\.|ning\.|^t\.co|xanga\./i)){ | |
//no - its social from a key social network | |
return channels.soc; | |
}else if(refuri.host.match(/orkut\.|friendster\.|vimeo\.com|bebo\.com|hi5\.com|yuku\.com|cafemom\.com|xanga\.com|livejournal\.com|blogspot\.com|wordpress\.com|myspace\.com|digg\.com|reddit\.com|stumbleupon\.com|twine\.com|yelp\.com|mixx\.com|chime\.in|delicious\.com|tumblr\.com|disqus\.com|intensedebate\.com|plurk\.com|slideshare\.net|backtype\.com|netvibes\.com|mister-wong\.com|diigo\.com|flixster\.com|12seconds\.tv|zooomr\.com|identi\.ca|jaiku\.com|flickr\.com|imeem\.com|dailymotion\.com|photobucket\.com|fotolog\.com|smugmug\.com|classmates\.com|myyearbook\.com|mylife\.com|tagged\.com|brightkite\.com|hi5\.com|yuku\.com|cafemom\.com|plus\.google\.com|instagram\.com|prezi\.com|newsvine\.com|pinterest\.com|wiebo\.com|nevkontakte\.com|qzone\.qq\.com|cloob\.com/i)){ | |
//no - its social from another social network | |
return channels.soc_other | |
}else{ | |
//no matching referrer but it is a referred visitor | |
return channels.other_ref; | |
} | |
}else{ | |
//no camp code nor ref uri | |
return channels.direct; | |
} | |
} | |
// IMPORTANT NOTE | |
// all dimension and campaign tracking must be set before the pageview is sent | |
//make sure there is a query params and not gclid - we won't touch campaign overrides where a gclid is present | |
if(landinguri.query != "" && !landinguri.queryKey.gclid){ | |
var landinguri = parseUri(document.URL); //split out the landing uri | |
var refuri = parseUri(document.referrer); // split out the referring uri | |
//is there an affil query parameter? | |
if(landinguri.queryKey.affil){ | |
//yes | |
//get the campaign medium (channel) | |
var campmedium = getCampaignMedium(landinguri, landinguri.queryKey.affil, refuri); | |
//set the custom dimension 9 with the campaign code (affil) | |
ga('set', 'dimension9', landinguri.queryKey.affil); | |
//if the campaign medium is not seo or direct | |
if(campmedium != "seo" && campmedium != "direct"){ | |
//not seo or direct (which ga handles well anyway) so lets override the campaign vars | |
ga('set', { | |
'campaignName': landinguri.queryKey.affil, | |
'campaignMedium': campmedium, | |
'campaignSource': refuri.host | |
}); | |
} | |
}else if(landinguri.queryKey.lpaffil){ | |
//no but there is a lpaffil parameter | |
//so set the custom dimension 10 | |
ga('set', 'dimension10', landinguri.queryKey.lpaffil); | |
}else if(landinguri.queryKey.intaffil){ | |
//no but there is a intaffil parameter | |
//so set the custom dimension 11 | |
ga('set', 'dimension11', landinguri.queryKey.intaffil); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment