Skip to content Skip to sidebar Skip to footer

Is There Any Code Needed In Activity So That Ga_autoactivitytracking = True Would Work For Google Analytics V4

In Google Analytics v3, to auto tracking for Activity, we need to have ga_autoActivityTracking flag in manifest.xml. GA code in onStart and onStop Google Analytics SDK for Androi

Solution 1:

Step 1

Add app_tracker.xml

<?xml version="1.0" encoding="utf-8"?><resources><!--  The following value should be replaced with correct property id. --><stringname="ga_trackingId">UA-00000000-1</string><!-- catch and report uncaught exceptions from the app --><boolname="ga_reportUncaughtExceptions">true</bool><integername="ga_sessionTimeout">300</integer><!-- Enable automatic Activity measurement --><boolname="ga_autoActivityTracking">true</bool><!-- The screen names that will appear in reports --><screenNamename="com.mypackage.NameActivity">Name Activity</screenName></resources>

Step 2

Added getTracker

publicstatic Tracker getTracker() {
    if (false == isGooglePlayServicesAvailable()) {
        returnnull;
    }

    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

publicclassMyFragmentActivityextendsSherlockFragmentActivity {
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);

        Utils.getTracker();

Solution 2:

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.

Solution 3:

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

// Get tracker.Trackert= ((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(newHitBuilders.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.

Post a Comment for "Is There Any Code Needed In Activity So That Ga_autoactivitytracking = True Would Work For Google Analytics V4"