In Google Analytics v3, to auto tracking for Activity
, we need to have
ga_autoActivityTracking
flag in manifest.xml.onStart
and onStop
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
ga_autoActivityTracking
flag in manifest.xml.<?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?
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.
You can sign in to your Analytics account from http://www.google.com/analytics. Click Sign in (at top right), and select Analytics.
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>
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;
}
Turn on GA during activity startup
public class MyFragmentActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.getTracker();
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.
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