Skip to content

Instantly share code, notes, and snippets.

@sansmischevia
Created March 21, 2018 21:52
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 sansmischevia/2d094966edb252ed9c2eb908126c491d to your computer and use it in GitHub Desktop.
Save sansmischevia/2d094966edb252ed9c2eb908126c491d to your computer and use it in GitHub Desktop.
function getQueryParam(url, param) {
// Expects a raw URL
param = param.replace(/[[]/, "\[").replace(/[]]/, "\]");
var regexS = "[\?&]" + param + "=([^&#]*)",
regex = new RegExp(regexS),
results = regex.exec(url);
if (results === null || (results && typeof(results[1]) !== 'string' && results[1].length)) {
return '';
} else {
return decodeURIComponent(results[1]).replace(/\W/gi, ' ');
}
};
function campaignParams() {
var campaign_keywords = 'utm_source utm_medium utm_campaign utm_content utm_term'.split(' '),
kw = '',
params = {},
first_params = {};
var index;
for (index = 0; index < campaign_keywords.length; ++index) {
kw = getQueryParam(document.URL, campaign_keywords[index]);
if (kw.length) {
params[campaign_keywords[index] + ' [last touch]'] = kw;
}
}
for (index = 0; index < campaign_keywords.length; ++index) {
kw = getQueryParam(document.URL, campaign_keywords[index]);
if (kw.length) {
first_params[campaign_keywords[index] + ' [first touch]'] = kw;
}
}
mixpanel.people.set(params);
mixpanel.people.set_once(first_params);
mixpanel.register(params);
}
campaignParams();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment