Mixpanel Module for Nuxt.js
- Add
@wonderfulday/nuxt-mixpanel
dependency to your project
yarn add @wonderfulday/nuxt-mixpanel # or npm install @wonderfulday/nuxt-mixpanel
- Add
@wonderfulday/nuxt-mixpanel
to themodules
section ofnuxt.config.js
export default {
modules: [
'@wonderfulday/nuxt-mixpanel',
],
mixpanel: {
id: '',
config: {
debug: true
}
}
}
You can use runtime config if need to use dynamic environment variables in production. Otherwise, the options will be hardcoded during the build and won't be read from nuxt.config
anymore.
export default {
modules: [
'@wonderfulday/nuxt-mixpanel'
],
mixpanel: {
id: '', // Used as fallback if no runtime config is provided
},
publicRuntimeConfig: {
mixpanel: {
id: process.env.MIXPANEL_ID,
}
}
}
Defaults:
export default {
mixpanel: {
debug: false,
enabled: true,
id: undefined,
windowStorageVariable: 'mixpanelStorage',
pageTracking: false,
pageViewEventName: 'nuxtRoute',
autoInit: true,
respectDoNotTrack: true,
scriptId: 'nuxtMixpanel',
scriptURL: 'cdn.mxpnl.com/libs/mixpanel-2-latest.min.js',
}
}
Mixpanel module uses a debug-only version of $mixpanel
during development (nuxt dev
).
You can explicitly enable or disable it using enabled
option:
export default {
mixpanel: {
// Always send real Mixpanel events (also when using `nuxt dev`)
enabled: true
}
}
Whether $mixpanel
API calls like track
are logged to the console.
There are several use cases that you may need more control over initialization:
- Block Mixpanel before user directly allows (GDPR realisation or other)
- Dynamic ID based on request path or domain
- Initialize with multi containers
- Enable Mixpanel on page level
nuxt.config.js
:
export default {
modules: [
'@wonderfulday/nuxt-mixpanel'
],
plugins: [
'~/plugins/mixpanel'
]
}
plugins/mixpanel.js
:
export default function({ $mixpanel, route }) {
$mixpanel.init('TOKEN')
}
- Note: All events will be still buffered in data layer but won't send until
init()
method getting called.
You can optionally set pageTracking
option to true
to track page views.
The default event name for page views is nuxtRoute
, you can change it by setting the pageViewEventName
option.
You can push events into the configured layer:
this.$mixpanel.track('event name', {
distinct_id: 'unique client id',
property_1: 'value 1',
property_2: 'value 1',
property_3: 'value 1'
})
- Clone this repository
- Install dependencies using
yarn install
ornpm install
- Start development server using
yarn dev
orMIXPANEL_ID=<your mixpanel token> yarn dev
if you want to provide custom MIXPANEL_TOKEN.
Copyright (c) Wonderfulday