Giter Club home page Giter Club logo

gatsby-plugin-mixpanel's Introduction

npm package Build Status codecov

gatsby-plugin-mixpanel

For Gatsby v5

(see previous releases for Gatsby v2, v3 & v4)

Integrate mixpanel on your gatsby project

Install

npm install --save gatsby-plugin-mixpanel or yarn add gatsby-plugin-mixpanel

How to use

Declare plugin

Add the plugin in your gatsby-config.js and set your mixpanel api token

plugins: [
  {
    resolve: 'gatsby-plugin-mixpanel',
    options: {
      apiToken: 'YOUR_MIXPANEL_API_TOKEN', // required
    },
  },
];

Using mixpanel by passing props

You can access to mixpanel library in the props of your component by using the function withMixpanel available in the plugin

import { withMixpanel } from 'gatsby-plugin-mixpanel'

class HelloWorld extends Component {
    componentDidMount() {
        const { mixpanel } = this.props
        mixpanel.track('Hello'); // send event 'Hello' to mixpanel
    }
    render() {/*...*/}
}

export default withMixpanel()(HelloWorld)

// or with decorators if your project supports
@withMixpanel()
class HelloWorld extends Component {
    /*...*/
}

Using mixpanel with react hooks

Your project must have a version of react that supports react-hooks.

Import the useMixpanel hook.

import { useMixpanel } from 'gatsby-plugin-mixpanel'

function HelloWorld() {
  const mixpanel = useMixpanel()
  mixpanel.track('Hello');
  return (
    <div>
      <button onClick={() => mixpanel.track('Hello button') }>Hello</button>
    </div>
  )
}

Configuration

The plugin is configurable, here are the configs by default.

plugins: [
  {
    resolve: 'gatsby-plugin-mixpanel',
    options: {
      apiToken: 'YOUR_MIXPANEL_API_TOKEN', // required
      // optional fields, default values
      enableOnDevMode: true, // if false mixpanel will be activated on NODE_ENV=production only
      mixpanelConfig: null, // override specific config for mixpanel initialization https://github.com/mixpanel/mixpanel-js/blob/8b2e1f7b/src/mixpanel-core.js#L87-L110
      pageViews: null, // see below
       // set pageViews to 'all' and use this option to set the same event name for all page view events
      trackPageViewsAs: null, // optionally: set an Event Name to use for all page views, eg: trackPageViewsAs: 'Page view'
      getPageViewTransformerFn: null, // optionally: function body as a string to customize the event sent to mixpanel. Receives one parameter: location. Example 'return () => ({url: location.pathname})'
    },
  },
];

The pageViews param is null by default, if you want to autotrack some page you can declare an object url path and events that will be send on mixpanel. Or if you want to track every route changes set pageViews to 'all'.

example:

plugins: [
  {
    resolve: 'gatsby-plugin-mixpanel',
    options: {
      apiToken: 'YOUR_MIXPANEL_API_TOKEN',
      pageViews: {
        '/blog': 'Page blog view', // an event 'Page blog view' will be send to mixpanel on every visit on the /blog page
        '/404': 'Page 404 view',
      },
      mixpanelConfig: null, // you can override default config for mixpanel library https://github.com/mixpanel/mixpanel-js/blob/8b2e1f7b/src/mixpanel-core.js#L87-L110
      /*
      pageViews: 'all' // to track every route changes
      */
    },
  },
];

Enjoy this little plugin and feel free to contribute ๐Ÿ˜ƒ

gatsby-plugin-mixpanel's People

Contributors

adamduren avatar byeokim avatar dependabot[bot] avatar emerzh avatar github-actions[bot] avatar jonaswide avatar lablancas avatar marclar avatar mozartdiniz avatar plag avatar tallyb avatar thomascarvalho avatar timbrandin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gatsby-plugin-mixpanel's Issues

Build error while setting mixpanelConfig

I'm trying out how loaded hook works with the library.

{
  resolve: `gatsby-plugin-mixpanel`,
  options: {
    apiToken: process.env.MIXPANEL_KEY || ' ',
    mixpanelConfig: {
      loaded: function(mixpanel) {
        mixpanel.register_once({
          'First Page Visit': new Date().toISOString(),
        });
      },
    },
  },
},

