@wonderfulday/nuxt-mixpanel
v1.0.2
Published
Mixpanel Module for Nuxt.js
Readme
@wonderfulday/nuxt-mixpanel
Mixpanel Module for Nuxt.js
Setup
- Add
@wonderfulday/nuxt-mixpaneldependency to your project
yarn add @wonderfulday/nuxt-mixpanel # or npm install @wonderfulday/nuxt-mixpanel- Add
@wonderfulday/nuxt-mixpanelto themodulessection ofnuxt.config.js
export default {
modules: [
'@wonderfulday/nuxt-mixpanel',
],
mixpanel: {
id: '',
config: {
debug: true
}
}
}Runtime Config
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,
}
}
}Options
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',
}
}enabled
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
}
}debug
Whether $mixpanel API calls like track are logged to the console.
Manual Mixpanel Initialization
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.
Router Integration
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.
Usage
Pushing events
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'
})Development
- Clone this repository
- Install dependencies using
yarn installornpm install - Start development server using
yarn devorMIXPANEL_ID=<your mixpanel token> yarn devif you want to provide custom MIXPANEL_TOKEN.
Contributors
License
Copyright (c) Wonderfulday
