Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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