Last active
November 8, 2022 14:36
-
-
Save cougrimes/491c9cbe3d397f8ccc94 to your computer and use it in GitHub Desktop.
UTM Parser for Marketo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//First, let's get the URI and have it split out all parameters | |
var getURLParams = function() { | |
var temp = {}; | |
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function() { | |
var decode = function(s) { | |
return decodeURIComponent(s.split("+").join(" ")); | |
}; | |
temp[decode(arguments[1])] = decode(arguments[2]); | |
}); | |
return temp; | |
}; | |
//Then, we'll take those parameters we temporarily saved and turn them into JS variables | |
var $_GET = getURLParams(); | |
var utmMedium = getURLParams()["utm_medium"]; | |
var utmSource = getURLParams()["utm_source"]; | |
var utmCampaign = getURLParams()["utm_campaign"]; | |
var utmTerm = getURLParams()["utm_term"]; | |
var utmContent = getURLParams()["utm_content"]; | |
var initPath = document.location.origin + document.location.pathname; | |
if (initPath.slice(-1)!="/"){ | |
var initPath = initPath + "/"; | |
} | |
//Now, time to tell Marketo what we want. Because associateLead (which writes directly into variables) needs us to both use SHA-1 generation and to have a known lead, this will not get the results we want. But even anonymous leads can visit URLs! | |
//But first: let's check if there's any valid UTM value before getting crazy here. | |
if(typeof utmSource=='string') { | |
Munchkin.munchkinFunction('visitWebPage', { | |
url: initPath + utmSource, params: utmSource | |
}); | |
} | |
if(typeof utmMedium=='string') { | |
Munchkin.munchkinFunction('visitWebPage', { | |
url: initPath + utmMedium, params: utmMedium | |
}); | |
} | |
if(typeof utmCampaign=='string') { | |
Munchkin.munchkinFunction('visitWebPage', { | |
url: initPath + utmCampaign, params: utmCampaign | |
}); | |
} | |
if(typeof utmTerm=='string') { | |
Munchkin.munchkinFunction('visitWebPage', { | |
url: initPath + utmTerm, params: utmTerm | |
}); | |
} | |
if(typeof utmContent=='string') { | |
Munchkin.munchkinFunction('visitWebPage', { | |
url: initPath + utmContent, params: utmContent | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cougrimes , I know this gist was from quite awhile back, but I'm interested to know if you might have an example of how to use GTM to accomplish this with the DOM Ready requirement?
Might it look something like this? (forgive me if I'm totally off, pretty new to all of this):