Integrate Google Analytics “v4” into an Android ap

2019-03-06 07:22发布

Thanks for the answer, but it still does not work.

I have create :

public class GAnalytics extends Application{

// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-XXXX-X";

public static int GENERAL_TRACKER = 0;

public enum TrackerName {
    APP_TRACKER, // Tracker used only in this app.
    GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
    ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}

HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

public GAnalytics() {
    super();
}

synchronized Tracker getTracker(TrackerName trackerId) {
    if (!mTrackers.containsKey(trackerId)) {

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

    }
    return mTrackers.get(trackerId);
}

}

Under OnCreate in my MainActivity:

String ACTIVITY_NAME = "MyApp";
// Get tracker.
    Tracker t = ((GAnalytics) getApplication()).getTracker(TrackerName.APP_TRACKER);

    // Set screen name.
    // Where path is a String representing the screen name.
    t.setScreenName(ACTIVITY_NAME);

    // Send a screen view.
    t.send(new HitBuilders.AppViewBuilder().build());

I have added the manifest under Application (android: name...)

And my Log:

05-16 18:27:41.705: E/AndroidRuntime(25187): FATAL EXCEPTION: main 05-16 18:27:41.705: E/AndroidRuntime(25187): Process: com.MyApplication.Banana, PID: 25187 05-16 18:27:41.705: E/AndroidRuntime(25187): java.lang.NoClassDefFoundError: com.google.android.gms.analytics.GoogleAnalytics 05-16 18:27:41.705: E/AndroidRuntime(25187): at com.MyApplication.Banana.GAnalytics.getTracker(GAnalytics.java:30) 05-16 18:27:41.705: E/AndroidRuntime(25187): at com.MyApplication.Banana.MainActivity.onCreate(MainActivity.java:56) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.Activity.performCreate(Activity.java:5231) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread.access$800(ActivityThread.java:135) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.os.Handler.dispatchMessage(Handler.java:102) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.os.Looper.loop(Looper.java:136) 05-16 18:27:41.705: E/AndroidRuntime(25187): at android.app.ActivityThread.main(ActivityThread.java:5017) 05-16 18:27:41.705: E/AndroidRuntime(25187): at java.lang.reflect.Method.invokeNative(Native Method) 05-16 18:27:41.705: E/AndroidRuntime(25187): at java.lang.reflect.Method.invoke(Method.java:515) 05-16 18:27:41.705: E/AndroidRuntime(25187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 05-16 18:27:41.705: E/AndroidRuntime(25187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-16 18:27:41.705: E/AndroidRuntime(25187): at dalvik.system.NativeStart.main(Native Method)

2条回答
Fickle 薄情
2楼-- · 2019-03-06 07:54

Today i faced same issue and got your post, but no one has pointed to right direction.

But i found solution and got the google analytics code working.

Please do follow:

1st - Move tracking code to your fragment class in public void onCreate(Bundle savedInstanceState) method (this has been changed in V4)
Reason: Because the lifetime of fragments is not as straightforward as that of Activities. Android does not provide callbacks for fragment lifecycle.

2nd - Step 2 from v4 implementation doc the Initialize Trackers code should be written into the Application class. Important for Application Class refer application tag and property as android:name="com.brodev.socialapp.entity.User" from AndroidManifest.xml

Please mark answer UP if this helps. So it will help others.

查看更多
欢心
3楼-- · 2019-03-06 08:06

As described in your link, the GA-related code should be placed inside the Application class instead of the Activity.

Create a class extends Application like this:

public class MyApplication extends Application{
    //the tracker related code should be place here (the enum, getTracker, mTrackers)
}

then in your AndroidManifest.xml, under the application tag, replace the android:name with the above MyApplication class.

The tracker code should be placed in the Activity.oncreate method such as

public class MainActivity extends Activity {
.
.
.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
// Get tracker.
    Tracker t = ((AnalyticsSampleApp) 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());
}
.
.
.
}
查看更多
登录 后发表回答