Skip to content

Instantly share code, notes, and snippets.

@RayHollister
Last active May 25, 2022 23:22
Show Gist options
  • Save RayHollister/e26c8a04e536c19e0be12f801fd33934 to your computer and use it in GitHub Desktop.
Save RayHollister/e26c8a04e536c19e0be12f801fd33934 to your computer and use it in GitHub Desktop.
A bit of Javascript to pass the UTM to a WPForm since Pantheon strips the UTMs
// Get the UTMs with Javascript and pass them to the WPForm on the page
function getQueryParams(qs) {
qs = qs.split('+').join(' ');
var params = {},
tokens, re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
window.onload = function returnUTMs() {
// Replace formID value with the ID number from the WPForm.
var formID = 82;
var query_params = getQueryParams(document.location.search);
// console.log(query_params);
utm_source = query_params['utm_source'];
utm_medium = query_params['utm_medium'];
utm_campaign = query_params['utm_campaign'];
utm_content = query_params['utm_content'];
document.getElementById('wpforms-'+formID+'-field_2').value = utm_source;
document.getElementById('wpforms-'+formID+'-field_3').value = utm_medium;
document.getElementById('wpforms-'+formID+'-field_4').value = utm_campaign;
document.getElementById('wpforms-'+formID+'-field_5').value = utm_content;
document.getElementById('wpforms-82-field_1').value = utm_source + " - " + utm_medium + " - " + utm_campaign + " - " + utm_content;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment