Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add with-google-analytics example #4036

Merged
merged 2 commits into from
Mar 29, 2018

Conversation

prichodko
Copy link
Contributor

Adds an example showing how to setup global site tag - gtag.js to send event data to Google Analytics.

gtag.js replaces older analytics.js solution and is currently recommended by Google - https://developers.google.com/analytics/devguides/collection/gtagjs/migration

Related: #160

@Vadorequest
Copy link
Contributor

@prichodko Beware to apply this:

  GA.set({page: pageName})
  GA.pageview(pageName)

I've read somewhere that doing a simple pageview isn't good, since it put the GA in some kind of broken state because it doesn't know that the page has changed if you don't use set. (and messes up other calls by providing the wrong page)

@prichodko
Copy link
Contributor Author

prichodko commented Mar 21, 2018

@Vadorequest thanks for checking out and yes, this is true for old analytics.js - docs, but is different with the new gtag.js - docs.

@mattiascibien
Copy link

@prichodko I think you should format the code using standard for the travis-ci build to pass and the therefore the code-coverage.

@prichodko
Copy link
Contributor Author

@mattiascibien thanks! Haven't noticed it. :)

PR updated.

@mattiascibien
Copy link

@prichodko in any case I have included the code you provided on my site and it worked like a charm (even with static export). I will do something similar for azure appinsights I guess but I am still experimenting.

@prichodko prichodko force-pushed the feat/with-google-analytics branch 2 times, most recently from e489aef to f7cb3c7 Compare March 26, 2018 13:53
@prichodko
Copy link
Contributor Author

@timneutkens rebased :)


export default class extends Document {
static getInitialProps ({ renderPage }) {
return renderPage()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { html, head, errorHtml, chunks } = renderPage()
return { html, head, errorHtml, chunks, styles }

lets do it like in the readme

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Done ✅.

@prichodko
Copy link
Contributor Author

@timneutkens rebased

@timneutkens
Copy link
Member

Nice!

@kachkaev
Copy link
Contributor

kachkaev commented Apr 12, 2018

Thanks for this example @prichodko! It'd be great if GA_TRACKING_ID was coming from process.env – that way it's easier to configure dev vs production.

This is roughly how I approached this a few months back: https://gitlab.com/kachkaev/website-frontend/commit/c36a04d7674ca572f90374bbf83c02d1bde11300

@prichodko
Copy link
Contributor Author

@kachkaev you are definitely right! I tried to keep example as simple as possible. However for the env variables I would currently use a new feature released in 5.1 - https://zeit.co/blog/next5-1#environment-configuration. What do you think?

@lock lock bot locked as resolved and limited conversation to collaborators Apr 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants