Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created October 4, 2013 19:18
Show Gist options
  • Save devinsays/6831221 to your computer and use it in GitHub Desktop.
Save devinsays/6831221 to your computer and use it in GitHub Desktop.
Small jQuery plugin to add UTM tracking to links in an element.
jQuery.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 );
});
}
// Example Call
$(document).ready( function() {
$('body').utm_tracking('http://www.example.com','example_source','example_link','example_campaign');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment