在Android服务应用中使用Google Analytics

时间:2015-02-13 18:11:39

标签: android analytics

我想在基于Android服务的应用程序中实施Google Analytics。该应用程序有一个主屏幕小部件,我想跟踪某些使用趋势。我是否必须使用应用程序,如大多数示例所示?我不能只使用一个静态变量来保存跟踪器并使用它来发送' Google Analytics的信息?如果没有reportActivityStart和reportActivityStop调用,Google Analytics服务是否无法正常运行?我已经尝试了我所描述的内容,我可以看到GAV4 logcat输出,一切似乎都能正常工作 - 根本没有错误(并且没有设置DryMode)但我在Google Analytics网站上看不到任何信息。

以下是处理日志记录的类的代码:

public class AnalyticsHelper {

    private static final String PROPERTY_ID = "UA-xxxxxx-xx";

    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.
    }

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

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

            GoogleAnalytics analytics = GoogleAnalytics.getInstance(WidgetHelper.context); 
            analytics.setDryRun(false);
            Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker) : analytics.newTracker(PROPERTY_ID);
                    mTrackers.put(trackerId, t);
        }

        return mTrackers.get(trackerId); 
    }

    public static void sendView(String screenName) {
        Tracker t = getTracker(TrackerName.APP_TRACKER); 
        if (t != null) { 
            t.setScreenName(screenName); 
            t.send(new HitBuilders.AppViewBuilder().build());
        }
    }

    public static void sendAction(String category, String action, String label, int value) {
        Tracker t = getTracker(TrackerName.APP_TRACKER); 
        if (t != null) { 
            t.send(new HitBuilders.EventBuilder()
        .setCategory(category)
        .setAction(action) 
        .setLabel(label) 
        .setValue(value) 
        .build());
        }
    }
}

以下是从主屏幕服务中记录操作或页面视图的类调用:

AnalyticsHelper.sendAction("Service", "Update", "", 0);
...
AnalyticsHelper.sendView("Settings");

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

<强>所有MyApplication

public class MyApplication extends Application {

    private static final String PROPERTY_ID = "UX-XXXXXXXX-Y";
    private static final String TAG = "MyApp";
    public static int GENERAL_TRACKER = 0;
    private static Context mContext;

    public enum TrackerName {
        APP_TRACKER, GLOBAL_TRACKER, ECOMMERCE_TRACKER,
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }

    public static Context getContext() {
        return mContext;
    }

    public MyApplication() {
        super();
    }

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

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

        }
        return mTrackers.get(trackerId);
    }
}

<强> GoogleAnalyticsHelper

public class GoogleAnalyticsHelper {

private Tracker mGaTracker = null;
private static String TAG = "GoogleAnalyticsHelper";


public GoogleAnalyticsHelper()    
{

}
public void init(Context ctx) {
    try {

        if (mGaTracker == null && ctx != null)
        {
            mGaTracker = GoogleAnalytics.getInstance(ctx).newTracker(Constant.PROPERTY_ID);
        }
    } catch (Exception e) {
        Log.d(GoogleAnalyticsHelper.TAG,"init, e="+e);
    }
}

public void SendScreenNameGoogleAnalytics(String screenName, Context iCtx)
{
    init(iCtx);

    mGaTracker.setScreenName(screenName);
    mGaTracker.send(new HitBuilders.AppViewBuilder().build());

}


public void SendEventGoogleAnalytics(Context iCtx,String iCategoryId, String iActionId, String iLabelId)
{
    init(iCtx);

        // Build and send an Event.
        mGaTracker.send(new HitBuilders.EventBuilder()
            .setCategory(iCategoryId)
            .setAction(iActionId)
            .setLabel(iLabelId)
            .build());

     }
  }

将活动中的屏幕视图发送为:

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

private void InitGoogleAnalytics()
    {           
        mGoogleHelper = new GoogleAnalyticsHelper();        
        mGoogleHelper.init(FragmentActivity_DashBoard.this);      
    }

    private void SendScreenNameGoogleAnalytics()
    {

        mGoogleHelper.SendScreenNameGoogleAnalytics("FragmentActivity_DashBoard",FragmentActivity_DashBoard.this);
    }

发送事件[点击任意视图]

tvOpt1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SendEventGoogleAnalytics("Button", "on click of test option1",
                        "test option1 clicked");
            }
        });

以下是GA仪表板上实时点击事件的屏幕截图

enter image description here