How to add Google analytics in Gatsby
Tags: gatsby, javascript
Published: 2021-02-03 10:38:55

---
title: How to add Google analytics in Gatsby
published: true
date: 2021-01-25 00:00:00 UTC
tags: gatsby, javascript
canonical_url: https://www.mariokandut.com/how-to-add-google-analytics-in-gatsby/
---

Adding Google Analytics in Gatsby is quite easy and can be done in just a few minutes. There are two possible ways to add Google Analytics.

What you need first is a Google Analytics account. Start with setting up an account [HERE](https://marketingplatform.google.com/about/analytics/).

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#1-get-your-tracking-id-from-google-analytics)1. Get your tracking ID from Google Analytics

After signing up for a Google Analytics account, create a property and get your tracking ID from the Tracking Code section under the Tracking Info menu. The tracking ID looks like this `UA-12341234-1`.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#2-add-website-tracking)2. Add website tracking

Now you have two options:

- Install the official Google analytics plugin [gatsby-plugin-google-gtag](https://gatsbyjs.com/plugins/gatsby-plugin-google-gtag/) OR
- Add the tracking code to customized `html.js`

In most cases the official plugin should be sufficient. Only if you have customized the html.js already, (script injection, etc.) you can add the tracking code there.

[💰: Start your cloud journey with $100 in free credits with DigitalOcean!](https://m.do.co/c/29f238135b61)

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#add-google-analytics-via-official-plugin)Add Google Analytics via official plugin

Install the plugin.

```
npm install gatsby-plugin-google-gtag
```

Add the plugin to your `gatsby-config.js` file:

```
module.exports = {
  plugins: [
    // All other plugins
    {
      resolve: `gatsby-plugin-google-gtag`,
      options: {
        // You can add multiple tracking ids and a pageview event will be fired for all of them.
        trackingIds: [
          'ADD-YOUR-TRACKING-CODE-HERE', // Google Analytics / GA
          // optional
          'OPTIONAL----AW-CONVERSION_ID', // Google Ads / Adwords / AW
          'OPTIONAL----DC-FLOODIGHT_ID', // Marketing Platform advertising products (Display & Video 360, Search Ads 360, and Campaign Manager)
        ],
        // This object gets passed directly to the gtag config command
        // This config will be shared across all trackingIds
        gtagConfig: {
          optimize_id: 'OPT_CONTAINER_ID',
          anonymize_ip: true,
          cookie_expires: 0,
        },
        // This object is used for configuration specific to this plugin
        pluginConfig: {
          // Puts tracking script in the head instead of the body
          head: false,
          // Setting this parameter is also optional
          respectDNT: true,
          // Avoids sending pageview hits from custom paths
          exclude: ['/preview/**', '/do-not-track/me/too/'],
        },
      },
    },
  ],
};
```

All configuration options can be found in the plugin [docs](https://gatsbyjs.com/plugins/gatsby-plugin-google-gtag/).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#add-google-analytics-in-htmljs)Add Google Analytics in html.js

If you have already a `html.js` file, skip the next paragraph.

Gatsby uses a React component to server render the `<head>` and other parts of the HTML outside of the core Gatsby application. Most sites should use the default html.js shipped with Gatsby and customizing html.js is not supported within a **Gatsby Theme**.

If you need to insert custom HTML into the `<head>` or `<footer>` of each page on your site, you can use html.js.

Copy the default html.js:

```
cp .cache/default-html.js src/html.js
```

Then add the website tracking code from Google Analytics:

```
<script
  async
  src="https://www.googletagmanager.com/gtag/js?id=UA-12341234-1"
/>
<script
  dangerouslySetInnerHTML={{
    __html: `
    window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-12341234-1');
  `,
  }}
/>
```

**IMPORTANT:** I recommend using an environment variable to store your `GA_TRACKING_ID`.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#3-build-and-test)3. Build and Test.

**The plugin only works in production mode.** Hence, to test the correct firing of events run: `gatsby build && gatsby serve`. After deploying your website confirm that website tracking is working in Google Analytics.

🎉🎉🎉 **Congratulations!** You have successfully added Google Analytics to your website. 🎉🎉🎉

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Gatsby](https://www.mariokandut.com/tags/gatsby)**, have a look at these [Gatsby Tutorials](https://www.mariokandut.com/tags/gatsby).

References (and Big thanks):[Gatsby Docs](https://gatsbyjs.com/plugins/gatsby-plugin-google-gtag/)