// script which reads UTM params from the URL and adds them to all links on the current page $(document).ready(function() { var utms = ['medium', 'source', 'campaign', 'term', 'content'], utmStrings = []; var domain = ''; // add domain here, only links to given domain will be siffixed with utm params var domainRegexp = new RegExp(domain.replace(/\./g, '\\.')); utms.forEach(function(utm) { var match = window.location.search.match(new RegExp('utm_' + utm + '=([^&]+)')); if(match) { utmStrings.push('utm_' + utm + '=' + match[1]); } }); if(utmStrings.length) { $('a').each(function() { var link = this.href, prefix, params = ''; if( link.match(domainRegexp) && !link.match(/[?&]utm_/) ) { prefix = link.match(/\?/) ? '&' : '?'; this.href = link + prefix + utmStrings.join('&'); } }); } });