I couldn't find a single place where all this information can be found, so here are the steps to upgrade Google Analytics for Android v2 to v4 (Google Play Services). This does not include the steps needed to integrate Google Play Services (some Manifest.xml & library includes):
- Move the analytics.xml from /res/values to /res/xml
- Add the following line to the Manifest.xml:
<meta-data android:name="com.google.android.gms.analytics.globalConfigResource" android:resource="@xml/analytics" />
- Replace the campaign tracker in the Manifest.xml if need:
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
- Add the following code to the Application class:
private Tracker mTracker = null;
synchronized public Tracker getTracker() {
if (mTracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
mTracker = analytics.newTracker(R.xml.analytics);
}
return mTracker;
}
- Activity tracking - replace:
@Override
public void onStart() {
super.onStart();
EasyTracker.getInstance().activityStart(this);
mTracker = EasyTracker.getTracker();
}
@Override
public void onStop() {
super.onStop();
EasyTracker.getInstance().activityStop(this);
}
with:
@Override
public void onStart() {
super.onStart();
mTracker = ((MyApplication)getApplication()).getTracker();
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}
@Override
public void onStop() {
super.onStop();
GoogleAnalytics.getInstance(this).reportActivityStop(this);
}
- Update events tracking to the new format:
mTracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.setValue(val)
.build());
4 comments:
Thanks for posting this. Doesn't seem to be a migration guide on https://developers.google.com/analytics/devguides/collection/android/v4/
If i got my custom receiver, do i have to register both receiver in manifest? ( Mine and google one)
Thanks. I saw logcat said campaign found but i dont get data in google report.
Sagi, you'll need to html encode the xml in the 2nd step.
Thank you for posting this very useful guide!
I've been putting off upgrading to v4 for quite some time but thanks to this it turned out to be quite easy :)
Post a Comment