-
-
Save pareshsojitra/8df81ace134971b3d17b60adb0f53042 to your computer and use it in GitHub Desktop.
Mixpanel Multiple Domain Tracking
This file contains hidden or 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
// Parses the url for query strings | |
function prefix_return_query_strings( url ) { | |
var urlParams; | |
var match, | |
pl = /\+/g, // Regex for replacing addition symbol with a space | |
search = /([^&=]+)=?([^&]*)/g, | |
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, | |
query = url.search.substring(1); | |
urlParams = {}; | |
while (match = search.exec(query) ) { | |
urlParams[decode(match[1])] = decode(match[2]); | |
} | |
return urlParams; | |
} | |
// If query data is present, track it | |
var query = prefix_return_query_strings( window.location ); | |
if ( query.data ) { | |
var data = atob( query.data ); | |
data = JSON.parse( data ); | |
// Now tracking scripts can be called using data | |
console.log( data.source ); | |
console.log( data.path ); | |
console.log( data.path ); | |
console.log( data.title ); | |
console.log( data.full_url ); | |
} |
This file contains hidden or 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
document.addEventListener("DOMContentLoaded", function(event) { | |
var data = { | |
'source': 'B.com', | |
'path': window.location.pathname, | |
'title': document.title, | |
'full_url' : window.location.href | |
} | |
data = JSON.stringify( data ); | |
data = btoa( data ); | |
var iframe = document.createElement('iframe'); | |
iframe.setAttribute( 'src', 'http://a.com/tracking/?data=' + data ); | |
iframe.style.width = "1px"; | |
iframe.style.height = "1px"; | |
document.body.appendChild(iframe); | |
}); |
This file contains hidden or 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
<?php | |
/** | |
* Template Name: Mixpanel iFrame | |
*/ | |
/* | |
This template is loaded from other domains as an iFrame. | |
This allows us to share the same *.A.com cookie. | |
This template expects the query string "data". | |
This is a base64 encoded stringified object the should contain: | |
* source | |
* path | |
* full_url | |
* title | |
Here's how it is sent client side: | |
var data = { | |
'source': 'B.com', | |
'path': window.location.pathname, | |
'title': document.title, | |
'full_url' : window.location.href | |
} | |
data = JSON.stringify( data ); | |
data = btoa( data ); | |
*/ | |
?> | |
<!doctype html> | |
<html> | |
<title>Mixpanel iFrame</title> | |
<head> | |
<script>(function(e,b){if(!b.__SV){var a,f,i,g;window.mixpanel=b;b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" "); | |
for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};b.__SV=1.2;a=e.createElement("script");a.type="text/javascript";a.async=!0;a.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";f=e.getElementsByTagName("script")[0];f.parentNode.insertBefore(a,f)}})(document,window.mixpanel||[]); | |
</script> | |
</head> | |
<body> | |
<script> | |
// Parses the url for query strings | |
function prefix_return_query_strings( url ) { | |
var urlParams; | |
var match, | |
pl = /\+/g, // Regex for replacing addition symbol with a space | |
search = /([^&=]+)=?([^&]*)/g, | |
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, | |
query = url.search.substring(1); | |
urlParams = {}; | |
while (match = search.exec(query) ) { | |
urlParams[decode(match[1])] = decode(match[2]); | |
} | |
return urlParams; | |
} | |
// If query data is present, track with Mixpanel | |
var query = prefix_return_query_strings( window.location ); | |
if ( query.data ) { | |
var data = atob( query.data ); | |
data = JSON.parse( data ); | |
mixpanel.init("MIXPANEL_TOKEN", { | |
'loaded' : function() { | |
// Tracking | |
mixpanel.register_once({ | |
'initial_source': data.source, | |
'initial_path': data.path, | |
'initial_title': data.title | |
}); | |
mixpanel.register({ | |
'$current_url' : data.full_url, | |
'path': data.path, | |
'title': data.title | |
}); | |
mixpanel.track( 'impression', { | |
'$current_url' : data.full_url, | |
'path' : data.path, | |
'full_url' : data.full_url, | |
'source': data.source, | |
}); | |
console.dir( mixpanel.cookie.props ); | |
} | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment