0

Anywhere that I check my website I get a leverage browser catching issue because of this file:

http://www.google-analytics.com/ga.js

In many questions and answers, articles people have offered to get the source of the script and serve it from my own server. Doing this is very easy but my concern is, If I do this, is there anything legal I should be concerned about?

Does google approve of doing this?

  • It's worth mentioning that despite it's short cache time, Google Analytics is so widely used that there's a decent chance visitors to your site will already have the file cached. Personally I'd just leave it as it is. – Tim Fountain Jun 30 '15 at 13:51
3

I think that the most important point is that creating a local copy of that file is totally counterproductive. The reason is very simple. The file comes in two versions (ga.js and the older analytics.js) and can be served over 2 protocols (HTTP and HTTPS). This means that the browser only ever needs to keep at most 4 cache entries, irrespective of the number of sites using Google Analytics that the user visits. These cache entries can be refreshed with HTTP requests that don't require to download the content (unless of course the content has changed).

Since so many sites use GA it is very unlikely that the browser needs to download the file when a user visits your site (because it either has an up to date version of the file or it just needs to send a small request to confirm that the file hasn't changed). In that case, using a local copy would actually increase the average latency of the first page view for your site.

| improve this answer | |
1

Approve? Well, they don't recommend it.

The major downside of this is updates. If they update, you don't get it because you downloaded.
This can result in broken of faulty statistics.

https://support.google.com/analytics/answer/1032389?hl=en

| improve this answer | |
  • Well I can solve the update problem, I can create a scheduled task to check for changes every hour and update if needed. what concerns me is that would it be a problem if I load it from my server? – Ashkan Mobayen Khiabani Jul 1 '15 at 8:32
  • But unless you set the cache headers on it such that it expires in an hour, then your users may be using an old version of it. Google sets the cache control of it appropriately already. Just use it as they provide it and don't worry about "leveraging browser caching". That is just a guideline. There are times when you should break the rules. Google has done so in this case because they know what they are doing. – Stephen Ostermiller Jul 1 '15 at 9:31
  • @StephenOstermiller I wouldnt worry about this "leverage caching" either, but scheduled fetches would still work fine. You dont need a header though, just make a cache buster by appending a timestamp like example.com/ga.js?t=1381257875. Everything is updated that way including other parse-to-caching methods that are agnostic to some asset headers, or proxy middlewares such as cloudflare. – dhaupin Jul 1 '15 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.