Fork of https://gist.github.com/devinsays/6831221 ; Yes, Devin basically wrote a few lines that I had to expand into 50. :) See some example uses in the content below. This fork allows you to use the current URL to change links on the page-- includes auto-replacement of existing UTM variables.
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
(function($) { | |
$.fn.utm_tracking = function(domain, source, medium, campaign) { | |
$(this).find('a[href^="' + domain + '"]').each(function() { | |
var url = $(this).attr('href'); | |
$(this).attr( 'href', url + '?utm_source=' + source + '&utm_medium=' + medium + '&utm_campaign=' + campaign ); | |
}); | |
} | |
$.fn.utm_tracking_via_obj = function(domain, utmObj) { | |
utmObj = utmObj || $.utm_data_from_url(); | |
$(this).find('a[href^="' + domain + '"]').each(function() { | |
var url = $(this).attr('href'); | |
$(this).attr( 'href', $.utm_data_replace_in_url(utmObj,url) ); | |
}); | |
} | |
$.utm_data_from_url = function(inUrl) { | |
var outObject = {}, urlChange = inUrl || (window.location.href); | |
urlChange = ((urlChange.charAt(0) === '?' && urlChange.substring(1)) || urlChange.split('?').slice(1).join('?') ); | |
if (urlChange.length <= 0) | |
return null; | |
$.each(urlChange.split('&'), function(i,v){ if ($.inArray(v.split('=')[0],['utm_campaign','utm_medium','utm_source']) !== -1) outObject[v.split('=')[0]] = v.split('=')[1]; }); | |
outObject.toString = function() { | |
var outString = []; | |
$.each(outObject, function(i,v) { typeof v !== 'function' && outString.push(i+'='+v); }) | |
return outString.join('&'); | |
} | |
return outObject; | |
} | |
$.utm_obj_to_suffix = function(utmObj, inUrl) { | |
var outUrl = inUrl || window.location.href; | |
return outUrl.indexOf('?') !== -1 ? outUrl + '&' + utmObj.toString() : outUrl + '?' + utmObj.toString(); | |
} | |
$.utm_data_replace_in_url = function(newUtm, inUrl) { | |
var outUrl = inUrl || window.location.href, | |
oldUtms = $.utm_data_from_url(inUrl); | |
if (oldUtms) { | |
$.each(oldUtms, function(i,v){ | |
if (typeof v !== 'function') | |
outUrl = outUrl.replace(i + '=' + v,''); | |
}); | |
outUrl = outUrl.replace(/&$/,'').replace('&&','&').replace('&&','&').replace('?&','?').replace(/&$/,'').replace(/\?$/,''); | |
} | |
return $.utm_obj_to_suffix(newUtm, outUrl); | |
} | |
}(jQuery)); | |
//Examples | |
$(document).ready( function() { | |
$('body').utm_tracking('http://www.example.com','example_source','example_link','example_campaign'); //Thanks, Devin | |
$('body').utm_tracking_via_obj('http://www.example.com'); // Uses UTM data from current URL, replaces them in object attributes | |
$('body').utm_tracking_via_obj('http://www.example.com', $.utm_data_from_url('?utm_campaign=foobar') ); // Uses UTM data from the second parameter's querystring (IE: utm_campaign=foobar in this case) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment