1

I have ran a site through Google Page speed insights and one of its recommendations is to leverage browser caching for https://www.google-analytics.com/analytics.js.

Is this possible on a Drupal site?

3
  • 2
    Only if you 1. Get Google to change their cache expiry or 2. Serve a copy of the analytics yourself from your domain, and set appropriate headers. Probably not a good idea as you won't be keeping up with changes to google's code.
    – Clive
    Commented Apr 2, 2015 at 10:43
  • Ok Thanks for the response , if you make the comment an answer I will mark it as the right answer Commented Apr 2, 2015 at 10:55
  • @Clive May you add your comment as answer? Do you think the question is off-topic for us?
    – avpaderno
    Commented May 17, 2016 at 6:53

2 Answers 2

1

As per one of the comments below the question, this is possible only if you:

  1. Get Google to change their cache expiry or
  2. Serve a copy of the analytics yourself from your domain, and set appropriate headers. Probably not a good idea as you won't be keeping up with changes to Google's code.

Credits: Clive.

1

If you have a .htaccess file, you can add the following code at the top of it:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
5
  • Google host this code themselves, a request will never hit your server so this won't do anything
    – Clive
    Commented Apr 1, 2016 at 8:27
  • is this only a drupal thing?I have tried this on joomla and wordpress sites and at least pagespeed sites like gtmetrix accept this and give me a leverage browser caching of 100%
    – Argiris A
    Commented Apr 1, 2016 at 8:33
  • Nope not just a Drupal thing - the file is hosted by Google, so only Google can change the cache headers. Sorry to shout, but it has been mentioned a few times already in this question :)
    – Clive
    Commented Apr 1, 2016 at 8:36
  • Yes, I got what you said in the first place, I just thought that this works, because I tested it on other sites and it worked, not only getting a higher rank at pagespeed sites, but actually loading the site faster without making any other change
    – Argiris A
    Commented Apr 1, 2016 at 8:43
  • 1
    Sorry to be the bearer of bad news, but it didn't work, not for the file we're talking about here - the only way to solve this issue for an external script is to make the script not external any more. That means serving it from your own server, which incidentally Google do not recommend. See stackoverflow.com/questions/29162881/… for the canonical network question about this
    – Clive
    Commented Apr 1, 2016 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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