My app has one activity and many fragments. I've read about ALL topics about this question, and no one worked as I can see. I've tried with both ga v3 and v4, following the
The problem is that BOTH ways, analytics retrieve only the first call I perform. When I change fragment, and perform another call in the same way, the 'real-time' analytics doesn't change. Both snippets are executed from onStart() method in every fragment.
// Using ga v4
Tracker t = GoogleAnalytics.getInstance(this).getTracker("UA-XXXX-1");
t.setScreenName("/home");
t.send(new HitBuilders.AppViewBuilder().build());
// Using ga v3
EasyTracker _tracker = EasyTracker.getInstance(getActivity());
_tracker.set(Fields.SCREEN_NAME, "/discover");
_tracker.send(MapBuilder.createAppView().build());
Any suggestion would be highly appreciated!
Google Analytics tracks screen transitions and attaches information about the current screen to events, enabling you to track metrics such as user engagement or user behavior per screen. Much of this data collection happens automatically, but you can also manually log screenviews.
screen_view when a screen transition occurs and any of the following criteria are met: No screen was previously set. The new screen name differs from the previous screen name. The new screen-class name differs from the previous screen-class name.
Google Analytics records screens as App Views/Screen Views. The App View/Screen View is used to populate Google Analytics information such as: Screen Name: What the screen is called. Screen Views: How many times the screen has been seen.
Actually I think I might found the way to make it works. Reading the documentation: https://developers.google.com/analytics/devguides/collection/android/v4/dispatch
By default, data is dispatched from the Google Analytics SDK for Android every 30 minutes.
Yes, my thoughts were "What???? 30 minutes? Is not suppose to be real time?" Anyway, reading a bit more:
To set the dispatch period programmatically:
// Set the dispatch period in seconds.
GoogleAnalytics.getInstance(this).setLocalDispatchPeriod(15);
To set the dispatch period in the XML configuration file:
<integer name="ga_dispatchPeriod">30</integer>
What more...
Setting a negative value will disable periodic dispatch, requiring that you use manual dispatch if you want to send any data to Google Analytics.
// Disable periodic dispatch by setting dispatch period to a value less than 1.
GoogleAnalytics.getInstance(this).setLocalDispatchPeriod(0);
Ok, maybe you have found a controversial here, negative value or less than 1? I tried, it is less than 1! (0 or negative number will disable periodic dispatch).
If you prefer then the manual dispatch, that would be, for example:
tracker.setScreenName(TAG);
tracker.send(new HitBuilders.AppViewBuilder().build());
GoogleAnalytics.getInstance(getActivity().getBaseContext()).dispatchLocalHits();
Notice that you can call dispatchLocalHits() whenever you want (even make a button that calls that function). By the way, that piece of code it's on my fragments (in the onResume() method).
Also the documentation says:
If a user loses network access or quits your app while there are still hits waiting to be >dispatched, those hits are persisted in local storage. They will be dispatched the next >time your app is running and dispatch is called.
But I was not sure if this applies to both manual and periodic dispatch. I tried it, both seem to be working :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With