Hello guys,
I am using flowrouter, how can I add google analytics in a flowrouter trigger directly?
I mean, I want to avoid an additional plugin.
Hello guys,
I am using flowrouter, how can I add google analytics in a flowrouter trigger directly?
I mean, I want to avoid an additional plugin.
Hi there,
I never used the flowrouter but let me offer you a react-router version and hopefully you can extrapolate from this concept.
This is based on a dynamic load of react-ga NPM after your Meteor app is fully loaded.
Wait for everything else to be loaded and then let this kick in.
handleGoogleAnalytics () {
if (!window.GoogleAnalyticsObject) { // are the analytics already loaded?
const { history } = this.props // this react prop comes from my react-router.
import('react-ga')
.then(ReactGA => {
ReactGA.initialize('UA-xxxxxx-1')
ReactGA.set({ page: window.location.pathname })
ReactGA.pageview(window.location.pathname)
history.listen((location, action) => { // here I listen on changes an I feed the update to Google. I suppose your router would have a listener, otherwise you can write one for the "window" context.
ReactGA.set({ page: location.pathname })
ReactGA.pageview(location.pathname)
})
})
}
}