Skip to content

Instantly share code, notes, and snippets.

@italodr
Created November 5, 2015 22:25
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 italodr/1fa1e2aafe6d3e0dc526 to your computer and use it in GitHub Desktop.
Save italodr/1fa1e2aafe6d3e0dc526 to your computer and use it in GitHub Desktop.
UTM generator
<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>
$(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