Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created October 4, 2013 19:18
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
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