自动活动跟踪如何适用于谷歌分析android

时间:2015-01-28 20:06:13

标签: android android-activity google-analytics

我已经使用viewDidAppear的快速实现为iOS实现了类似的功能,以允许我的公司在我们自己的内部系统中跟踪应用程序周围的流量。

现在,理想情况下,我希望避免必须实施“显示”和“消失跟踪我们内部使用的每个活动,所以希望有人可以了解GA库如何实现这一点。

我有一个很好的谷歌,并且在活动进入前台时无法找到任何内部事件,所以现在有点亏。

干杯!

3 个答案:

答案 0 :(得分:3)

我知道以下不是您问题的直接答案,但为什么不应用基本的OOP原则?继承。

import android.app.Activity;

public class BaseActivity extends Activity {

    @Override
    protected void onResume() {
        super.onResume();
        // hit when activity appears. Tell Appserver!
    }

    @Override
    protected void onPause() {
        super.onPause();
        // hit when activity hides. Tell Appserver!
    }

}

然后你的其他Acivities扩展了这个而不是 android.app.Activity

答案 1 :(得分:3)

您可以使用ActivityLifecycleCallback。示例如下:

public class MordorApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        registerActivityLifecycleCallbacks(new ActivityLifecycleLogger());
    }

}


public class ActivityLifecycleLogger implements Application.ActivityLifecycleCallbacks {

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        createActivityStateLog(activity, "created");
    }

    @Override
    public void onActivityStarted(Activity activity) {
        createActivityStateLog(activity, "started");
    }

    @Override
    public void onActivityResumed(Activity activity) {
        createActivityStateLog(activity, "resumed");
    }

    @Override
    public void onActivityPaused(Activity activity) {
        createActivityStateLog(activity, "paused");
    }

    @Override
    public void onActivityStopped(Activity activity) {
        createActivityStateLog(activity, "stopped");
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        createActivityStateLog(activity, "savingStateInstance");
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        createActivityStateLog(activity, "destroyed");
    }

    private void createActivityStateLog(Activity activity, String state) {
        String log = String.format("Activity %s - %s", activity.getClass().getName(), state);
        LOG.debug(log);
    }

    private static final Logger LOG = LoggerFactory.getLogger(ActivityLifecycleLogger.class);
}

答案 2 :(得分:1)

Google Analytics具有自动活动跟踪功能 看到 https://developers.google.com/analytics/devguides/collection/android/v4/?hl=en#analytics-xml

或约翰的回答:) 您可以创建委托类(假设为GaUtils),然后在恢复/暂停时调用它。只有一行,不是吗?

@Override
protected void onPause() {
    super.onPause();

    GaUtils.onPause(screenName);  // track pause
}