next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Adding GA script tag?

Open robinvdvleuten opened this issue 10 years ago β€’ 76 comments

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

robinvdvleuten avatar Oct 30 '16 13:10 robinvdvleuten

checkout <Head>

nodegin avatar Oct 30 '16 15:10 nodegin

@robinvdvleuten I'd suggest creating a GoogleTagManager component and including it in the Head or right after it depending on your implementation choice.

dariye avatar Oct 31 '16 15:10 dariye

as people mentioned you can use the Head tag or a react-friendly solution like https://github.com/react-ga/react-ga

impronunciable avatar Nov 02 '16 12:11 impronunciable

Any tips on getting react-ga working with next.js?

gtramontina avatar Nov 21 '16 12:11 gtramontina

@gtramontina it should work. Otherwise I'm one of the react-ga contributors so I could help with that

impronunciable avatar Nov 22 '16 13:11 impronunciable

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>`}} />

image

musemind avatar Dec 13 '16 19:12 musemind

I would recommend a better solution to track client side page view: autotrack. See https://github.com/MoOx/phenomic/issues/384 for more information.

MoOx avatar Dec 13 '16 20:12 MoOx

@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 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...

sedubois avatar Jan 17 '17 10:01 sedubois

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 .

impronunciable avatar Jan 17 '17 11:01 impronunciable

Pretty sure autotrack is more than enough, only injected on the client side - no need to think about the server-side.

MoOx avatar Jan 17 '17 11:01 MoOx

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.

MoOx avatar Jan 17 '17 11:01 MoOx

Yeah we don't want to track server renders, Since it Will obfuscate things like location.

timneutkens avatar Jan 17 '17 11:01 timneutkens

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 :)

MoOx avatar Jan 17 '17 11:01 MoOx

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 😬 πŸ˜„

sedubois avatar Jan 17 '17 13:01 sedubois

You can use "real time" view to debug GA easily.

MoOx avatar Jan 17 '17 14:01 MoOx

@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)

sedubois avatar Jan 17 '17 14:01 sedubois

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 πŸ™‚

sedubois avatar Jan 18 '17 11:01 sedubois

Ah, got it working by switching to react-ga πŸ˜„

https://github.com/relatenow/relate/commit/a9d2bce46e5314075af7c12bbaa0266865ff25da

screen shot 2017-01-18 at 14 01 28

sedubois avatar Jan 18 '17 12:01 sedubois

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 avatar Mar 07 '17 12:03 luandro

@luandro don't remember the details, but you can look at the commit I linked above if it helps

sedubois avatar Mar 07 '17 13:03 sedubois