Google PageSpeed Insights vs. Google Analytics

Validation tools such as the W3C Markup Validation Service or Google PageSpeed Insights are helpful to objectively evaluate the code and performance of a website. Especially Google influences a whole industry with concepts like above the fold - and also gives strict but useful guidelines in terms of image compression and caching. But it can happen that Google steps on its own feet.


Will the JavaScript tracking code

  (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','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-12350231-1', 'auto');
  ga('send', 'pageview');

Integrated by Google Analytics on the website as officially described , Google complains about a lack of browser caching:

Google PageSpeed Insights vs. Google Analytics

Unfortunately, technically this can only be corrected by the host of the file, in this case Google itself, and it is only possible to fix it with the following workaround: First you bend the URL

(window,document,'script','//www.google-analytics.com/analytics.js','ga');

to the own server to

(window,document,'script','analytics.js','ga');

and sets up a simple cron job via PHP, which downloads the latest .js file freshly to its own server every day:

file_put_contents("analytics.js", fopen("https://www.google-analytics.com/analytics.js", 'r'));

Then you can fine-tune the caching so that PageSpeed ​​no longer criticizes the error:

Google PageSpeed Insights vs. Google Analytics

Back