Redux Beacon
  • Introduction
  • Getting Started (Redux)
  • Getting Started (ngrx)
  • Examples & Recipes
  • Targets
    • GoogleAnalytics
    • GoogleAnalyticsGtag
    • GoogleTagManager
    • Segment
    • Amplitude
    • React Native: GoogleAnalytics
    • React Native: GoogleTagManager
    • React Native: Segment
  • API Reference
    • createMiddleware
    • createMetaReducer
    • EventDefinition
    • EventsMap
    • EventsMapper
  • Extensions
    • logger
    • OfflineWeb
    • OfflineReactNative
  • Utils
    • debounceEvent
    • ensure
    • combineEvents
  • FAQ
  • Migration Guide (v1 to v2)
  • Notes on Offline Events
Powered by GitBook
On this page
  • Setup
  • Usage
  • Event Definitions
  • pageView
  • event
  1. Targets

GoogleAnalyticsGtag

PreviousGoogleAnalyticsNextGoogleTagManager

Last updated 4 years ago

Setup

  1. Sign up for Google Analytics and .

  2. Add the to your site.

  3. Install the target:

    npm install --save @redux-beacon/google-analytics-gtag

Usage

import GoogleAnalyticsGtag from '@redux-beacon/google-analytics-gtag';

// Create or import an events map.
// See "getting started" pages for instructions.

const trackingId = 'REPLACE_WITH_YOUR_TRACKING_ID';
const ga = GoogleAnalyticsGtag(trackingId);

const gaMiddleware = createMiddleware(eventsMap, ga);
const gaMetaReducer = createMetaReducer(eventsMap, ga);

Event Definitions

pageView

Docs:

import { trackPageView } from '@redux-beacon/google-analytics-gtag';

const pageView = trackPageView((action, prevState, nextState) => {
 return {
   title: /* (optional) */,
   location: /* (optional) */,
   path: /* (optional) */,
   fieldsObject: { /* (optional) */
     [ /* dimension | metric */]: /* value */,
   },
 };
}, /* (optional) tracking Id */, /* (optional) tracking Id */, ...);

The last line of the tracking snippet gtag('config', 'GA_TRACKING_ID'); hits Google Analytics with a page view that matches the first loaded route. If you're tracking page views using Redux Beacon, be sure to remove this line so the initial page load isn't recorded twice.

event

Docs:

import { trackEvent } from '@redux-beacon/google-analytics-gtag';

const event = trackEvent((action, prevState, nextState) => {
  return {
    category: /* fill me in */,
    action: /* fill me in */,
    label: /* (optional) */,
    value: /* (optional) */,
    fieldsObject: { /* (optional) */
      [ /* dimension | metric */]: /* value */,
    },
  };
});

Don't see your event listed? Please submit a pull request to the with the missing event. Use the source of the existing event-helpers to guide your work. If you need any support feel free to make the pull request with all you're able to do. We can fill in the gaps from there.

Redux Beacon repository
https://developers.google.com/analytics/devguides/collection/gtagjs/pages
https://developers.google.com/analytics/devguides/collection/gtagjs/events
create a new web property
gtag.js tracking snippet
Setup
Usage
Event Definitions
pageView
event