Search code examples
google-analyticsservice-workerprogressive-web-appssw-toolbox

Handling Google analytics in an offline enabled web app


I am a developer of Codiva - java ide and online compiler. I am working on improving offline support, reducing network usage, reducing the latency by pre-caching as much as possible.

I want to know how to handle requests to google analytics.

  1. First is the ga script. I use google tag manager to setup GA. Is it okay to cache that request, that is, can I use networkFirst strategy for this request? Or should it always be networkOnly?

  2. How to make sure the actions that happened offline gets tracked correctly?

  3. I am planning to start using Firebase for some featuers, firebase also has some kind of analytics. Would it automatically handle analytics when the device goes offline?


Solution

  • First is the ga script. I use google tag manager to setup GA. Is it okay to cache that request, that is, can I use networkFirst strategy for this request? Or should it always be networkOnly?

    I'm not sure it's wise to cache the GTM script. The analytics.js script is relatively static, but the GTM script can be updated by anyone who has access to your GTM account. Changes made in there obviously wouldn't get propagated to users of the cached version of the script.

    How to make sure the actions that happened offline gets tracked correctly?

    The key is to use the qt parameter, which allows you to send a hit after the fact, and specify its time offset.

    There's an unofficial service worker script that does this today that you should take a look at. It will probably become officially supported sometime soon: https://gist.github.com/jeffposnick/466ef7578c4c880a78c7270e6ac69620

    I am planning to start using Firebase for some featuers, firebase also has some kind of analytics. Would it automatically handle analytics when the device goes offline?

    At this point Firebase analytics is mobile-only. If you're using their web SDK, I don't think you get any analytics at this point.