mixpanel-js
mixpanel-js copied to clipboard
Mixpanel librairy_name hard to type
Nowadays if we want to use 2 mixpanel projects in the same app with have to use the librairy name:
mixpanel.init('new token', { your: 'config' }, 'project1');
mixpanel.init('new token', { your: 'config' }, 'project2');
And then we use it like this:
mixpanel.project1.track();
mixpanel.project2.track();
This is hard to use with typescript and currently @types/mixpanel-browser
does not handle this case.
It would seems simpler to do:
const project1 = new Mixpanel('new token', { your: 'config' });
const project2 = new Mixpanel('new token', { your: 'config' });
project1.track();
project2.track()
Once the lib code has loaded, mixpanel.init()
returns the new MixpanelLib
instance, so you can do
const project1 = mixpanel.init('new token', { your: 'config' }, 'project1');
project1.track();
If you're bundling the SDK with your other JS (i.e. loading the lib as a module via import
or require
), then the above should Just Work.
If however you're using the asynchronous script tag loader (https://github.com/mixpanel/mixpanel-js/blob/master/mixpanel-jslib-snippet.min.js), this won't work as it looks like the stubbed init function that runs in the embed code doesn't return anything. But I'm guessing if you're worrying about types then you're more likely to be using the module loader anyway.