Is there any code needed in Activity so that ga_autoActivityTracking = true would work for Google Analytics V4

纵然是瞬间 提交于 2019-12-03 11:33:23
Cheok Yan Cheng

Step 1

Add app_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--  The following value should be replaced with correct property id. -->
    <string name="ga_trackingId">UA-00000000-1</string>

    <!-- catch and report uncaught exceptions from the app -->
    <bool name="ga_reportUncaughtExceptions">true</bool>

    <integer name="ga_sessionTimeout">300</integer>

    <!-- Enable automatic Activity measurement -->
    <bool name="ga_autoActivityTracking">true</bool>

    <!-- The screen names that will appear in reports -->
    <screenName name="com.mypackage.NameActivity">Name Activity</screenName>

</resources>

Step 2

Added getTracker

public static Tracker getTracker() {
    if (false == isGooglePlayServicesAvailable()) {
        return null;
    }

    if (tracker == null) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(MyApplication.instance());
        tracker = analytics.newTracker(R.xml.app_tracker);
    }
    return tracker;
}

Step 3

Turn on GA during activity startup

public class MyFragmentActivity extends SherlockFragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);

        Utils.getTracker();
snark

Yes, there appears to be a bug in GAV4. See the answers here and here.

For devices running API v14 (Ice Cream Sandwich) or later you need to call enableAutoActivityReports in addition to setting ga_autoActivityTracking to true in your tracker configuration file. I've confirmed this works (that is, screen views do get reported in my Google Analytics console) on a post v14 device.

If you want your app to support devices running pre-API 14 you also have to add calls to reportActivityStart and reportActivityStop in onStart and onStop for all the activities you want to track. I've confirmed this works on a pre v14 device.

I've only tried this with activities, not fragments, and, from one of the links above, it looks like automated screen tracking doesn't work with fragments.

You need to add the following code mentioned in Step 4 of the link posted by you in your Activity/Fragment code:

   // Get tracker.
    Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
        TrackerName.APP_TRACKER);

    // Set screen name.
    // Where path is a String representing the screen name.
    t.setScreenName(path);

    // Send a screen view.
    t.send(new HitBuilders.AppViewBuilder().build());


If you look at the link: https://developers.google.com/analytics/devguides/collection/android/v4/screens#implementation, there's a sample Fragment snippet given there as well.


EDIT:
Sorry, the above information was for manual tracking.
As per this link: https://developers.google.com/analytics/devguides/collection/android/v4/screens#automatic, if you turn on automatic screen view tracking in your configuration XML, you need to perform only two steps:

  • Set the ga_autoActivityTracking parameter in your XML configuration file.
  • Give each of your Activities a screen name in your XML configuration file.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!