DEV Community

Leo Kalshteyn
Leo Kalshteyn

Posted on

Google Analytics & React

Google Analytics

While reading more about various React libraries and modules I came across something that would prove useful for tracking and reporting website traffic, which is a web analytics service offered by Google. A react module exists that uses this service and can be incorporated into any React app.

React GA, or React Google Analytics Module, is a JavaScript module that can include google analytics tracking code for an app with a front end that uses React. While not containing React code, it is compatible with some Mozilla foundation sites that use React. There are many features and customizable options for React GA that you can see on the Readme on GitHub.

Installation

npm install react-ga --save
Enter fullscreen mode Exit fullscreen mode

Example of Use

import ReactGA from 'react-ga';
ReactGA.initialize('UA-000000-01');
ReactGA.pageview(window.location.pathname + window.location.search);
Enter fullscreen mode Exit fullscreen mode

There is also a plugin for the Gatsby framework which incorporates Google Analytics

Installation

npm install --save gatsby-plugin-google-analytics
Enter fullscreen mode Exit fullscreen mode

Example of Use

For use, you need to add config in your gatsby.config.js

 {
      resolve: 'gatsby-plugin-google-analytics',
      options: {
        trackingId: 'UA-111352358-2',
      },
    },
Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)