benjaminhoffman / gatsby-plugin-segment-js

Gatsby plugin for segment.com's analytic.js snippet
https://www.npmjs.com/package/gatsby-plugin-segment-js
MIT License
40 stars 28 forks source link

Cannot read property 'track' of undefined #16

Open peduarte opened 5 years ago

peduarte commented 5 years ago

Hello,

I've followed the instructions on the Readme.

On development mode, I get Cannot read property 'track' of undefined.

Am I doing something wrong?

I know I can check if window.analytics exists, but the docs imply that this plugin was handling that?

benjaminhoffman commented 5 years ago

this is weird. do you have an ad blocker? which browser are you using. how does this work on production, or you only see the issue on dev? when you check for window.analytics in your chrome console, what happens?

please let me know the above, or if the issue is solved, please comment what the fix was. thanks!

daveyheuser commented 5 years ago

I had the same issue, and window.analytics returned undefined for me in the Chrome console. However, this was only on localhost, and it works fine on production.

benjaminhoffman commented 5 years ago

what about when you do yarn build ... i assume it works then?

and you've checked incognito browser, with no ad blockers, right?

peterphan1996 commented 5 years ago

I also had this issue, window.analytics returned undefined, I've tried yarn build but no luck

geoffgolder-ah commented 4 years ago

just in case someone comes across this, it means you probably don't have a DEV key defined. the prod key will only run in production, local host is dev environment.

alexfornuto commented 4 years ago

I hit the same issue, until I wrapped my window.analytics.track in a if (window.analytics) conditional.

It looks to me like the analytics.js script isn't being loaded into the page header. This is true even on production build. When looking at the chrome console (adblocker disabled), I have no window.analytics options available.

149203 commented 2 years ago

I had this problem because I copied the code snippet from the docs which contains a junk code snippet:

customSnippet: '!function(){var analytics=window.analytics||[];...;analytics.load("${writeKey}");analytics.page();}}();'

I had to remove that. 😦

Senthil-Sivanath commented 1 year ago

Without a valid env variable, analytics doesn't load; Set NODE_ENV to production or development

export NODE_ENV=production

or

export NODE_ENV=development