Last active
June 9, 2017 22:30
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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