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

perf(v2): more performant gtag and analytics plugin #2070

Merged
merged 5 commits into from
Dec 1, 2019

Conversation

endiliey
Copy link
Contributor

@endiliey endiliey commented Nov 30, 2019

Motivation

I merged #2062 (comment) to wrong branch. But anyway I added some more optimization here

Gtag
We use inject head html hooks, so that the script head is not appended after JS load. Removing this code

We should move this

window.dataLayer = window.dataLayer || [];
function gtag() {
// Have to use `arguments` instead of spreading as there are
// other properties attached to it e.g. callee.
// The GA library requires usage of `arguments.
window.dataLayer.push(arguments);
}
// Expose globally.
window.gtag = gtag;
gtag('js', new Date());
gtag('config', trackingID);
/* eslint-enable */
to head like what https://developers.google.com/analytics/devguides/collection/gtagjs/ recommend.

Doing it via client modules previously will put it in main bundle.

Google analytics
Instead of old https://developers.google.com/analytics/devguides/collection/analyticsjs/#the_google_analytics_tag

We use the async tag https://developers.google.com/analytics/devguides/collection/analyticsjs/#alternative_async_tag since our user is mostly not on IE9 or older browser

Summary

  • Also tried to leverage more on build time for checking instead of doing it client side
  • Try to be more spec compliant

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

Gtag
Production build
image

Also spec compliant with
https://developers.google.com/analytics/devguides/collection/gtagjs/
image

Google analytics

image

https://developers.google.com/analytics/devguides/collection/analyticsjs/#alternative_async_tag
image

@endiliey endiliey added the pr: performance This PR does not add a new behavior, but existing behaviors will be more memory- / time-efficient. label Nov 30, 2019
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Nov 30, 2019
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Nov 30, 2019

Deploy preview for docusaurus-2 ready!

Built with commit a76613b

https://deploy-preview-2070--docusaurus-2.netlify.com

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Nov 30, 2019

Deploy preview for docusaurus-preview ready!

Built with commit a76613b

https://deploy-preview-2070--docusaurus-preview.netlify.com

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.

Looks good, thanks for the optimization.

return {
headTags: [
{
tagName: 'link',
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we still need preconnect to https://www.google-analytics.com?

I see <script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script> in the deploy preview's HTML which seems to be added in by the loaded gtag script. Is that why you do preconnect to google-analytics.com here? If so, it would be good to add a comment as it isn't obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. because gtag automatically add google analytics script for you. So its better to preconnect

@endiliey endiliey merged commit 046a53e into master Dec 1, 2019
@endiliey endiliey deleted the endi/preconnect2 branch December 1, 2019 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: performance This PR does not add a new behavior, but existing behaviors will be more memory- / time-efficient.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants