gpt4 book ai didi

android - 在 Android 服务应用程序中使用 Google Analytics

转载 作者:行者123 更新时间:2023-11-30 02:21:24 24 4
gpt4 key购买 nike

我想在基于 Android 服务的应用程序中实现 Google Analytics。该应用程序有一个主屏幕小部件,我想跟踪某些使用趋势。如大多数示例所示,我是否必须使用应用程序?我不能只使用一个静态变量来保存跟踪器并使用它来将信息“发送”到 Google Analytics 吗?如果没有 reportActivityStart 和 reportActivityStop 调用,分析服务是否无法工作?我已经尝试过我所描述的内容,我可以看到 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());
}
}
}

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

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

最佳答案

试试下面的代码:

我的申请

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

}
}

在 Activity 中将屏幕 View 发送为:

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

发送事件 [onClick of any view]

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

关于android - 在 Android 服务应用程序中使用 Google Analytics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28505985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com