-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
base: master
Are you sure you want to change the base?
Conversation
Do you have any benchmarks that moving to the head doesn't actually impact page load speed? 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. |
I just ran a test where I put the line
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. |
I did some testing of my own and didn't see much difference in load times having the GA script before Only change I'd like to see is to move it after the other scripts in the head so it's just before GA script before GA script before |
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. |
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? |
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. |
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.