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 | |
}); | |
} |
Hey @nduralt,
If you're working with GTM, there's definitely a faster way of processing this using the data layer rather than using this script (which is meant for vanilla instances, and as you note, requires DOM Ready)--you can just skip all the variable capturing, use GTM's variables, and have the script just do everything from line 30 on.
@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):
<script type="text/javascript">
(function() {
var didInit = false;
function initMunchkin() {
if(didInit === false) {
didInit = true;
Munchkin.init('xxxxxx');
console.log("init");
}
}
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'https://munchkin.marketo.net/munchkin.js';
s.onreadystatechange = function() {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
initMunchkin();
}
};
s.onload = initMunchkin;
document.getElementsByTagName('head')[0].appendChild(s);
})();
</script>
var utmMedium = {{UTM_Medium}};
var utmSource = {{UTM_Source}};
var utmCampaign = {{UTM_Campaign}};
var utmTerm = {{UTM_=Term}};
var utmContent = {{UTM_Content}};
var initPath = document.location.origin + document.location.pathname;
if (initPath.slice(-1)!="/"){
var initPath = initPath + "/";
}
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
So I spent the better part of a day figuring why this wasn't working through GTM and I realized that you can't fire it until it's DOM ready