The following solution works for mixpanel api 2.2
add mixpanel with the following adjustment -
path : { 'mixpanel' : '//cdn.mxpnl.com/libs/mixpanel-2.2.min' } shim : { 'mixpanel' : { exports : 'mixpanel' }, }
and use the following requirejs module instead of the fragment given by the mixpanel -
define('mixpanel-snippet', [], function(){ 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 } b.init("YOUR TOKEN"); require(['mixpanel'], function(mixpanel){}); return b; });
I just took a snippet from mixpanel, removed the asynchronous loading of the mixpanel, and wrapped it in the definition of the requirejs module.
Change โYOUR TOKENโ at the bottom of the module.
Call Demand Usage Example -
require([ 'mixpanel-snippet', ], function (mixpanel) { mixpanel.track("Landing Page with AMD SHIM"); });
EDIT: the second is the correct answer after a little modification. the way the mixpanel script works, it is necessary that the init call in the fragment be made before the mixer panel actually loads. the trick is to require mixpanel after calling init.i edited the second answer and deleted the first, and here's the gist
EDIT: Reply to the comment by @johanandren The requirement follows the AMD principle, and the order in which the scripts are loaded is not fixed. If you need to download mixpanel before using mixpanel-snippet, you can use the following hack.
//at the end of mixpanel-snippet code mentioned above force the script to block until mixpanel is loaded b.init("YOUR TOKEN"); var wait = true; require(['mixpanel'], function(mixpanel){wait = false;}); while(wait){} return b;
** it violates Async boot functions for AMD, makes the script block plus even in the batila mixpan flag, the download is asynchronous, and availability for initial api calls is not guaranteed