PageSpeed Insights is a tool from Google that evaluates the loading speed and behaviour of a page on mobile and desktop devices and, depending on the results, provides concrete suggestions for improvement.
It's Simple to UseVisit the Google PageSpeed Insights page. Input your web page URL. Click Analyze.
PageSpeed Service is an online service to automatically speed up loading of your web pages. PageSpeed Service fetches content from your servers, rewrites your pages by applying web performance best practices and serves them to end users via Google's servers across the globe.
Well, if Google is cheating on you, you can cheat Google back:
This is the user-agent for pageSpeed:
“Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.8 (KHTML, like Gecko; Google Page Speed Insights) Chrome/19.0.1084.36 Safari/536.8”
You can insert a conditional to avoid serving the analytics script to PageSpeed:
<?php if (!isset($_SERVER['HTTP_USER_AGENT']) || stripos($_SERVER['HTTP_USER_AGENT'], 'Speed Insights') === false): ?>
// your analytics code here
<?php endif; ?>
Obviously, it won't make any real improvement, but if your only concern is getting a 100/100 score this will do it.
There's a subset of Google Analytics js library called ga-lite that you can cache however you want.
The library uses Google Analytics' public REST API to send the user tracking data to Google. You can read more from the blog post about ga-lite.
Disclaimer: I am the author of this library. I struggled with this specific problem and the best result I found was to implement this solution.
Here is a really simple solution using JS, for basic GA tracking, which will also work for edge caches/proxies (this was converted from a comment):
if(navigator.userAgent.indexOf("Speed Insights") == -1) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXXX-X', 'auto');
ga('send', 'pageview');
}
Note: This is the default GA script. You may have other ga()
calls, and if so, you would need to always check the user agent before calling ga()
, otherwise it may error out.
I wouldn't worry about it. Don't put it on your own server, it sounds like this is an issue with Google, but as good as it gets. Putting the file on your own server will create many new problems.
They probably need the file to get called every time rather than getting it from the client's cache, since that way you wouldn't count the visits.
If you have a problem to feel fine with that, run the Google insights URL on Google insights itself, have a laugh, relax and get on with your work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With