but during build it fails with

 4951 |  */
  4952 | MixpanelLib.prototype.get_config = function(prop_name) {
> 4953 |     return this['config'][prop_name];
       | ^
  4954 | };
  4955 | 
  4956 | /**


  WebpackError: TypeError: Cannot read   property 'name' of undefined
  
  - mixpanel.cjs.js:4953 MixpanelLib..    /node_modules/mixpanel-browser/bui    ld/mixpanel.cjs.js.MixpanelLib.get    _config
    [lib]/[mixpanel-browser]/build/mix    panel.cjs.js:4953:1
  
  - mixpanel.cjs.js:4979 MixpanelLib..    /node_modules/mixpanel-browser/bui    ld/mixpanel.cjs.js.MixpanelLib.toS    tring
    [lib]/[mixpanel-browser]/build/mix    panel.cjs.js:4979:1

Mixpanel is not receiving any data

Thanks for creating this plugin! I'm having trouble getting it to work.

I've installed based on the Hooks implementation and have this in my src/pages/index.js file for an initial Site Visit track.

  import { useMixpanel } from 'gatsby-plugin-mixpanel';
   
  const mixpanel = useMixpanel();
  useEffect(() => {
    mixpanel.track('Site Visit');
  }, []);

only track first page view

I would like to only track the initial page load then track all the navigation events in a different way, from what I can see tracking page events is all or nothing though. Any recommendations on how to make it work?

Allow auto tracking on route change

exports.onRouteUpdate = ({ location }, pluginOptions) => {
  const options = getOptions(pluginOptions);

  if (!isEnable(options)) {
    return;
  }

  if (options.pageViews) {
    const mixpanelEvent = options.pageViews[location.pathname];
    if (mixpanelEvent) {
      mixpanel.track(mixpanelEvent, location);
    }
  }
};

How about something like

if pageViews is array
check if current route should be tracked
if pageViews is set to all
track

Currently I have to whitelist 15+ pages.

Missing coma in config

Hi there, I think there is a missing comma after the "mixpanelConfig: null"

plugins: [
{
resolve: 'gatsby-plugin-mixpanel',
options: {
apiToken: 'YOUR_MIXPANEL_API_TOKEN', // required
// optional fields, default values
enableOnDevMode: true, // if false mixpanel will be activated on NODE_ENV=production only
mixpanelConfig: null, // override specific config for mixpanel initialization https://github.com/mixpanel/mixpanel-js/blob/8b2e1f7b/src/mixpanel-core.js#L87-L110
pageViews: null, // see below
// set pageViews to 'all' and use this option to set the same event name for all page view events
trackPageViewsAs: null, // optionally: set an Event Name to use for all page views, eg: trackPageViewsAs: 'Page view'
getPageViewTransformerFn: null, // optionally: function body as a string to customize the event sent to mixpanel. Receives one parameter: location. Example 'return () => ({url: location.pathname})'
},
},
];

Plugin does not work on my site

I have tried this plugin for two weeks now but for some reason, mixpanel UI says that mixpanle has not been installed on my Gatsby website (https://www.danielemaasit.com/). I followed the install-instructions to the letter but I can't figure out what's wrong. It's driving me crazy. I installed other related npm packages like react-mixpanel, mixpanel-browser as well, hoping this would solve it but it doesn't. Please help. My config looks like this:

{
      resolve: 'gatsby-plugin-mixpanel',
      options: {
        apiToken: config.mixpanelAPIToken, // required
        // optional fields, default values
        // debug: false, // if true activate debug mode on mixpanel library
        // enableOnDevMode: true, // if false mixpanel will be activated on NODE_ENV=production only
        // pageViews: null, //
      },
    }

My mixpanel Token is saved in a config.js file.

Gatsby v5 support

Hi, just wondering if this will be updated to support version 5 of Gatsby?

Build error while using Hooks

This very simple example

const Lorem = () => {
  if (mixpanel) {
    const sessionId = mixpanel.get_distinct_id();
  }

// ...
}

fails with

 4973 |  */
  4974 | MixpanelLib.prototype.get_property = function(property_name) {
> 4975 |     return this['persistence']['props'][property_name];
       | ^
  4976 | };
  4977 | 
  4978 | MixpanelLib.prototype.toString = function() {


  WebpackError: TypeError: Cannot read prop  erty 'props' of undefined

Any ideas @thomascarvalho ?

Mixpanel new version

New Mixpanel version use import {MixpanelProvider, MixpanelConsumer} from "react-mixpanel";

I kind of lost with this new way of coding.

Any one know how to use MixpanelConsumer ?

Support React 18

This plugin is great, but it's stopping me upgrade to React 18, please could you update the peer dependencies

Edited to only mention React 18, not Gatsby 4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.