Skip to content

Instantly share code, notes, and snippets.

@transbetty
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save transbetty/ffa7374d8f71667a1e44 to your computer and use it in GitHub Desktop.
Save transbetty/ffa7374d8f71667a1e44 to your computer and use it in GitHub Desktop.
A small JS Script to add usual Google Analytics utm_ variables to outgoing links so you can track your referrers in NATS system.
/* A small JS Script to add usual Google Analytics utm_ variables to outgoing links so you can track your referrers in NATS system. This works only if NATS has Automatic Campaigns (autocampaigns) enabled: http://wiki.toomuchmedia.com/index.php/Automatic_Campaigns tags
NATS 4.1 also supports tags / autotags which can be handy to split utm_ parameters apart
Sample outgoing affil link http://www.affiliate.com/?utm_source=TS&utm_campaign=Header
author: transbetty@gmail.com, www.transbetty.com
This is client-side based solution as oposed to my original solution: https://gist.github.com/transbetty/e23f4277c48416173f6c which causes problmes with server-side solution.
*/
// get URL params with JS http://goo.gl/Yljccv
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var myUtmSource = getParameterByName('utm_source');
var myUtmCampaign = getParameterByName('utm_campaign');
var myUtmMedium = getParameterByName('utm_medium');
//console.log (myUtmSource);
//console.log (myUtmCampaign);
//console.log (myUtmMedium);
// basic JS library from quirksmode http://goo.gl/7XJHS
// instead update with JS with domain settting http://goo.gl/muKhv9
var Cookie =
{
set: function(name, value, days)
{
var domain, domainParts, date, expires, host;
if (days)
{
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
}
else
{
expires = "";
}
host = location.host;
if (host.split('.').length === 1)
{
// no "." in a domain - it's localhost or something similar
document.cookie = name+"="+value+expires+"; path=/";
}
else
{
// Remember the cookie on all subdomains.
//
// Start with trying to set cookie to the top domain.
// (example: if user is on foo.com, try to set
// cookie to domain ".com")
//
// If the cookie will not be set, it means ".com"
// is a top level domain and we need to
// set the cookie to ".foo.com"
domainParts = host.split('.');
domainParts.shift();
domain = '.'+domainParts.join('.');
document.cookie = name+"="+value+expires+"; path=/; domain="+domain;
// check if cookie was successfuly set to the given domain
// (otherwise it was a Top-Level Domain)
if (Cookie.get(name) == null || Cookie.get(name) != value)
{
// append "." to current domain
domain = '.'+host;
document.cookie = name+"="+value+expires+"; path=/; domain="+domain;
}
}
},
get: function(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i=0; i < ca.length; i++)
{
var c = ca[i];
while (c.charAt(0)==' ')
{
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
},
erase: function(name)
{
Cookie.set(name, '', -1);
}
};
// read GA cookie http://goo.gl/46vG4
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}
// change URL inspired by http://goo.gl/pPFRnZ
/* is URL par present?
- yes
- add params to all urls
- set cookie
- no
- check cookie new
- check cookie old
- add params to all urls from
1. new cookie
2. old cookie, delete old cookie
*/
function changeUrl() {
var linkOutClass = 'linkOut';
var cookieName = 'trackRefs';
var cookieNameOld = 'trackSource';
var cookieValue = myUtmSource + ',' + myUtmCampaign + ',' + myUtmMedium;
var natsVariableString = '?autocamp=';
if ( myUtmSource || myUtmCampaign || myUtmMedium ) {
var allLinkoutLinks = document.getElementsByClassName(linkOutClass);
for(var i = 0; i < allLinkoutLinks.length; i++) {
allLinkoutLinks[i].href = allLinkoutLinks[i].href + natsVariableString + encodeURIComponent(myUtmSource+','+myUtmCampaign+ ',' + myUtmMedium);
}
Cookie.set(cookieName,encodeURIComponent(cookieValue),30);
} else {
var cookieValue = Cookie.get(cookieName);
var cookieValueOld = Cookie.get(cookieNameOld);
if (cookieValue == undefined && cookieValueOld ) {
cookieValue = cookieValueOld;
Cookie.erase(cookieNameOld);
Cookie.set(cookieName,cookieValue,30);
}
if (cookieValue) {
var allLinkoutLinks = document.getElementsByClassName(linkOutClass);
for(var i = 0; i < allLinkoutLinks.length; i++) {
allLinkoutLinks[i].href = allLinkoutLinks[i].href + natsVariableString + cookieValue ;
}
}
}
}
changeUrl();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment