Getting Started (ngrx)

Last updated 7 months ago

In a nutshell: for each analytics event, pick out an event definition (listed in each target's docs) and map it to an action. The map should be an object with the action type as a key, and event definition as the value. Then, you simply provide your target and the events map to Redux Beacon's createMetaReducer function.

Step 1

Choose a target, and follow the setup instructions.

Don't see the target you need? Follow the instructions here to build your own.

Step 2

Decide what you want to track and pick out the corresponding event definition:

Step 3

Complete the event definition by filling in the object properties, and match it to an action.

For example, say you want to intercept a PLAY_VIDEO action and track it as a Google Analytics event:

import { createMetaReducer, EventsMap } from 'redux-beacon';
import GoogleAnalytics, { trackEvent } from '@redux-beacon/google-analytics';
import logger from '@redux-beacon/logger'; // optional
// Copy & paste the event definition you chose in step 2, then fill it in.
const emitVideoPlayed = trackEvent(action => ({
category: 'Videos',
action: action.type,
}));
// Match the event definition to a Redux action:
const eventsMap: EventsMap = {
'PLAY_VIDEO': emitVideoPlayed,
};
// Create the meta reducer
const ga = GoogleAnalytics();
const gaMetaReducer = createMetaReducer(eventsMap, ga, { logger });

Step 4

Follow the instructions here to apply the meta reducer to your store.

Next Steps

Track More Events:

Repeat steps 2 and 3 for every event you want to track. In a nutshell: 1. Find the event definition for the thing you want to track. 2. Match the event definition to a Redux action then complete the event definition.

Decide Where to put Analytics Logic:

As the number of events you track grows, it might make sense to move your event definitions and events map into their own files. Redux Beacon has no requirements on where these are kept. Have a discussion with your team on how you'd like to organize your app's analytics logic.

Check Out the Recipes:

We have a number of recipes to get you started with Redux Beacon. Check them out here.

Check Out the Extensions:

We have extensions for logging and for buffering analytics events offline. Check them out here.