[英]How to implement the new Google Play Services (Analytics)?
I don't know exactly how to implement the new GPS Analytics into my app. 我不知道如何在我的应用程序中实施新的GPS Analytics。 My problem is located at the 4th step being described here .
我的问题位于此处描述的第四步。 I dont know how to use
我不知道怎么用
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
correctly. 正确地。 More exactly what do I need to write for AnalyticsSampleApp .
更确切地说,我需要为AnalyticsSampleApp编写什么。
My Apps package name is com.mnd.tsst 我的Apps套件名称为com.mnd.tsst
My global_tracker.xml: 我的global_tracker.xml:
<?xml version="1.0" encoding="utf-8"?>
300 300
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The screen names that will appear in reports -->
<screenName name="com.google.android.gms.analytics.samples.mobileplayground.ScreenviewFragment">
TimetableApp TimeTableView
</screenName>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-xxxxxx-5</string>
I entered a correct property ID, just removed it to show it here. 我输入了正确的属性ID,只是将其删除以在此处显示。
You need to create a new class that extends Application
class with any name you want (say, MyApp
) 您需要创建一个新类,用您想要的任何名称扩展
Application
类(例如, MyApp
)
In this class you have to add the methods and fields as suggested in the guide to be added to Application
class. 在此类中,您必须按照要添加到
Application
类中的指南中的建议添加方法和字段。
public class MyApp extends Application {
public enum TrackerName {
APP_TRACKER,
GLOBAL_TRACKER,
E_COMMERCE_TRACKER,
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
: analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
Also, add the following in the application
tag of your manifest: 另外,在清单的
application
标签中添加以下内容:
android:name=".MyApp"
where MyApp
is the name you have given to your Application
class. MyApp
是您为Application
类指定的名称。
In the global_tracker.xml file, I have yet understand the purpose of screenName
, but however, for the time being, you should at least modify the sample package address to package name of your own Activities. 在global_tracker.xml文件中,我尚未了解
screenName
的用途,但是暂时,您至少应将示例程序包地址修改为您自己的Activity的程序包名称。
Now, let's say to send a screen view to Analytics, you would simply type anywhere inside any activity (maybe in onCreate or onResume): 现在,假设要将屏幕视图发送到Google Analytics(分析),您只需在任何活动内的任何地方键入内容(可能在onCreate或onResume中):
Tracker t = ((MyApp) getApplication()).getTracker(
TrackerName.APP_TRACKER); // Get tracker.
t.setScreenName(ACTIVITY_NAME); // Pass a String representing the screen name.
t.send(new HitBuilders.AppViewBuilder().build()); // Send a screen view.
Similarly, to send an event (maybe from onClick of some button) 同样,要发送事件(可能来自某些按钮的onClick)
Tracker t = ((MyApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
t.send(new HitBuilders.EventBuilder()
.setCategory(categoryString)
.setAction(actionString)
.setLabel(labelString)
.build());
This should give you a head-start. 这应该给您一个领先的机会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.