Mix n' Match Analytics Campaign Tracking

Google Analytics alternative campaign trackingHow to use both Google Analytics’ built in campaign tracking and your own custom campaign tracking variables.

If your campaign links already contains tracking variables you can map these variables to Google Analytics by modifying the Google tracking code. This works great unless some of your campaigns contain Google Analytics tracking variables and other campaigns contain your own custom variables. If you find yourself trying to track standard Google Analytics campaign links and your own modified campaign variables this is a simple solution using custom JavaScript.

Parsing the variables from the URL

You need a way to tell the Google analytics tracking code that the link contains a custom variable. The way we do this is with JavaScript.

Somewhere before the <body> tag insert the following code:

<script type=”text/javascript”>
// skip the first character, we are not interested in the “?”
var query = location.search.substring(1);
// split at each “&” character to give a list of “name=value” pairs
var pairs = query.split(“&”);
for (var i=0; i<pairs.length; i++) {
//alert(“Item ” + i + ” is:” + pairs[i] + “.”);
// break each pair at the first “=” to obtain the name and value
var pos = pairs[i].indexOf(“=”);
var argname = pairs[i].substring(0,pos).toLowerCase();
var value = pairs[i].substring(pos+1).toLowerCase();

// process only possible argname that represent custom campaign variables
if (argname == “my_campaign”) {
var gaTracking = “modified”; //url string contains a custom variable and should use the modified tracking code
}
else {
var gaTracking = “standard”; // url doesn’t contain custom tracking proceed as normal
}
}
</script>

Modify your existing GA code:

<script type=”text/javascript”>
if (gaTracking ==”modified”) {
var pageTracker = _gat._getTracker(“UA-123456-7”);
pageTracker._setCampSourceKey(“my_src”); // source
pageTracker._setCampTermKey(“my_kwd”); // term/keyword
pageTracker._setCampNameKey(“my_campaign”); // name
pageTracker._setCampMediumKey(“my_med”); // medium
pageTracker._trackPageview();
}
else{
var pageTracker = _gat._getTracker(“UA-123456-7”);
pageTracker._trackPageview();
}
document.write(unescape(“%3Cscript src='” + gaTracking + “.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>

Make sure to replace UA-123456-7 with your unique Google analytics account ID. Also, if you need to track more unique campaigns you can modify the first batch of code to include those additional variables.

That’s all there is to it, you can now track both your custom variables and Google’s standard campaign variables.

If this has been helpful or you have any questions please post a comment below!