rangle / redux-beacon

Analytics integration for Redux and ngrx/store
https://rangle.gitbook.io/redux-beacon/
MIT License
668 stars 71 forks source link
analytics angular google-analytics ngrx react react-native redux redux-beacon

Redux Beacon

Redux Beacon

Analytics integration for Redux and ngrx/store

Docs

Introduction

If you're using Redux or ngrx/store to manage your app's state, you can use Redux Beacon to tap into your dispatched actions and map them to events that are consumable by an analytics service (e.g. Google Analytics). With Redux Beacon your entire global state life-cycle becomes trackable.

Redux Beacon Diagram

Supported Targets

Version Package
Google npm @redux-beacon/google-analytics
Google npm @redux-beacon/google-analytics-gtag
Google npm @redux-beacon/google-tag-manager
Google npm @redux-beacon/react-native-google-analytics
Google npm @redux-beacon/react-native-google-tag-manager
Amplitude npm @redux-beacon/amplitude
Segment npm @redux-beacon/segment
Segment npm @redux-beacon/react-native-segment

Third-Party Targets

Version Package
Matomo npm redux-beacon-matomo-tag-manager
Appsflyer npm redux-beacon-react-native-appsflyer

Other Targets

If you don't see your analytics target listed it might be worth a shot doing a quick search on npmjs.org. If all else fails you can always build and provide your own custom Targets!

Integrations

Integration Integrates with Description
redux-dynamic-modules-beacon redux-dynamic-modules Redux Dynamic Modules is a library that allows to modularize redux code. With help of this extension events do not have to be defined in one central location but can be defined for each module individually.

Usage

Examples

_Track a page view on each ROUTE_CHANGED action_

const eventsMap = {
  'ROUTE_CHANGED': trackPageView(action => ({
    page: action.payload.routerState.url,
  })),
}

_Track an event on each VIDEO_SELECTED action, use the state before the action and the state after the action to hydrate the analytics event_

const eventsMap = {
  'VIDEO_SELECTED': trackEvent((action, prevState, nextState) => ({
    category: 'Videos',
    action: action.type,
    label: prevState.videos.currentCampaign,
    value: nextState.videos.numVideosSelected,
  }))
}

_Track an event on every action using the special '*' key_

const eventsMap = {
  '*': trackEvent(action => ({
    category: 'redux',
    action: action.type,
  })),
}

_Track multiple events on each VIDEO_PLAYED action using the combineEvents util_

const eventsMap = {
  'VIDEO_PLAYED': combineEvents(
    trackTiming(action => ({
      category: 'Videos',
      action: 'load',
      value: action.payload.loadTime,
    }))
    trackEvent(() => ({
      category: 'Videos',
      action: 'play'
    })),
  ),
}

_Track an event on each 'VIDEOSEARCHED' action, but throttle it with the debounceEvent util so it doesn't fire as often

const eventsMap = {
  'VIDEO_SEARCHED': debounceEvent(300,
     trackEvent(action => ({
       category: 'Videos',
       action: 'search',
       label: action.payload.searchTerm,
     }))
   ),
}

The trackPageView, trackEvent, and trackTiming functions used above are called eventDefinitions and are what you use to create events that are consumable by an analytics service (a.k.a "target"). Each target will have its own set of eventDefinitions that you can use and customize.

Don't like the idea of using an object to map actions?

You can use a function...

const pageView = trackPageView(action => ({
  page: action.payload.routerState.url,
}));

const videoLoadTime = trackTiming(action => ({
   category: 'Videos',
   action: 'load',
   value: action.payload.loadTime,
}));

const videoPlayed = trackEvent(() => ({
  category: 'Videos',
  action: 'play'
}));

const eventsMapper = (action) => {
  switch(action.type) {
    case 'ROUTE_CHANGED':
      return pageView;
    case 'VIDEO_PLAYED':
      return [videoLoadTime, videoPlayed]
    default:
      return [];
  }
}

More Examples & Recipes

Extensions & Plugins

Version Package
šŸ”Œ npm @redux-beacon/logger
šŸ”§ npm @redux-beacon/combine-events
šŸ”§ npm @redux-beacon/ensure
šŸ”§ npm @redux-beacon/debounce-event
šŸ”Œ npm @redux-beacon/offline-web
šŸ”Œ npm @redux-beacon/offline-react-native