Skip to content

Instantly share code, notes, and snippets.

@jaspreetsidhu
Created May 22, 2019 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaspreetsidhu/119de0a988cfca4edcb3488efac830cc to your computer and use it in GitHub Desktop.
Save jaspreetsidhu/119de0a988cfca4edcb3488efac830cc to your computer and use it in GitHub Desktop.
Pardot Javascript UTM
<script type="text/javascript">
// Parse the URL
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, " "));
}
// Give the URL parameters variable names
var source = getParameterByName('utm_source');
var medium = getParameterByName('utm_medium');
var campaign = getParameterByName('utm_campaign');
var term = getParameterByName('utm_term');
var content = getParameterByName('utm_content');
// Put the variable names into the hidden fields in the form. selector should be "p.YOURFIELDNAME input"
document.querySelector("p.source input").value = source;
document.querySelector("p.utm_medium input").value = medium;
document.querySelector("p.utm_campaign input").value = campaign;
document.querySelector("p.utm_term input").value = term;
document.querySelector("p.utm_content input").value = content;
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment