Developers
The most common way of including mParticle’s JavaScript code on your webpage is by embedding our snippet in the <head>
tag of your page. This snippet automatically fetches the necessary code for the core mParticle web SDK, plus the necessary client-side code for any enabled integrations, from mParticle’s CDN.
For most clients, the best way to instrument web properties is by including the snippet on each web page, as detailed in Initialization.
However, from version 2.9.5
of the Web SDK, mParticle gives you the ability to self-host the SDK, by installing it to your JavaScript project via NPM.
There are two key reasons why you might want to self-host:
In your project folder, install the main package, plus client-side code for any integrations you want to include, from the command line.
npm i @mparticle/web-sdk
npm i @mparticle/web-mixpanel-kit
package.json
By default, mParticle packages will be added to your package.json
with a caret version statement such as "^2.9.5-rc.3"
. This allows you to automatically upgrade to a new version, as long as the left-most number isn’t incremented.
For example, if your package.json
includes the following dependency: "@mparticle/web-sdk": "^2.9.5-rc.3"
, an npm install
might install version 2.9.7
, but not version 3.0
. If you wish, you can edit your dependency to an exact version.
Config options are the same as described in the Getting Started guide.
mParticleConfig = {
isDevelopmentMode: true,
identifyRequest: {
userIdentities: { email: 'h.jekyll.md@example.com', customerid: 'h.jekyll.md' }
},
identityCallback: function(result) {
// Do something once an identity call has been made.
// For more information, see https://docs.mparticle.com/developers/sdk/web/idsync/#sdk-initialization-and-identify
console.log(result);
}
}
When using the mParticle snippet, the SDK will automatically initialize itself on load. If you switch to self-hosting, you will need to explicitly initialize the SDK in your code.
The mParticle.init()
method requires your API key, and the config object created in step 3.
mParticle.init("YOUR_API_KEY", mParticleConfig)
// your main index.js file
import mParticle from '@mparticle/web-sdk';
import mixpanelKit from '@mparticle/web-mixpanel-kit'
function myIdentityCallback(result) {
console.log(result)
}
var mParticleConfig = {
isDevelopmentMode: true,
identifyRequest: {
userIdentities: { email: 'h.jekyll.md@example.com', customerid: 'h.jekyll.md' }
},
identityCallback: function(result) {
// Do something once an identity call has been made.
// For more information, see https://docs.mparticle.com/developers/sdk/web/idsync/#sdk-initialization-and-identify
console.log(result);
},
};
mixpanelKit.register(mParticleConfig);
mParticle.init('apiKey', mParticleConfig)
Was this page helpful?