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

Add `onReady` option to delayed loader #23

Closed shortcircuit3 closed 4 years ago

shortcircuit3 commented 4 years ago

I need this to add google analytics auto linking. I dont think this is the best implementation, but it's the only way I could figure out how. Please make suggestions and I can fix @Kilian @benjaminhoffman

Fixes #22

Kilian commented 4 years ago

Looking through the changes you've made here,I think this is the wrong there is a better approach and we already provide a way to do what you want.

What you want to do is add your own gatsby-browser.js file, add the code from this page here: https://github.com/benjaminhoffman/gatsby-plugin-segment-js/blob/master/src/gatsby-browser.js

Then in the trackSegmentPage function you would add your window.analytics.ready() code. Once the code in trackSegmentPage is ran, you're guaranteed to have the window.analytics code loaded. If you need to run it only once, you can add a boolean for that that you flip after the code has run. Does that make sense?

shortcircuit3 commented 4 years ago

Thank you for the reply! This approach worked for me :). Maybe a PR with an explanation of adding code would be helpful for noobies like me.