4

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?

JackDaniels
  • 985
  • 9
  • 26

2 Answers2

3

Use the Service Worker helper for Google Analytics:

https://developers.google.com/web/updates/2016/07/offline-google-analytics?hl=en

Try PWA Template https://github.com/StartPolymer/progressive-web-app-template

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Josef Ježek
  • 2,208
  • 2
  • 17
  • 10
2

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.

Philip Walton
  • 29,693
  • 16
  • 60
  • 84
  • Can you provide some information on how to use the offline-analytics.js script? I don't see any documentation. But looking at the code, I guess, I have to import the script in my service either coffee, then everything else will be magic. Is that right? – JackDaniels Jun 22 '16 at 18:38
  • Yeah, you can import it with`importScripts()`, which is a service worker thing. For a working example (albeit with a lot of other stuff going on), you should check out the [Google I/O web app](https://github.com/GoogleChrome/ioweb2016) source code. – Philip Walton Jun 22 '16 at 18:49