The official Mixpanel JavaScript browser client library
npm install mixpanel-browserThe Mixpanel JavaScript Library is a set of methods attached to a global mixpanel object
intended to be used by websites wishing to send data to Mixpanel projects. A full reference
is available here.
mixpanel-browser to distinguish it from Mixpanel's server-side Node.js library, available on NPM as mixpanel). To install into a project using NPM with a front-end packager such as Vite or Webpack:``sh`
npm install --save mixpanel-browser
You can then import the lib:
`javascript
import mixpanel from 'mixpanel-browser';
mixpanel.init("YOUR_TOKEN", {autocapture: true});
mixpanel.track("An event");
`
NOTE: the default mixpanel-browser bundle includes a bundled mixpanel-recorder SDK. We provide the following options to exclude mixpanel-recorder if you do not intend to use session replay or want to reduce bundle size:
To load the core SDK with no option of session recording:
`javascript`
import mixpanel from 'mixpanel-browser/src/loaders/loader-module-core';
To load the core SDK and optionally load session recording bundle asynchronously (via script tag):
`javascript`
import mixpanel from 'mixpanel-browser/src/loaders/loader-module-with-async-recorder';
If you are leveraging browser JavaScript modules, you can use importmap to pull in this library.
`html`
Then you are free to import mixpanel-browser in your javascript modules.
`js
// main.js
import mixpanel from 'mixpanel-browser';
mixpanel.init('YOUR_TOKEN', {autocapture: true, debug: true, persistence: 'localStorage'});
`
- Build: npm run build-distRunning tests
- Install development dependencies: npm install
- Run unit tests: npm test
- Start test server for browser tests: npm run integration_test
- Browse to http://localhost:3001/tests/ and choose a scenario to runIn the future we plan to automate the last step with a headless browser to streamline development (although
Mixpanel production releases are tested against a large matrix of browsers and operating systems).
Generating and publishing documentation
- Create bundled source build: npm run build-dist
- Generate Markdown: npm run dox (result is at doc/readme.io/javascript-full-api-reference.md)
- Publish to readme.io via the rdme util: RDME_API_KEY=