Giter Site home page Giter Site logo

madhums / rn-redux-mixpanel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danscan/rn-redux-mixpanel

0.0 2.0 0.0 23 KB

Configurable redux middleware that sends your actions & user profile data to Mixpanel. Works with React Native!

JavaScript 100.00%

rn-redux-mixpanel's Introduction

rn-redux-mixpanel

Configurable redux middleware that sends your actions & user profile data to Mixpanel. It also works with React Native ;)

Installation

npm install --save rn-redux-mixpanel

Example

// store/index.js
import mixpanel from 'rn-redux-mixpanel'
import { INIT_PERSISTENCE, HYDRATE, SESSION_ACTIVITY, SIGN_IN } from '../../constants/ActionTypes'
import humanize from 'underscore.string'

// define a blacklist to be used in the ignoreAction filter
const blacklist = [
  INIT_PERSISTENCE,
  HYDRATE,
  SESSION_ACTIVITY,
];

// Export configured mixpanel redux middleware
export default mixpanel({

  // add ignore action filter
  ignoreAction: (action) => {
    return blacklist.indexOf(action.type) > -1;
  },

  // Mixpanel Token
  token: YOUR_MIXPANEL_TOKEN,

  // derive Mixpanel event name from action and/or state
  selectEventName: (action, state) => humanize(action.type),

  // Per-action selector: Mixpanel event `distinct_id`
  selectDistinctId: (action, state) => {
    if (state.session && state.session.userId) {
      return state.session.userId
    } else if (SIGN_IN === action.type && action.user) {
      return action.user._id
    }
  },

  // Per-action selector: Mixpanel Engage user profile data
  selectUserProfileData: (action, state) => {
    const user = action.user

    // Only update user profile data on SIGN_IN action type
    if (SIGN_IN === action.type && user) {
      // User data to `$set` via Mixpanel Engage request
      const userProfileData = {
        '$first_name': user['first_name'],
        '$last_name': user['last_name'],
        '$email': user['email_address'],
        '$created': user['date_created'],
      }

      return userProfileData
    }
  },
})

Usage

Configure the mixpanel redux middleware by invoking with an options object, containing:

  1. token – Your Mixpanel application token.
  2. ignoreAction – An optional function, that receives an action and returns a truthy value, if it should be ignored.
  3. selectDistinctId – A selector function that returns the distinct_id (user id), given the action and store state.
  4. selectUserProfileData – A selector function that returns user profile data for a Mixpanel Engage request, given the action and store state.
  5. selectEventName – A optional selector function that returns the Mixpanel event name, given the action and store state. By default action.type.
  6. selectProperties - An optional selector function that returns Mixpanel properties to add to the request, given the action and store state.

rn-redux-mixpanel's People

Contributors

danscan avatar cgarvis avatar tomru avatar pewallin avatar por avatar

Watchers

James Cloos avatar  avatar

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.