Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  1. ga_autoActivityTracking flag in manifest.xml.
  2. GA code in onStart and onStop

Google Analytics SDK for Android v3

public class myTrackedActivity extends Activity {

  @Override
  public void onStart() {
    super.onStart();
    ... // The rest of your onStart() code.
    EasyTracker.getInstance(this).activityStart(this);  // Add this method.
  }

  @Override
  public void onStop() {
    super.onStop();
    ... // The rest of your onStop() code.
    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
  }
}

<?xml version="1.0" encoding="utf-8" ?>

<resources>
  <!--Replace placeholder ID with your tracking ID-->
  <string name="ga_trackingId">UA-XXXX-Y</string>

  <!--Enable automatic activity tracking-->
  <bool name="ga_autoActivityTracking">true</bool>

  <!--Enable automatic exception tracking-->
  <bool name="ga_reportUncaughtExceptions">true</bool>
</resources>

However, when comes to newer version of Google Analytics v4, (https://developers.google.com/analytics/devguides/collection/android/v4/), I don't see any code need to be added in Activity. From the above documentation, it seems that we only need

  1. ga_autoActivityTracking flag in manifest.xml.

Google Analytics SDK v4

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <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.google.android.gms.analytics.samples.mobileplayground.ScreenviewFragment">
        AnalyticsSampleApp ScreenView
    </screenName>
    <screenName name="com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment">
        AnalyticsSampleApp EcommerceView
    </screenName>
    <!--  The following value should be replaced with correct property id. -->
    <string name="ga_trackingId">UA-XXXXXXX-Y</string>
</resources>

However, after tested, it seems that there is no tracking information being automatically sent out from Activity.

Is the documentation for Google Analytics V4 missing something?

like image 284
Cheok Yan Cheng Avatar asked Jun 29 '14 07:06

Cheok Yan Cheng


People also ask

Where do I get Google Analytics code?

Google Analytics Tracking Code On the bottom left-hand side of the page, click Admin. From the left column titled Account, select an account from the dropdown menu. From the middle column titled Property, select a property from the dropdown menu. Under the Property column, click Tracking Info > Tracking Code.

How do I check Google Analytics?

You can sign in to your Analytics account from http://www.google.com/analytics. Click Sign in (at top right), and select Analytics.


2 Answers

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();
like image 134
Cheok Yan Cheng Avatar answered Oct 17 '22 02:10

Cheok Yan Cheng


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.

like image 33
snark Avatar answered Oct 17 '22 02:10

snark