Search code examples
google-analyticsbackgroundtitaniumappcelerator

Titanium app - Google Analytics gives active usage but is in background. How to fix?


our app uses Google Analytics (https://github.com/benbahrenburg/Ti.GA) but we've noticed that the data that we actually can view is not true.

We have a lot of more active usage of the app than it should have. We believe it is because our app uses background mode fetch with Apple Healthkit- which makes the app resume and send data to the backend.

We have just, directly in app.js put the init-script for the GA-module, but maybe that's not the way to do it?

I guess we should initialize the GA-script if the app actually is active and opened by the user. But how do we detect and do that?

Is there like some way to for sure know if the user has opened the app, and if the app is resumed, by the user, keep on tracking the GA-event?


Solution

  • You should only fire pageviews/screenviews and other events using the postlayout events of windows.

    Initialising Ti.Ga in the background on app.js is fine as long as it doesn't send anything. So if your app comes from the background it seems it is an incorrect way of doing so.

    I recommend writing a wrapper lib for Ti.Ga and calling that lib for sending data to Google Analytics. In that lib when receiving data from your app check if it is already initialised, and if not, init it then. Not in app.js