Adding GA script tag?
Hi there!
I am currently converting my website to a next.js project and can't figure out on how to add the Google Analytics script tag to the page. Any thoughts?
Cheers, Robin
checkout <Head>
@robinvdvleuten I'd suggest creating a GoogleTagManager component and including it in the Head or right after it depending on your implementation choice.
as people mentioned you can use the Head tag or a react-friendly solution like https://github.com/react-ga/react-ga
Any tips on getting react-ga working with next.js?
@gtramontina it should work. Otherwise I'm one of the react-ga contributors so I could help with that
No need for react-ga. Use the google tag manager with the <Head> component to inject analytics. There are some small changes to the google tag manager code necessary to make it work in nextjs/react:
<Head>
<script dangerouslySetInnerHTML={{__html: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');`}} />
</Head>
<noscript dangerouslySetInnerHTML={{__html: `<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX" height="0" width="0" style="display:none;visibility:hidden;"></iframe>`}} />

I would recommend a better solution to track client side page view: autotrack. See https://github.com/MoOx/phenomic/issues/384 for more information.
@impronunciable it would be great to have an example as after some googling I'm still confused π Could this be reopened?
- @MoOx seems to imply that react-ga isn't necessary, should use Google AutoTrack instead
- but Autotrack instructs to create a script tag with code which accesses the
windowobject, so that doesn't work server-side - and anyway if using a react component such as react-ga, where should it be put? In a
render?componentWillMount?getInitialProps? It's not super clear as it kind of supposes that you use react-router and load the code a single time for all pages.
The page tracking should work both when the page is server-rendered and then when navigating to another page. It would be would be great to see how the whole thing should integrate in a typical Next app with a Head, a page layout HOC, etc.
Would also be great to know if you think there's a better/simpler/less cumbersome/more state-of-the-art combination than Next.js + Google Analytics Autotrack...
I can investigate this
On Tue, Jan 17, 2017, 7:59 AM SΓ©bastien Dubois [email protected] wrote:
@impronunciable https://github.com/impronunciable it would be great to have an example as after some googling I'm still confused π Could this be reopened?
- @MoOx https://github.com/MoOx seems to imply that react-ga isn't necessary, should use Google AutoTrack https://github.com/googleanalytics/autotrack instead
- but Autotrack instructs to create a script tag with code which accesses the window object, so that doesn't work server-side
- and anyway if using a react component such as react-ga, where should it be put? In a render? componentWillMount? getInitialProps? It's not super clear as it kind of supposes that you use react-router and load the code a single time for all pages.
The page tracking should work both when the page is server-rendered and then when navigating to another page. It would be would be great to see how the whole thing should integrate in a typical Next app with a Head, a page layout HOC, etc.
Would also be great to know if you think there's a better/simpler/less cumbersome/more state-of-the-art combination than Next.js + Google Analytics Autotrack...
β You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/zeit/next.js/issues/160#issuecomment-273108099, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKHp9-2bfLXj2hMGlNlkqUSRqEL7R2Cks5rTJ8kgaJpZM4KkXxa .
Pretty sure autotrack is more than enough, only injected on the client side - no need to think about the server-side.
See https://github.com/MoOx/react-toulouse/commit/c42045d1001ab813c5d7eae5ab2f4f8c08c0025e for an example. It's not using next, but I guess it should be easy to adapt.
Yeah we don't want to track server renders, Since it Will obfuscate things like location.
No you don't since it will be handled by the client: keep in mind that a page is request by the client and will be rendered in the client browser :)
OK I didn't realise the code shouln't run server-side. So I used autotrack as recommended by @MoOx, I think this should work: https://github.com/relatenow/relate/commit/50dc3f317eb3efa045d5bbe40d1a1a1ad1116458
Just haven't confirmed yet if it counts page views correctly, this is new to me π¬ π
You can use "real time" view to debug GA easily.
@MoOx yes well that's what bothers me, I don't see anything in the realtime panel when I visit https://relate.now.sh in parallel in another tab (0 active users)
I'm still not getting this... I don't get any data with the code I wrote.
All the examples I see seem to require a <script href="https://www.google-analytics.com/analytics.js" async /> but things in the Head don't seem to be executed in the client? (my console.log aren't printed in browser console...)
An example would still be very appreciated π
Ah, got it working by switching to react-ga π
https://github.com/relatenow/relate/commit/a9d2bce46e5314075af7c12bbaa0266865ff25da

How did you get it to work? I've been trying to call a pageview function on componentDidMount.
The production app GA Check fails.
@luandro don't remember the details, but you can look at the commit I linked above if it helps