Monday, April 7, 2014

Upgrading Google Analytics for Android v2 to v4

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):

  1. Move the analytics.xml from /res/values to /res/xml
  2. Add the following line to the Manifest.xml:
    <meta-data android:name="" android:resource="@xml/analytics" />
  3. Replace the campaign tracker in the Manifest.xml if need:
    <service android:name="" />
    <receiver android:name=""
      <action android:name="" />
  4. Add the following code to the Application class:
  5. private Tracker mTracker = null;
    synchronized public Tracker getTracker() {
      if (mTracker == null) {
        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        mTracker = analytics.newTracker(;
      return mTracker;
  6. Activity tracking - replace:
    public void onStart() {
      mTracker = EasyTracker.getTracker();
    public void onStop() {
    public void onStart() {
      mTracker = ((MyApplication)getApplication()).getTracker();
    public void onStop() {
  7. Update events tracking to the new format:
  8. mTracker.send(new HitBuilders.EventBuilder()


Fung said...

Thanks for posting this. Doesn't seem to be a migration guide on

Trung Nguyen Nam said...

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.

Gavriel said...

Sagi, you'll need to html encode the xml in the 2nd step.

Erik André said...

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 :)