如果不在Android Google Analytics中调用stop()函数会发生什么?

时间:2011-11-02 15:20:18

标签: android google-analytics

我一直在查看GoogleIO Android app的代码,我注意到他们没有在GoogleAnalytics实例上调用stop()函数。如果我们不致电stop()会怎样?

这是代码:

package com.google.android.apps.iosched.util;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * Helper singleton class for the Google Analytics tracking library.
 */
public class AnalyticsUtils {
    private static final String TAG = "AnalyticsUtils";

    GoogleAnalyticsTracker mTracker;
    private Context mApplicationContext;

    /**
     * The analytics tracking code for the app.
     */
    // TODO: insert your Analytics UA code here.
    private static final String UACODE = "INSERT_YOUR_ANALYTICS_UA_CODE_HERE";

    private static final int VISITOR_SCOPE = 1;
    private static final String FIRST_RUN_KEY = "firstRun";
    private static final boolean ANALYTICS_ENABLED = true;

    private static AnalyticsUtils sInstance;

    /**
     * Returns the global {@link AnalyticsUtils} singleton object, creating one if necessary.
     */
    public static AnalyticsUtils getInstance(Context context) {
        if (!ANALYTICS_ENABLED) {
            return sEmptyAnalyticsUtils;
        }

        if (sInstance == null) {
            if (context == null) {
                return sEmptyAnalyticsUtils;
            }
            sInstance = new AnalyticsUtils(context);
        }

        return sInstance;
    }

    private AnalyticsUtils(Context context) {
        if (context == null) {
            // This should only occur for the empty Analytics utils object.
            return;
        }

        mApplicationContext = context.getApplicationContext();
        mTracker = GoogleAnalyticsTracker.getInstance();

        // Unfortunately this needs to be synchronous.
        mTracker.start(UACODE, 300, mApplicationContext);

        Log.d(TAG, "Initializing Analytics");

        // Since visitor CV's should only be declared the first time an app runs, check if
        // it's run before. Add as necessary.
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mApplicationContext);
        final boolean firstRun = prefs.getBoolean(FIRST_RUN_KEY, true);
        if (firstRun) {
            Log.d(TAG, "Analytics firstRun");

            String apiLevel = Integer.toString(Build.VERSION.SDK_INT);
            String model = Build.MODEL;
            mTracker.setCustomVar(1, "apiLevel", apiLevel, VISITOR_SCOPE);
            mTracker.setCustomVar(2, "model", model, VISITOR_SCOPE);

            // Close out so we never run this block again, unless app is removed & =
            // reinstalled.
            prefs.edit().putBoolean(FIRST_RUN_KEY, false).commit();
        }
    }

    public void trackEvent(final String category, final String action, final String label,
            final int value) {
        // We wrap the call in an AsyncTask since the Google Analytics library writes to disk
        // on its calling thread.
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    mTracker.trackEvent(category, action, label, value);
                    Log.d(TAG, "iosched Analytics trackEvent: "
                            + category + " / " + action + " / " + label + " / " + value);
                } catch (Exception e) {
                    // We don't want to crash if there's an Analytics library exception.
                    Log.w(TAG, "iosched Analytics trackEvent error: "
                            + category + " / " + action + " / " + label + " / " + value, e);
                }
                return null;
            }
        }.execute();
    }

    public void trackPageView(final String path) {
        // We wrap the call in an AsyncTask since the Google Analytics library writes to disk
        // on its calling thread.
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    mTracker.trackPageView(path);
                    Log.d(TAG, "iosched Analytics trackPageView: " + path);
                } catch (Exception e) {
                    // We don't want to crash if there's an Analytics library exception.
                    Log.w(TAG, "iosched Analytics trackPageView error: " + path, e);
                }
                return null;
            }
        }.execute();
    }

    /**
     * Empty instance for use when Analytics is disabled or there was no Context available.
     */
    private static AnalyticsUtils sEmptyAnalyticsUtils = new AnalyticsUtils(null) {
        @Override
        public void trackEvent(String category, String action, String label, int value) {}

        @Override
        public void trackPageView(String path) {}
    };
}

正如您所看到的,他们以5分钟的间隔mTracker.start(UACODE, 300, mApplicationContext);启动跟踪器,但从不调用mTracker.stop()方法。会有什么后果吗?这是否意味着即使应用程序关闭或停止,服务也会调度数据?

2 个答案:

答案 0 :(得分:2)

EasyTracker - 我在Google Analytics分析博客中找到了this blog entry。在那里,他们描述了一个EasyTracker类,它包含了普通的Tracker类,并且有一些不错的功能。

  • 通过资源文件配置(无需编码)
  • 一切都在一个单独的线程中完成(不像普通的跟踪器那样在ui线程中完成)
  • ...

此EasyTracker也不需要被明确表示。

答案 1 :(得分:1)

应该不会发生太多 - 当你关闭或停止应用程序时,即使它仍然存在于后台并且不再有任何活动,也不会收集新数据(因为没有人再调用track()所以我认为明确停止跟踪器是一种很好的举止。