I followed this guide to implement Google Analytics in my Android app. I am having trouble understanding the document.
In the document it says to create an XML file with this as content:
<screenName name="com.mycompany.myapp.MainActivity">
SomeApp MainActivity
</screenName>
And to send the screen view, I have to do this:
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName(screenName);
t.send(new HitBuilders.AppViewBuilder().build());
However, I am unsure what this screenName
should be. Is it the name
value (com.mycompany.myapp.MainActivity
) or the screenName
value (SomeApp MainActivity
)?
The xml configuration you describe is used whenever 'automatic Activity tracking' is enabled. This tracking automatically reports a new screen view whenever an Activity starts, saving you from manually triggering screen view events in your code.
To enable auto Activity tracking, you can set the ga_autoActivityTracking
boolean to true in your tracker's xml configuration:
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
By default, this automatic reporting tool will use your Activity class names as the reported screen name (e.g. com.mycompany.myapp.MainActivity
). These class names are often long and hard to read, so Google allows us to customize the reported screen name for each Activity. That's what this part of your question is doing:
<screenName name="com.mycompany.myapp.MainActivity">
SomeApp MainActivity
</screenName>
With this configuration, auto Activity tracking would use the string "SomeApp MainActivity" instead of "com.mycompany.myapp.MainActivity" when automatically reporting screen views for that Activity.
It's important to note that if you are not using auto Activity tracking, any screen name configuration in the tracker's xml will be ignored. In that case, you will have to specify the screen name each time you programmatically trigger a screen view event. That's what is happening in your code example:
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName(screenName);
t.send(new HitBuilders.AppViewBuilder().build());
The screen name setup in your xml configuration is not used in this code, because it's not part of auto Activity tracking. Instead, you have to manually set the screen name on the tracker before sending the screen view. This is more flexible than auto Activity tracking - for example, in a Fragment-heavy app, navigation might be performed via Fragment transactions rather than Activity changes. In that scenario, tracking Activity changes alone would not report every screen change in your app appropriately. Auto Activity tracking is supposed to be less overhead than manually sending screen views, but I find it inflexible and more prone to error (you have to manually add <screenName>
entries to your xml every time a new Activity is introduced, and it's very easy to forget).
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