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

Allow Google Analytics to use gtag.js #601

Merged
merged 4 commits into from
Apr 29, 2018

Conversation

ramiel
Copy link
Contributor

@ramiel ramiel commented Apr 25, 2018

The code now uses the new gtag script to track the website on google analytics.
The migration is explained here.
This will help to solve issues such as #375.
As indicated at https://support.google.com/analytics/answer/1008080
the analytics code must be placed in the head tag, so it is moved there.

Motivation

I need to link my website to google search console so, I need the google analytics code to be setup properly

Have you read the Contributing Guidelines on pull requests?

No :)

Test Plan

The new code is on the now, instead of the body. There were no test for this before and I added none

Related PRs

None

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Apr 25, 2018
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@JoelMarcey
Copy link
Contributor

Have you read the Contributing Guidelines on pull requests? No :)

😢

But, you provided enough detail that we can look at this PR anyway 😄

I am trying to think if this will cause any backward compatibility problems - don't think so. I added @yangshun to see if he thinks there are any issues here.

Copy link
Contributor

@JoelMarcey JoelMarcey left a comment

Choose a reason for hiding this comment

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

Please rebase and run prettier if you can. That's why the tests are failing.

@yangshun
Copy link
Contributor

yangshun commented Apr 25, 2018

Moving it into <head> is ok since the <script> now has the async attribute. The script will start downloading earlier, which is great.

@ramiel the tests are failing, I think it's because the code has to be prettified first. Run yarn prettier.

Note that making this change will break users who are relying on analytics.js and the ga() method for pushing events as outlined in Google Analytics' Migration Docs. Existing analytics.js users will need to migrate from ga() to gtag().

IMO if we want to merge this, it is a breaking change and will require a major version bump. But I do think this upgrade will have to be done eventually. Alternatively, add an option into siteConfig that loads gtag.js instead of analytics.js. That way, we support both.

cc @JoelMarcey

@ramiel ramiel force-pushed the ganalytics-on-head branch 2 times, most recently from 0d20196 to 869d02f Compare April 26, 2018 07:00
@ramiel
Copy link
Contributor Author

ramiel commented Apr 26, 2018

So, I rebased and run prettier, now tests pass. For the moment I'd leave this PR as is until you decide what's better. If you say to have an option in siteConfig, I'll create it.
Another alternative could be to only move the original script into head, without migrating to gtag and to leave that change for later. Let me know

@JoelMarcey
Copy link
Contributor

@ramiel @yangshun I am ok with adding a siteConfig option for this. Let's go that route. We can call it gaGtag (consistent with our current gaTrackingId) and have it be boolean true for it to take hold. If you have a better name for the config option, that's good too. And we can put both options in Head.js.

Let's do it!

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

Let's go with Joel's suggestions 👍

@ramiel
Copy link
Contributor Author

ramiel commented Apr 28, 2018

Ok, I'll do these changes soon

The code now uses the new gtag script to track the website on google analytics.
The migration is explained [here](https://developers.google.com/analytics/devguides/collection/gtagjs/migration).
This will help to solve issues such as facebook#375.
As indicated at https://support.google.com/analytics/answer/1008080
the analytics code must be placed in the head tag, so it is moved there.
Copy link
Contributor

@JoelMarcey JoelMarcey left a comment

Choose a reason for hiding this comment

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

Nice. I think you can simplify the code a little bit with just one gaTrackingId && gaGtag check instead of two. What do you think?

)}
{this.props.config.gaTrackingId &&
this.props.config.gaGtag && (
<script
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't use combine the the two <script> into one gaTrackingID && gaGtag check, instead of having two of the same check?

Copy link
Contributor

Choose a reason for hiding this comment

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

Basically

        {this.props.config.gaTrackingId &&
          this.props.config.gaGtag && (
            <script
              async
              src={`https://www.googletagmanager.com/gtag/js?id=${
                this.props.config.gaTrackingId
              }`}
            />
            <script
              dangerouslySetInnerHTML={{
                __html: `
              window.dataLayer = window.dataLayer || [];
              function gtag(){dataLayer.push(arguments); }
              gtag('js', new Date());
              gtag('config', '${this.props.config.gaTrackingId}');
            `,
              }}
            />
          )}

Copy link
Contributor

Choose a reason for hiding this comment

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

Does that not work?

@ramiel
Copy link
Contributor Author

ramiel commented Apr 29, 2018

In react 15 you cannot return two children without a common parent. This is why I did like that. But I can check again,maybe I'm wrong

@yangshun yangshun changed the title Moved google analytics script to head Allow Google Analytics to use gtag.js Apr 29, 2018
@yangshun yangshun merged commit 976ae77 into facebook:master Apr 29, 2018
@j-f1
Copy link

j-f1 commented May 5, 2018

Any idea when this will make it to a release?

@JoelMarcey
Copy link
Contributor

@j-f1 I am going to try to get out a release this weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants