Mixpanel 2.2 within an AMD structured web-app - e.g. require.js

后端 未结 4 634
[愿得一人]
[愿得一人] 2021-02-06 09:55

I\'m attempting to make use of Mixpanel event tracking in a single page site based on Backbone.js and require.js.

Looking at the snippet that Mixpanel provide for cut-an

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-06 10:17

    This worked for me. Place your mixpanel snippet in your js/lib directory named mixpanel-snippet.js.

    In your app.js add the following shim to require.config:

    'mixpanel-snippet': {
      exports: 'mixpanel'
    }
    

    In your require function add 'mixpanel-snippet' to the required array and initialize mixpanel:

    require(['jquery', 'backbone', 'app/router', 'mixpanel'], function ($, Backbone, Router) {
        var router = new Router();
        Backbone.history.start();
        mixpanel.init(key);
        mixpanel.track("Landed on Start up Page");
    });
    

    I can provide a full app.js example if it would help but this should get your started. Let me know if this works.

提交回复
热议问题