Last active
February 11, 2020 01:27
-
-
Save dorian-davis/b53dcc48706476aec8699cd8875879dc to your computer and use it in GitHub Desktop.
Grab UTM params from URL and add them to the Intercom Javascript object
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
<script> | |
// Get UTM Params | |
var querystring = document.location.search; // Get the string after the domain | |
querystring = querystring.substring(1); // Remove the leading "?" | |
querystring = querystring.split('&'); // Split the string in utm variables | |
var utms = {}; | |
for (var i = 0; i < querystring.length; i++) { | |
var utm_part = querystring[i]; | |
if (utm_part) { | |
var keyValue = utm_part.split("="); | |
console.log("keyvalue: " + keyValue); | |
if (keyValue.length > 1) { | |
utms[keyValue[0]] = keyValue[1]; | |
} | |
} | |
} | |
// END Get UTM Params | |
var APP_ID = 'your_intercom_app_id' | |
window.intercomSettings = { | |
app_id: APP_ID | |
} | |
// Add UTM parameters to the Intercom object if they are not null | |
if (utms["utm_campaign"]) { window.intercomSettings.utm_campaign = utms["utm_campaign"]; } | |
if (utms["utm_medium"]) { window.intercomSettings.utm_medium = utms["utm_medium"]; } | |
if (utms["utm_source"]) { window.intercomSettings.utm_source = utms["utm_source"]; } | |
(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/'+APP_ID;var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()</script> | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment