36

Using Google Analytics with React

 4 years ago
source link: https://www.tuicool.com/articles/2y6N3ef
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Using Google Analytics with React

How to set up analytics tracking in React.js with Google Analytics.

Er6rEbe.png!webZjIZv2A.png!web

Overview

Google Analytics provides a great dashboard with plenty of insights and reports on customer use in our applications. This information can be critical for monitoring performance, measuring growth, geolocation of users, and even user acquisition insights! It is simply one of the best, free to use website analytics tools out there. It just so happens that it is also tremendously easy to integrate with React.

7jU3Inj.png!webNz2EZ3m.png!web
MrayYbf.png!webryErayv.png!web
6JniMvb.png!webBfErem6.png!web
RnERfqY.png!webJVbYRnz.png!web
Real analytics data from one of my personal projects https://tournio.io.

Installation and Setup

To begin integrating Google Analytics with your react project, you’ll need to first install the react-ga package from npm.

npm i react-ga -S

The react-ga package is the official JavaScript module to connect Google Analytics with React.

Next, you simply need to import and initialize the module in your application.

f2ii63u.jpg!web

In the above example, you will need to replace trackingId with your actual Google Analytics tracking id. To find your tracking id:

  • Sign in to https://analytics.google.com
  • Click Admin.
  • Select an account from the menu in the ACCOUNT column.
  • Select a property from the menu in the PROPERTY column.
  • Under PROPERTY , click Tracking Info then Tracking Code .
3iYVV3J.png!webnAbuYf3.png!web

Usage with Custom Events

It can be a very beneficial tool to be able to monitor how users are interacting with your application. For example, if you had a “Sign Up Now” button, you could measure how many times the button was clicked vs how many users actually signed up. In order to implement this, we will use the event feature.

f2ii63u.jpg!web

The event would then be reported to Google and available for analytics within a few seconds.

Pageview Tracking

In my personal implementations, I will also use react-router which allows you to easily create a React app with full routing functionality. You can easily tie into page change events to track page views by also using the history package.

First, install the packages.

npm i history react-router-dom -S

Next, we simply need to connect our browser history instance to the router and our Google Analytics tracking.

f2ii63u.jpg!web

That’s all there is to it! There are many more features that you can look into implementing with both react-ga and react-router . If you are interested in learning more about both of these packages, go ahead and check them out!

Thanks for reading and good luck with your projects! Leave a comment or message me if you have any questions.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK