Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Google Analytics SDK 4

Im having some trouble setting up Google Analytics on my Android app. Could any one help me out and point me to some sample code or tutorial. Im trying to follow this one

Heres my code:

package com.examp2.testq;

import java.util.HashMap;

 import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



}

public enum TrackerName {
    APP_TRACKER, // Tracker used only in this app.

  }

  HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
  synchronized Tracker getTracker(TrackerName trackerId) {
        if (!mTrackers.containsKey(trackerId)) {

          GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
          Tracker t = (trackerId == TrackerName.APP_TRACKER) ?         analytics.newTracker(PROPERTY_ID)
              : 
          mTrackers.put(trackerId, t);

        }
        return mTrackers.get(trackerId);
      }

Im not sure what to do with the PROPERTY ID or how to call it? Thanks!

like image 311
user3462221 Avatar asked Dec 09 '22 08:12

user3462221


2 Answers

Put the following line inside MainActivity:

    private static final String PROPERTY_ID = "UA-xxxxx-x";
    private Tracker tracker;
    HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

replacing UA-xxxxx-x with the tracking id for your app.

I'm using Google Analytics in an app that is only one screen, so my MainActivity onCreate method looks like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

    GoogleAnalytics.getInstance(this).newTracker(PROPERTY_ID);
    GoogleAnalytics.getInstance(this).getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
    tracker = getTracker(TrackerName.APP_TRACKER);
    tracker.setScreenName("MainActivity");
    tracker.send(new HitBuilders.AppViewBuilder().build());

    setContentView(R.layout.main);
    //...etc.

This is enough for a ton of useful data in Analytics.

You'll have to add the following includes:

    import com.google.android.gms.analytics.HitBuilders;
    import com.google.android.gms.analytics.Logger;
    import com.google.android.gms.analytics.Tracker;
    import com.google.android.gms.analytics.GoogleAnalytics;

Don't forget to add the following permissions before the <application> tag inside the <manifest> tag of AndroidManifest.xml:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Google also says to add the following tag inside the <application> tag.

    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />

Lastly, if you're using Android Studio, Google says to add the following lines to proguard-rules.txt:

    -keep class * extends java.util.ListResourceBundle {
        protected Object[][] getContents();
    }

    -keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
        public static final *** NULL;
    }

    -keepnames @com.google.android.gms.common.annotation.KeepName class *
    -keepclassmembernames class * {
        @com.google.android.gms.common.annotation.KeepName *;
    }

    -keepnames class * implements android.os.Parcelable {
        public static final ** CREATOR;
    }

...and also this dependency to your project's build.gradle file:

    apply plugin: 'android'
    ...

    dependencies {
        compile 'com.android.support:appcompat-v7:+'
        compile 'com.google.android.gms:play-services:4.3.23'
    }
like image 114
Jason Hartley Avatar answered Dec 10 '22 20:12

Jason Hartley


Property id is simply a string of the format UA-XXXXXX-Y. It is also called the tracking id, webproperty id etc. You can get this from the admin settings of Google Analytics account. Most likely you already have it.

There are basically two ways of getting a tracker. You can create it from an xml file. If you are doing that, you need to use public Tracker newTracker (int configResId)

The second method is to use public Tracker newTracker (String trackingId). In your code snippet, you are using the second method.

like image 20
Avi Avatar answered Dec 10 '22 20:12

Avi