Skip to content

Instantly share code, notes, and snippets.

@chook
Created August 19, 2020 16:23
Show Gist options
  • Save chook/cced21c932f1579d24b73a2bef546633 to your computer and use it in GitHub Desktop.
Save chook/cced21c932f1579d24b73a2bef546633 to your computer and use it in GitHub Desktop.
var MIXPANEL_API_KEY = ‘xxxxxxxxxxx’;
var TAKIPI_MIXPANEL_IDENTIFIED = false;
var TAKIPI_MIXPANEL_COOKIE = 'TKP_MP_DATA';
var IS_REGISTERED_USER_SINGED_IN= 'takipiRegisteredUserLoggedIn';
var mp_old_distinct_id = '';
mp_fill_old_distinct_id = function()
{
try
{
mp_old_distinct_id = getParameterByName("mp");
if (mp_old_distinct_id.length > 0)
{
window.history.replaceState(null, null, removeURLParameter(window.location.href, "mp"));
}
}
catch (err) {}
}
mp_init = function()
{
mixpanel.init(MIXPANEL_API_KEY, {'loaded': mp_loaded_callback});
};
mp_loaded_callback = function()
{
// if IS_REGISTERED_USER_SINGED_IN exists then we have a MP user and so we'll want to send
// people properties to it. To do so, mixpanel requires us to run .identify first
//
if ($.cookie(IS_REGISTERED_USER_SINGED_IN))
{
mp_identify();
}
mp_handle_utm();
mp_handle_page_load();
mp_track_site_page();
};
mp_track_site_account_links = function()
{
mixpanel.track_links("#mainCreateAccountBtn", "Create Account Clicked", {'referrer': document.referrer });
mixpanel.track_links("#footerCreateAccountBtn", "Footer Create Account Clicked", {'referrer': document.referrer });
};
mp_track_site_pricing_links = function()
{
mixpanel.track_links("#yearlySavingsPlanBtn", "Yearly Create Account Clicked", {'referrer': document.referrer });
mixpanel.track_links("#monthlyPlanBtn", "Monthly Create Account Clicked", {'referrer': document.referrer });
};
mp_handle_utm = function()
{
var campaign_keywords = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term'];
var keywordIndex;
var peopleProps = {};
var eventOnceProps = {};
var eventPreRegistrationProps = {};
var eventPostRegistrationProps = {};
for (keywordIndex = 0; keywordIndex < campaign_keywords.length; ++keywordIndex)
{
campaignKeyword = campaign_keywords[keywordIndex];
var campaignKeyWordValue = getParameterByName(campaignKeyword);
if (campaignKeyWordValue.length > 0)
{
peopleProps[campaignKeyword] = campaignKeyWordValue;
eventOnceProps[campaignKeyword + ' site first'] = campaignKeyWordValue;
eventOnceProps[campaignKeyword + ' Takipi first'] = campaignKeyWordValue;
eventPreRegistrationProps[campaignKeyword + ' site last pre registration'] = campaignKeyWordValue;
eventPreRegistrationProps[campaignKeyword + ' Takipi last pre registration'] = campaignKeyWordValue;
eventPostRegistrationProps[campaignKeyword + ' site last post registration'] = campaignKeyWordValue;
eventPostRegistrationProps[campaignKeyword + ' Takipi last post registration'] = campaignKeyWordValue;
}
}
if ($.isEmptyObject(peopleProps) == false)
{
mp_people_set(peopleProps, true);
mixpanel.register_once(eventOnceProps);
if (mp_is_registered_user())
{
mixpanel.register(eventPostRegistrationProps);
}
else
{
mixpanel.register(eventPreRegistrationProps);
}
}
};
mp_handle_page_load = function()
{
var curDate = new Date();
if (!window.location.origin) {
window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}
var href = window.location.origin + window.location.pathname;
var ref = document.referrer;
var peopleProps = {
"First seen": curDate,
"Site first seen": curDate,
"Visitor first page": href,
"Site first page": href
};
var eventOnceProps = {
"Takipi first seen": curDate,
"Site first seen": curDate,
"Takipi first touch": href,
"Site first touch": href,
"Takipi first referrer": ref,
"Site first referrer": ref
};
var eventPreRegistrationProps = {
"Takipi last touch pre registration": href,
"Site last touch pre registration": href,
"Takipi last referrer pre registration": ref,
"Site last referrer pre registration": ref
};
var eventPostRegistrationProps = {
"Takipi last touch post registration": href,
"Site last touch post registration": href,
"Takipi last referrer post registration": ref,
"Site last referrer post registration": ref
};
mp_people_set(peopleProps, true);
mixpanel.register_once(eventOnceProps);
if (mp_is_registered_user())
{
mixpanel.register(eventPostRegistrationProps);
}
else
{
mixpanel.register(eventPreRegistrationProps);
}
};
mp_is_registered_user = function()
{
return (!!$.cookie(IS_REGISTERED_USER_SINGED_IN));
};
mp_track_site_page = function()
{
mixpanel.track("Site page seen");
};
mp_site_default_init = function()
{
mp_fill_old_distinct_id();
mp_init();
mp_track_site_account_links();
};
mp_site_pricing_init = function()
{
mp_site_default_init();
mp_track_site_pricing_links();
};
mp_setCookie = function(key, value)
{
var topLevelDomain = mp_topLevelDomain();
$.cookie(key, value, {path: '/', domain: topLevelDomain, expires: 30});
};
mp_removeCookie = function(key)
{
var topLevelDomain = mp_topLevelDomain();
// We set the cookie to expire in 0 days
$.cookie(key, '', {path: '/', domain: topLevelDomain, expires: 0});
};
mp_topLevelDomain = function()
{
try
{
var domain = window.location.hostname;
var isIp = /^(((1?[1-9]?|10|2[0-4])\d|25[0-5])($|\.(?!$))){4}$/;
if (isIp.test(domain))
{
return domain;
}
var domainParts = domain.split('\.');
if (domainParts.length > 2)
{
domainParts = domainParts.slice(1, domainParts.length);
}
var topLevelDomain = '.' + domainParts.join('.');
return topLevelDomain;
}
catch(e)
{
return '.overops.com';
}
};
// Use this instead of - mixpanel.people.set / .set_once
//
mp_people_set = function(prop, setOnce)
{
if ((mp_is_registered_user()) &&
(TAKIPI_MIXPANEL_IDENTIFIED))
{
mp_submit_mp_cookie_data();
if (setOnce)
{
mixpanel.people.set_once(prop);
}
else
{
mixpanel.people.set(prop);
}
}
else
{
mp_save_properties_to_cookie(prop, setOnce);
}
};
//Use this instead of - mixpanel.people.increment
//
mp_people_inc = function(prop)
{
if ((mp_is_registered_user()) &&
(TAKIPI_MIXPANEL_IDENTIFIED))
{
mp_submit_mp_cookie_data();
mixpanel.people.increment(prop);
}
else
{
mp_increment_property_in_cookie(prop);
}
};
//Use this instead of - mixpanel.identify
//
mp_identify = function(uid)
{
mixpanel.identify(uid);
TAKIPI_MIXPANEL_IDENTIFIED = true;
mp_submit_mp_cookie_data();
}
//Use this instead of - mixpanel.identify
//
mp_alias = function(uid)
{
mixpanel.alias(uid);
TAKIPI_MIXPANEL_IDENTIFIED = true;
mp_submit_mp_cookie_data();
}
mp_submit_mp_cookie_data = function()
{
if ((!mixpanel) ||
(!mp_is_registered_user()) ||
(!TAKIPI_MIXPANEL_IDENTIFIED))
{
return;
}
var rawCookieData = $.cookie(TAKIPI_MIXPANEL_COOKIE);
var cookieData;
try
{
cookieData = JSON.parse(rawCookieData);
}
catch(e) {}
if (!cookieData)
{
return;
}
mp_submit_mp_cookie_data_internal(cookieData, true);
mp_submit_mp_cookie_data_internal(cookieData, false);
mp_removeCookie(TAKIPI_MIXPANEL_COOKIE);
};
mp_submit_mp_cookie_data_internal = function(cookieData, setOnce)
{
var sectionName = ((setOnce) ? "propertiesOnce": "properties");
var cookieSectionData = cookieData[sectionName];
if (!cookieSectionData)
{
return;
}
if (setOnce)
{
mixpanel.people.set_once(cookieSectionData);
}
else
{
mixpanel.people.set(cookieSectionData);
}
}
mp_save_properties_to_cookie = function(data, setOnce)
{
var rawCookieData = $.cookie(TAKIPI_MIXPANEL_COOKIE) || "{}";
var cookieData;
try
{
cookieData = JSON.parse(rawCookieData);
}
catch(e)
{
cookieData = {};
}
var sectionName = ((setOnce) ? "propertiesOnce": "properties");
if (!cookieData[sectionName])
{
cookieData[sectionName] = {};
}
cookieSectionData = cookieData[sectionName];
var keys = Object.keys(data);
for (var i = 0; i < keys.length; i++)
{
var key = keys[i];
if ((setOnce) &&
(cookieSectionData[key]))
{
continue;
}
var value = data[key];
cookieSectionData[key] = value;
}
cookieData[sectionName] = cookieSectionData;
mp_setCookie(TAKIPI_MIXPANEL_COOKIE, JSON.stringify(cookieData));
};
mp_increment_property_in_cookie = function(prop)
{
var rawCookieData = $.cookie(TAKIPI_MIXPANEL_COOKIE) || "{}";
var cookieData;
try
{
cookieData = JSON.parse(rawCookieData);
}
catch(e)
{
cookieData = {};
}
if (!cookieData.properties)
{
cookieData.properties = {};
}
var currentValue = cookieData.properties[prop];
if ((!currentValue) ||
(isNaN(currentValue)))
{
currentValue = 0;
}
currentValue++;
cookieData.properties[prop] = currentValue;
mp_setCookie(TAKIPI_MIXPANEL_COOKIE, JSON.stringify(cookieData));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment