Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The google analytics plugin is not loaded with Gatsby in production #80

Closed
danielkcz opened this issue Aug 20, 2020 · 2 comments
Closed

Comments

@danielkcz
Copy link

I am not sure what might be wrong. It works in development, but in production running window.Analytics.getState() I see this. All tracking requests are in the queue and never transmitted over to GA.

image

You can see the site live at https://lovemyvoice.app/. It's not localized to English yet, but that shouldn't matter. You can see analytics.ts file in devtools (source maps are included).

gatsby@2.23.12
analytics@0.5.5
@analytics/google-analytics@0.4.1
gatsby-plugin-analytics@0.2.0

@DavidWells
Copy link
Owner

I don't see window.Analytics on the global window on https://lovemyvoice.app/

For the gatsby route plugin to work you need to set analytics to the window.

/* analytics.js */
import Analytics from 'analytics'
import segmentPlugin from 'analytics-plugin-segment'
import gtagManagerPlugin from 'analytics-plugin-google-tag-manager'
// ... whatever analytics provider you use

const analytics = Analytics({
  plugins: [
    gtagManagerPlugin({
      containerId: 'GTM-XYZ'
    }),
    segmentPlugin({
      writeKey: '123567',
    }),
  ]
})

// Set to global so analytics plugin will work with Gatsby
if (typeof window !== 'undefined') {
  window.Analytics = analytics
}

// Set to window.
window.Analytics = analytics

https://github.com/DavidWells/analytics/blob/master/packages/gatsby-plugin-analytics/gatsby-browser.js#L6

Alternatively, you can use a standalone routeChange utility https://github.com/DavidWells/analytics/blob/master/examples/react/src/index.js#L12 to avoid exposing to the global window

@danielkcz
Copy link
Author

Sorry, I needed to deploy some working solution there. Ended up with a combination of gatsby-plugin-google-analytics along with a direct Firebase Analytics module (still deciding which one to keep). Both of those are working correctly, but the analytics module wasn't for some reason.

I guess I will close it here now. Thanks for the reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants