Skip to content

Instantly share code, notes, and snippets.

@ialex
Last active June 9, 2017 22:30
Show Gist options
  • Save ialex/025d5ac03b87debc55bf81fc225219e8 to your computer and use it in GitHub Desktop.
Save ialex/025d5ac03b87debc55bf81fc225219e8 to your computer and use it in GitHub Desktop.
Attach utm to links on page so they pass from page to page uses jQuery
<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
jQuery(document).ready(function() {
var siteURL = "http://" + top.location.host.toString();
jQuery("a[href^='"+siteURL+"'], a[href^='/'], a[href^='./'], a[href^='../'], a[href^='#']").attr('href', function(i, h) {
var param, value;
var utms = ['utm_source', 'utm_term', 'utm_content', 'utm_medium', 'utm_campaign'];
utms.forEach(function(utm) {
value = getParameterByName(utm);
param = utm + '=' + value;
if(value.length > 0) {
h = h + (h.indexOf('?') != -1 ? "&" : "?") + param;
}
})
return h;
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment