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

Moved Google Analytics script to head #294

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

akindofyoga
Copy link
Contributor

@akindofyoga akindofyoga commented May 28, 2018

Google recommends putting the tracking script for Google Analytics "near the top of the <head> tag and before any other script or CSS tags" on this page.

I know they used to recommend putting the script at the bottom of the page to avoid slowing down the page's load time. However, it doesn't seem like this is a concern anymore.

I therefore modified the templates to insert the Google Analytics tracking code before other scripts or stylesheets.

I also removed a file called "google-analytics.html" that did not seem to be used for anything.

@mmistakes
Copy link
Owner

Do you have any benchmarks that moving to the head doesn't actually impact page load speed?
I know Google likes it up top so it loads before anything else, in the case someone starts clicking around before the page has fully loaded.

But in my experience that's unnecessary.

I believe their script loads async so it shouldn't block the page load, but I still feel like there is a performance hit having it up in the head.

@akindofyoga
Copy link
Contributor Author

akindofyoga commented Jun 5, 2018

I just ran a test where I put the line let start = Date.now(); right before the if statements at the start of the Google Analytics code. I then put the following lines after directly after the Google Analytics code:

let elapsed = Date.now() - start;
console.log(elapsed);

The elapsed time I observed was only two milliseconds.

I also found this Stack Overflow answer about the placement of the Google Analytics script.

I agree the benefits to moving this are relatively small. But I don't think there is any benefit to leaving the script at the bottom of the page. There doesn't seem to be a performance cost having it up in the head.

@mmistakes
Copy link
Owner

I did some testing of my own and didn't see much difference in load times having the GA script before </body> vs. moving it up before </head>. So I'm cool with this PR.

Only change I'd like to see is to move it after the other scripts in the head so it's just before </head> as suggested in the SO thread. Just in case it's not loaded async I'd like priority on the CSS, fonts, and other scripts so they always load before some tracking script. To me content is more important than monitoring visitors.

GA script before </body>

footer

GA script before </head>

head

@akindofyoga
Copy link
Contributor Author

akindofyoga commented Jun 16, 2018

The answer on that thread suggesting placing the snippet before the tag was quoting Google's recommendation from 2014. However, Google's current recommendation is to put the snippet right after the tag.

I just updated the code to use the snippet that Google currently recommends. This snippet uses JavaScript's async keyword, so there isn't any risk of the script not loading asynchronously.

I understand wanting to prioritize content over tracking in theory. However, in practice, I don't feel this hurts anything. Even if putting the analytics code at the start of the head added an additional 10 milliseconds of delay before the css and fonts started loading, no one using a site would notice this. However, putting the snippet where Google recommends it will allow for real-time reporting.

I agree that this is a small benefit. But this seems like getting small benefit compared to getting no benefit to me.

@akindofyoga
Copy link
Contributor Author

Just wanted to check in on this since it's been a while since this pull request has been responded to. Would you be willing to have the Google Analytics code in the header?

@skulumani
Copy link

As an additional consideration, I do not think the currently implemented default works anymore. I had to modify my version with a custom scripts.html with the updated tracking code from Google.

Using the updated script and moving to the head seems to be the recommended solution by Google.

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

Successfully merging this pull request may close these issues.

None yet

3 participants