Skip to content

Instantly share code, notes, and snippets.

@SoreThumb
SoreThumb / utm_tracking
Last active December 7, 2017 02:25 — forked from devinsays/utm_tracking
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.
(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() {