在 Android 服务应用程序中使用 Google 分析
Using Google Analytics in Android service application
我想在基于 Android 服务的应用程序中实施 Google 分析。该应用程序有一个主屏幕小部件,我想跟踪某些使用趋势。如大多数示例所示,我是否必须使用应用程序?我不能只使用一个静态变量来保存跟踪器并将其用于 'send' 信息到 Google 分析吗?如果没有 reportActivityStart 和 reportActivityStop 调用,分析服务是否无法工作?我已经尝试了我所描述的,我可以看到 GAV4 logcat 输出并且一切似乎都正常工作 - 完全没有错误(并且 DryMode 未设置)但是我没有看到 [=24= 上的任何信息] 分析网站。
这是处理日志记录的 class 的代码:
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());
}
}
}
以下是从主屏幕服务调用 class 以记录操作或页面视图:
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 中发送屏幕浏览量为:
@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 任何视图]
tvOpt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SendEventGoogleAnalytics("Button", "on click of test option1",
"test option1 clicked");
}
});
以下是 GA 仪表板上实时事件点击选项卡的屏幕截图
我想在基于 Android 服务的应用程序中实施 Google 分析。该应用程序有一个主屏幕小部件,我想跟踪某些使用趋势。如大多数示例所示,我是否必须使用应用程序?我不能只使用一个静态变量来保存跟踪器并将其用于 'send' 信息到 Google 分析吗?如果没有 reportActivityStart 和 reportActivityStop 调用,分析服务是否无法工作?我已经尝试了我所描述的,我可以看到 GAV4 logcat 输出并且一切似乎都正常工作 - 完全没有错误(并且 DryMode 未设置)但是我没有看到 [=24= 上的任何信息] 分析网站。
这是处理日志记录的 class 的代码:
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());
}
}
}
以下是从主屏幕服务调用 class 以记录操作或页面视图:
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 中发送屏幕浏览量为:
@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 任何视图]
tvOpt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SendEventGoogleAnalytics("Button", "on click of test option1",
"test option1 clicked");
}
});
以下是 GA 仪表板上实时事件点击选项卡的屏幕截图