Skip to content

Instantly share code, notes, and snippets.

@sorinpantis
Last active January 5, 2017 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorinpantis/1bab148dcbd192742c2364e0c7502285 to your computer and use it in GitHub Desktop.
Save sorinpantis/1bab148dcbd192742c2364e0c7502285 to your computer and use it in GitHub Desktop.
Get the UTM parameters form the URL as custom attributes in Intercom
// 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 settings = {
app_id: app_id,
email: 'jane.doe@example.com',
created_at: 1234567890,
name: 'Jane Doe',
user_id: '9876123'
}
// Add UTM parameters to the Intercom object if they are not null
if (utms["utm_campaign"]) { settings.utm_campaign = utms["utm_campaign"]; }
if (utms["utm_medium"]) { settings.utm_medium = utms["utm_medium"]; }
if (utms["utm_source"]) { settings.utm_source = utms["utm_source"]; }
Intercom('boot', settings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment