Created
September 23, 2013 22:01
-
-
Save jbwyme/6677566 to your computer and use it in GitHub Desktop.
Mixpanel 2.2 AMD/Require example #2: Async If you REALLY want async support, you'll need to update your stub's methods once the mixpanel lib is loaded. I don't recommend this because (among other reasons) it results in window.mixpanel !== mixpanel after the copy. This also means you must protect against race conditions on synchronous calls like …
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
<html> | |
<head> | |
<title>Mixpanel AMD Example - Async</title> | |
<script type="text/javascript" src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script> | |
<script type="text/javascript"> | |
requirejs.config({ | |
paths : { 'mixpanel-lib': "//cdn.mxpnl.com/libs/mixpanel-2.2.min" } | |
}); | |
define("mixpanel", function(require) { | |
var b = window.mixpanel || []; | |
if (!b.__SV) { var 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 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.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 } | |
// go ahead and start loading the mixpanel-lib | |
require(['mixpanel-lib']); | |
b.init("YOUR TOKEN", {loaded: function() { | |
// now that we know mixpanel is loaded, copy the prop references to our module def | |
for(var prop in window.mixpanel) { | |
b[prop] = window.mixpanel[prop]; | |
} | |
}}); | |
return b; | |
}); | |
</script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
require(['mixpanel'], function(mixpanel) { | |
mixpanel.track("my event", {prop1: "val1"}); | |
console.log(mixpanel.get_distinct_id()); // probably undefined | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment