3

I try to send an event just before disabling the Google analytics in android. But, the event doesn't show up in the real-time GA console.

    tracker.send(new HitBuilders.EventBuilder()
                    .setCategory(category)
                    .setAction(action)
                    .setLabel(label)
                    .build());
    //disable GA
    GoogleAnalytics.getInstance(this).setAppOptOut(true);

Thanks for any advice.

cagryInside
  • 790
  • 2
  • 15
  • 41
  • real time console shows up events atleast after 60-90 secs – KOTIOS Feb 24 '15 at 05:25
  • @diva Yes I know, I test a lot – cagryInside Feb 24 '15 at 05:28
  • then there may be soem other issues , may be network or the Id for GA – KOTIOS Feb 24 '15 at 05:29
  • I don't think there is a network issue, because only this method doesn't show up. Other analytics flow in the app (event tracking not before setAppOptOut(true)) works fine. – cagryInside Feb 24 '15 at 05:31
  • oops that api basically disbale the GA tracking – KOTIOS Feb 24 '15 at 05:33
  • Yes, i just wanna send an event just before disabling the GA. But, in my understanding, GA api puts this events in some kind of stack and doesn't push immediately. When it wants the push, GA already becomes inactive – cagryInside Feb 24 '15 at 05:36
  • Also I called dispatchLocalHits() method before disabling the GA. But it is also not working :( – cagryInside Feb 24 '15 at 05:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71551/discussion-between-diva-and-cagryinside). – KOTIOS Feb 24 '15 at 05:47
  • We've found that `dispatchLocalHits()` does not work on devices with Google Play Services installed. See http://stackoverflow.com/questions/28701628/flush-google-analytics-events-manually-with-google-play-services. Does anyone know of an alternative method when using Google Play Services? – Sean Barbeau Feb 25 '15 at 16:32

1 Answers1

1

If you enable Google Analytics logging you can see that when you call setAppOptOut(true) Google Analytics will clear all queued hits since it last sent hits to the Google Analytics servers:

V/GAV4﹕ Thread[GAThread,5,main]: clearHits called

As you noticed yourself dispatchLocalHits() doesn't help since it does nothing with Google Play Services installed. What you need to do is to wait with calling setAppOptOut(true) until after your hits have been dispatched. However as you don't know when the hits are dispatched it's not an easy thing to do.

You can specify what dispatch period your app should have with the ga_dispatchPeriod setting (with the default being 30 minutes). If you wait for longer than the configured dispatch period you should be fairly certain that your event has been sent, however this is not guaranteed since GA may wait with sending the data even longer if you do not have any network connection at the moment.

If you would take this approach you must make sure that the wait works across sessions since Google Play services is a separate service on the device and it will hold on to your hits even if you restart your app. So opting out on next startup of your app will not work either.

However waiting with opting out for more than 30 minutes might not be very nice to your users since that gives a lot of time for data to be gathered and submitted after the user thinks that they have opted out.

A better approach might be to have your own internal Google Analytics wrapper class that all parts of your app calls to report data. Then each reporting method could have a check if Google Analytics is enabled, never call any of the real Google Analytics methods.

This way you can be sure that you final event is sent to Google Analytics while no more events are sent after that even though you don't call setAppOptOut(true).

Do note that this only works if you do not rely on any automatic tracking like automatic reporting of uncaught exceptions or automatic screen measurement.

Community
  • 1
  • 1
nibarius
  • 4,007
  • 2
  • 38
  • 56
  • Thanks for your reply. But, I already implement my own wrapper class to manage Ga events. It's really weird that we don't have control on dispatches – cagryInside Mar 04 '15 at 04:09
  • I noticed that you solved it yourself in the chat, but I thought I leave an answer for others with the same problem who find this question in the future. – nibarius Mar 04 '15 at 20:44