Created
November 5, 2015 22:25
-
-
Save italodr/1fa1e2aafe6d3e0dc526 to your computer and use it in GitHub Desktop.
UTM generator
This file contains hidden or 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
<html> | |
<head> | |
( ... ) | |
</head> | |
<body data-utmcampaing="my-campaing" data-utmdate="20151105"> | |
<a href="http://some-address-to-go.com" data-utmterm="the_term">Some link</a> | |
<a href="http://some-address-to-go.com?with=query" data-utmterm="the_term">Some link with querystring</a> | |
<a href="http://some-address-to-go.com" no-utm>Some link not trackable</a> | |
</body> | |
</html> |
This file contains hidden or 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
$(function() { | |
var utmCampaign = $('body').data('utmcampaign'), | |
utmDate = $('body').data('utmdate'); | |
var getTrackingTail = function(utmCampaign, utmTerm, hasHash) { | |
if (typeof hasHash === undefined) { | |
hasHash = false; | |
} | |
var trackingTail = (hasHash) ? '&' : '?'; | |
trackingTail += 'utm_medium=' + 'the_medium'; | |
trackingTail += '&utm_source=' + 'the_source'; | |
trackingTail += '&utm_content=' + 'the_content'; | |
trackingTail += '&utm_campaign=' + utmCampaign; | |
trackingTail += '&utm_term=' + utmTerm; | |
return trackingTail; | |
}; | |
$('a').each(function(index) { | |
var theHref = $(this).attr('href'), | |
hasHref = (typeof theHref !== undefined), | |
noUtm = $(this).attr('no-utm'), | |
utmTerm = $(this).data('utmterm'), | |
hasHash = false, | |
trackingTail; | |
if (hasHref) { | |
hasHash = (theHref.indexOf('?') > 0); | |
} | |
if (noUtm !== undefined) { | |
return; | |
} | |
utmTerm = (utmTerm === undefined) ? utmDate : utmTerm +'-'+ utmDate; | |
trackingTail = getTrackingTail(utmCampaign, utmTerm, hasHash); | |
theHref += trackingTail; | |
$(this).attr('href', theHref); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment