Examples & Recipes
How to Track Pageviews in a React-Redux app
The following example shows how you can use Redux Beacon to track page views in an React app that uses Redux for state management, and React Router for navigation.
import { LOCATION_CHANGE } from 'react-router-redux';
import { createMiddleware } from 'redux-beacon';
import GoogleAnalytics, { trackPageView } from '@redux-beacon/google-analytics';
const eventsMap = {
[LOCATION_CHANGE]: trackPageView(action => ({
page: action.payload.pathname,
})),
};
const gaMiddleware = createMiddleware(eventsMap, GoogleAnalytics());Click here for instructions on how to apply the middleware to your store.
Click here for a runnable example.
How to Track Pageviews in an Angular-ngrx app
The following example shows how you can use Redux Beacon to track page views in an Angular app that uses ngrx/store for state management.
Click here for instructions on how to apply the meta reducer to your store.
Click here for a runnable example.
How to Send Analytics to Multiple Targets
There is no limit to the number of middlewares or meta reducers you can create and use. So, to send analytics to multiple targets, create a middleware or a meta reducer for each target:
Likewise, for createMetaReducer:
How to Create Your Own Target
Both createMiddleware and createMetaReducer require a target as their second parameter. A target is a function that Redux Beacon calls with an array of generated analytics events. What a target does with the array of generated events is up to the target's author, although the assumption is that the target will send those events to an external analytics service.
Most targets leverage an existing JavaScript sdk from an analytics service (e.g. Google Analytics), but some might post their own server requests.
If your target relies on an SDK attached to window, it is good practice to first check that window exists to avoid issues during server side rendering.
If you decide to publish your target, please let us know so we can link to it in our docs.
Last updated