Skip to content

Instantly share code, notes, and snippets.

@THEtheChad
Created July 13, 2018 20:35
Show Gist options
  • Save THEtheChad/70fda4fdd18ecd5e9f183c3d3b4d3142 to your computer and use it in GitHub Desktop.
Save THEtheChad/70fda4fdd18ecd5e9f183c3d3b4d3142 to your computer and use it in GitHub Desktop.
Function for forwarding query parameters for the purpose of analytics and attribution tracking.
function prependParams(){
var params = document.location.search;
if(!params) return;
params = params + '&';
var nodeList = document.getElementsByTagName('a');
var anchors = Array.prototype.slice.call(nodeList);
var l = anchors.length;
var q = /\?/;
while(l--){
var anchor = anchors[l];
var href = anchor.href;
var match = q.exec(anchor.href);
if(!match){
match = {index: href.length};
}
anchor.href = href.slice(0, match.index) + params + href.slice(match.index + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment