Skip to main content

Google Analytics In React, Next.js and Gatsby.js

Google Analytics is a free web analytics service that provides insights into your website or app's traffic and performance. By adding Google Analytics to your React, Next.js, or Gatsby.js application, you can track key metrics such as page views, bounce rate, and average session duration. This data can help you to understand how your users are interacting with your application and make informed decisions about how to improve it.

In this blog post, we'll show you how to add Google Analytics to your React, Next.js, and Gatsby.js applications. We'll also provide some code examples to help you get started.


Adding Google Analytics to React

To add Google Analytics to your React application, you'll need to install the react-ga package . You can do this by running the following command in your terminal:


npm install react-ga

Once you've installed the package, you'll need to import it into your React application. You can do this by adding the following line to the top of your index.js file:


import ReactGA from 'react-ga';

Next, you'll need to create a new Google Analytics property. You can do this by going to the Google Analytics website and clicking on the "Create Property" button.


Once you've created a property, you'll need to add the tracking ID to your React application. You can do this by setting the trackingId prop on the ReactGA component. For example:


<ReactGA trackingId="YOUR_TRACKING_ID" />

Finally, you'll need to add the Google Analytics snippet to your React application. You can do this by adding the following line to the <head> element of your index.html file:


<script src="https://www.google-analytics.com/analytics.js"></script>


Adding Google Analytics to Next.js

To add Google Analytics to your Next.js application, you'll need to install the next-analytics package. You can do this by running the following command in your terminal:


npm install next-analytics

Once you've installed the package, you'll need to import it into your Next.js application. You can do this by adding the following line to the top of your _app.js file:


import { GA_TRACKING_ID } from '../lib/gtag';

Next, you'll need to create a new Google Analytics property. You can do this by going to the Google Analytics website and clicking on the "Create Property" button.


Once you've created a property, you'll need to add the tracking ID to your Next.js application. You can do this by setting the GA_TRACKING_ID environment variable in your .env.local file. For example:


GA_TRACKING_ID=YOUR_TRACKING_ID

Finally, you'll need to add the Google Analytics snippet to your Next.js application. You can do this by adding the following line to the <head> element of your _document.js file:


<script src="https://www.google-analytics.com/analytics.js"></script>


Adding Google Analytics to Gatsby.js

To add Google Analytics to your Gatsby.js application, you'll need to install the gatsby-plugin-google-analytics package. You can do this by running the following command in your terminal:


npm install gatsby-plugin-google-analytics

Once you've installed the package, you'll need to add it to your Gatsby.js application's gatsby-config.js file. You can do this by adding the following line to the plugins array:


plugins: [

  {

    resolve: `gatsby-plugin-google-analytics`,

    options: {

      trackingId: 'YOUR_TRACKING_ID',

    },

  },

],

Finally, you'll need to add the Google Analytics snippet to your Gatsby.js application. You can do this by adding the following line to the <head> element of your index.html file:


<script src="https://www.google-analytics.com/analytics.js"></script>


Conclusion

Adding Google Analytics to your React, Next.js, or Gatsby.js application is a relatively simple process. By following the steps outlined in this blog post, you can easily add Google Analytics to your application and start tracking your website or app's performance.


Here are some additional resources that you may find helpful:


Google Analytics documentation

React GA package documentation

Next Analytics package documentation

Gatsby plugin for Google Analytics documentation

Comments

Topics

Show more