Skip to content

wonderfuldaygroup/nuxt-mixpanel

Repository files navigation

@wonderfulday/nuxt-mixpanel

npm version npm downloads License

Mixpanel Module for Nuxt.js

📖 Release Notes

Setup

  1. Add @wonderfulday/nuxt-mixpanel dependency to your project
yarn add @wonderfulday/nuxt-mixpanel # or npm install @wonderfulday/nuxt-mixpanel
  1. Add @wonderfulday/nuxt-mixpanel to the modules section of nuxt.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

  1. Clone this repository
  2. Install dependencies using yarn install or npm install
  3. Start development server using yarn dev or MIXPANEL_ID=<your mixpanel token> yarn dev if you want to provide custom MIXPANEL_TOKEN.

Contributors

License

MIT License

Copyright (c) Wonderfulday

Releases

No releases published

Packages

No packages published