Skip to content

Instantly share code, notes, and snippets.

@robksawyer
Last active October 30, 2020 00:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robksawyer/e9c27651fb2ac3bd5e6ab345f7cb43aa to your computer and use it in GitHub Desktop.
Save robksawyer/e9c27651fb2ac3bd5e6ab345f7cb43aa to your computer and use it in GitHub Desktop.
Solution to using UTM parameters & cookies to capture lead source information. See https://nation.marketo.com/thread/24866
<script type="text/javascript">
// getQueryParams
//This grabs the UTM parameters from the URL
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;
}
// getQueryBoolParams
// Handles getting boolean parameters
// See https://regex101.com/r/QFnVPM/1
function getQueryBoolParams(qs) {
qs = qs.split("+").join(" ");
var params = {}, tokens,
re = /[?&]([^=]+)&|&([^=]+)&?/g;
while (tokens = re.exec(qs)) {
params[] = decodeURIComponent(tokens[1]);
}
return params;
}
// checkForUTMDataInForm
// Handles checking to see if the UTM hidden fields exist on the page yet
function checkForUTMDataInForm(query, totalChecks = 100){
var utmInt = setInterval(doCheck, 1000);
var counter = 0;
function doCheck() {
// Kill the interval after total checks reached
if (counter == totalChecks) {
clearInterval(utmInt);
}
// Check to see if the utm_source exists as a test
if( document.hasOwnProperty("utm_source") ){
clearInterval(utmInt);
//This gets the cookies
//ID = MARKETO FIELD NAME
addUTMData(query);
}
}
}
// addUTMData
// Handles adding the UTM data based on the query params
function addUTMData(query){
// Crate an array
var elements = [];
// UTM Source
if(document.hasOwnProperty("utm_source")){
var utmSource = document.getElementById("utm_source");
elements["utm_source"] = utmSource;
elements["utm_source"].value = query.utm_source;
}
// UTM Campaign
if(document.hasOwnProperty("utm_campaign")){
var utmCampaign = document.getElementById("utm_campaign");
elements["utm_campaign"] = utmCampaign;
elements["utm_campaign"].value = query.utm_campaign;
}
// UTM Medium
if(document.hasOwnProperty("utm_medium")){
var utmMedium = document.getElementById("utm_medium");
elements["utm_medium"] = utmMedium;
elements["utm_medium"].value = query.utm_medium;
}
// UTM Medium
if(document.hasOwnProperty("utm_term")){
var utmTerm = document.getElementById("utm_term");
elements["utm_term"] = utmTerm;
elements["utm_term"].value = query.utm_term;
}
// UTM Content
if(document.hasOwnProperty("utm_content")){
var utmContent = document.getElementById("utm_content");
elements["utm_content"] = utmContent;
elements["utm_content"].value = query.utm_content;
}
return elements;
}
// populateCookie
// Handles populating the cookie
// Setting domain and path attributes allow cookies to be read across subdomains
// To have a cookie available to all subdomains, you must put a . in front of your domain.
// Setting the path=/ will have the cookie be available within the entire specified domain.
function populateCookie(domains, query, queryBools){
if (!domains) {
console.log('ERROR: You did not provide any domains.');
return false;
}
var queryContainsBools = queryBools ? true : false;
// Don't forget to change "example.com" to your domain.
if (document.hasOwnProperty('cookie')){
// Traverse through keyed params
for (var key in query) {
//console.log(key, query[key]);
document.cookie = key + "=" + query[key];
// Traverse through the domains
/*
for(var c=0;c<domains.length;c++){
document.cookie = key + "=" + query[key] + ";domain=."+domains[c]+";path=/";
}
*/
}
if (queryContainsBools){
// Traverse through bool params
for (var i=0; i<queryBools.length;i++) {
//console.log(key, query[key]);
// document.cookie = queryBools[i] + "=" + "true"; // Alternate assignment
document.cookie = queryBools[i];
// Traverse through the domains
/*
for(var d=0;d<domains.length;d++){
document.cookie = queryBools[i] + "=" + true + ";domain=."+domains[d]";path=/";
}
*/
}
}
return document.cookie;
}
return false;
}
// Handles filling the cookie with content
//This sets the cookies
var query = getQueryParams(document.location.search);
var queryBools = getQueryBoolParams(document.location.search);
var domains = [ "example.com", "example2.com" ];
populateCookie(domains, query, queryBools);
//This runs the script so the hidden fields are automatically populated.
var totalChecks = 100;
checkForUTMDataInForm(query, totalChecks);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment