Skip to content

Instantly share code, notes, and snippets.

@lithiumlab
Created May 16, 2019 21:24
Show Gist options
  • Save lithiumlab/2e1615b2f17ad854d27835cf7645318b to your computer and use it in GitHub Desktop.
Save lithiumlab/2e1615b2f17ad854d27835cf7645318b to your computer and use it in GitHub Desktop.
Conimar tracking campaign parameters from URL trough forms
var queryForm = function(settings){
var utm_data = ['utm_source','utm_medium','utm_campaign','utm_term','utm_content'];
var reset = settings && settings.reset ? settings.reset : false;
var self = window.location.toString();
var querystring = self.split("?");
if (querystring.length > 1) {
var pairs = querystring[1].split("&");
for (i in pairs) {
var keyval = pairs[i].split("=");
var supported_params = utm_data.indexOf(keyval[0]) !=-1;
var paramval = decodeURIComponent(keyval[1]);
console.log('parameter',keyval[0], supported_params, paramval);
if ( (reset || sessionStorage.getItem('wwlc_cf_'+keyval[0]) === null) && supported_params) {
sessionStorage.setItem('wwlc_cf_'+keyval[0], paramval);
}
}
}
var hiddenFields = document.querySelectorAll("input[type=hidden]");
if(document.getElementById('wwlc-registration-form')){
for (var i=0; i<hiddenFields.length; i++) {
var param = sessionStorage.getItem('wwlc_cf_'+hiddenFields[i].name);
if (param && document.getElementById('wwlc_cf_'+hiddenFields[i].name) ) document.getElementById(''+hiddenFields[i].name).value = param;
}
}
}
queryForm({'reset':true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment