将Google Analytics“v4”集成到Android应用中

时间:2014-05-11 17:52:40

标签: android google-analytics

感谢您的回答,但它仍然不起作用。

我创造了:

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);
}

}

在我的MainActivity中的OnCreate下:

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());

我在Application(android:name ...)

下添加了清单

我的日志:

  

05-16 18:27:41.705:E / AndroidRuntime(25187):致命异常:主要   05-16 18:27:41.705:E / AndroidRuntime(25187):进程: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):在android.app.Activity.performCreate(Activity.java:5231)   05-16 18:27:41.705:E / AndroidRuntime(25187):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)   05-16 18:27:41.705:E / AndroidRuntime(25187):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)   05-16 18:27:41.705:E / AndroidRuntime(25187):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)   05-16 18:27:41.705:E / AndroidRuntime(25187):在android.app.ActivityThread.access $ 800(ActivityThread.java:135)   05-16 18:27:41.705:E / AndroidRuntime(25187):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)   05-16 18:27:41.705:E / AndroidRuntime(25187):在android.os.Handler.dispatchMessage(Handler.java:102)   05-16 18:27:41.705:E / AndroidRuntime(25187):在android.os.Looper.loop(Looper.java:136)   05-16 18:27:41.705:E / AndroidRuntime(25187):在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 个答案:

答案 0 :(得分:2)

如您的链接所述,与GA相关的代码应放在Application类而不是Activity内。

像这样创建一个类Application

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

然后在AndroidManifest.xml的{​​{1}}标记下,将application替换为上述android:name类。

跟踪代码应放在MyApplication方法中,例如

Activity.oncreate

答案 1 :(得分:2)

今天我面临同样的问题并得到了你的职位,但没有人指出正确的方向。

但我找到了解决方案,并使谷歌分析代码正常运作。

请遵循:

1st - 将跟踪代码移动到 public void onCreate(Bundle savedInstanceState)方法中的片段类(在V4中已更改)
原因:因为片段的生命周期不像活动那么简单。 Android不提供片段生命周期的回调。

第2步 - 来自v4实现文档的第2步,应将Initialize Trackers代码写入Application类。对于Application类而言重要的是将应用程序标记和属性称为AndroidManifest.xml中的android:name =“com.brodev.socialapp.entity.User”

如果有帮助,请标记答案。所以它会帮助别人。