In Google Analytics v3, to auto tracking for Activity
, we need to have
ga_autoActivityTracking
flag in manifest.xml.- GA code in
onStart
andonStop
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
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?
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();
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.
来源:https://stackoverflow.com/questions/24474203/is-there-any-code-needed-in-activity-so-that-ga-autoactivitytracking-true-woul