Google Analytics View Snippet

My application has one activity and many fragments. I read ALL topics on this subject, and no one worked, as I see it. I tried both ga v3 and v4, after

The problem is that in BOTH ways, analysts only retrieve the first call I make. When I change a fragment and make another call in the same way, real-time analytics does not change. Both fragments are executed from the onStart () method in each 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!

+6
source share
1 answer

In fact, I think I could find a way to make it work. Read the documentation: https://developers.google.com/analytics/devguides/collection/android/v4/dispatch

By default, data is sent from the Google Analytics SDK for Android every 30 minutes .

Yes, my thoughts were "What ?? 30 minutes? It is not assumed that you are in real time?" Anyway, reading a little more:

To set the sending period for the program period:

 // Set the dispatch period in seconds. GoogleAnalytics.getInstance(this).setLocalDispatchPeriod(15); 

To set the send period in the XML configuration file:

 <integer name="ga_dispatchPeriod">30</integer> 

What else...

Setting a negative value will disable periodic sending, requiring you to use manual sending 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); 

Well, maybe you find the questionable here, a negative value or less than 1? I tried, it is less than 1! (0 or a negative number will disable periodic sending).

If you prefer manual sending, it will be, for example:

 tracker.setScreenName(TAG); tracker.send(new HitBuilders.AppViewBuilder().build()); GoogleAnalytics.getInstance(getActivity().getBaseContext()).dispatchLocalHits(); 

Note that you can call dispatchLocalHits () whenever you want (even create a button that calls this function). By the way, this part of the code is on my snippets (in the onResume () method).

The documentation also says:

If a user loses access to the network or leaves your application while there are still hits awaiting sending> sending, these hits are stored in local storage. They will be sent the next time your application is launched and sending is called.

But I was not sure that this applies to both manual and periodic dispatch. I tried, both seem to work :)

+5
source

All Articles