Google Analytics not working in Gatsby blog

Google Analytics not working in Gatsby blog

I recently moved from Jekyll to Gatsby and I was setting up Google Analytics on the Gatsby blog as documented HERE. Even after countless cache refreshes and incognito tabs, I couldn't get it to work. There is already a closed issue HERE which talks about the same issue and luckily I found a solution from the same.

The Solution

Simply copy the gatsby-plugin-google-analytics plugin entry in the gatsby-config.js to the top of the plugins list and remove the options except the trackingId, head and anonymize.

Basically, my gatsby-config.js looks like this now:

plugins: [
        // Google analytics
            resolve: `gatsby-plugin-google-analytics`,
            options: {
              trackingId: "your tracking code here",
              head: true,
              anonymize: true,
    	// Other plugins

That should be it. Now, I have no clue why it works, I didn't have the time or knowledge to dive into the plugin's source code.  🤷‍♂️ Hope this helps