Gatsby: Cache Google Analytics in Gatsby using gatsby-plugin-google-analytics to leverage browser cache?

Created on 28 Sep 2018  路  7Comments  路  Source: gatsbyjs/gatsby

I want to leverage Browser Caching while using Gatsby Plugin Google Analytics.

I tried using https://blinkloader.com for my site https://akshaykadam.me but it gave me 1 issue LEVERAGE BROWSER CACHING

Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.

So I tried putting a cookieExpires field in gatsby-config.js like

{
    resolve: `gatsby-plugin-google-analytics`,
    options: {
        trackingId: GOOGLE_ANALYTICS_TRACKING_ID,
        // Puts tracking script in the head instead of the body
        head: false,
        // Setting this parameter is optional
        anonymize: true,
        // Setting this parameter is also optional
        respectDNT: true,
        // Avoids sending pageview hits from custom paths
        exclude: [],
        cookieExpires: 30
    }
}

But it didn't work as it still gives the same issue. How to do it like this ?

Hacktoberfest good first issue help wanted

Most helpful comment

I'd like to work on this

All 7 comments

@deadcoder0904 This option is currently not being used in gatsby-plugin-google-analytics. Would you like to work on this and raise a PR for it?

You'll need to make changes in the file below:
https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js

Here's google analytics documentation for cookieExpires for your reference.

Idk how to do that actually. All these stuff is a bouncer for me that's why I let Gatsby handle that 馃槀

I'd like to work on this

Sure go ahead. Thanks Miguel 馃檶

@deadcoder0904 This option is currently not being used in gatsby-plugin-google-analytics. Would you like to work on this and raise a PR for it?

You'll need to make changes in the file below:
https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-analytics/src/gatsby-ssr.js

Here's google analytics documentation for cookieExpires for your reference.

@kakadiadarpan, actually this has nothing to do with the expiresCookie option, which in fact works just fine as it is part of the knownOptions object defined at the top of the plugin. The problem the OP has is with the server side caching of the google-analytics code which is hosted on the google servers and does not provide server caching.

There are only tricks to handle this, like hosting the file locally or sniffing the user agent and not serving the file if it is the "Page Speed Insights" bot.

I believe this issue should be closed as it does not relate to Gatsby.

Also @deadcoder0904 keep in mind that the cookieExpires value represents seconds not days.

@gpetrioli Yep I thought so too but was not sure

Regarding its not related to Gatsby, can it be done through gatsby-plugin-google-analytics?

Or should I just add this using react-helmet?

@gpetrioli yes you are right, cookieExpires sets the expiration date for the cookies and not for the static resources caching.

I agree that we should close this issue as it does not relate to Gatsby.

Was this page helpful?
0 / 5 - 0 ratings