问题描述
我在我的应用中加入了 Google 分析.但是当我创建 Tracker
的对象时,跟踪器不起作用
I have included Google analytics in my app.But when i create an object of Tracker
the tracker does not work
在我的一项活动中编码
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
try{
tracker=((GoogleAnalyticsConfig)this.getApplication()).getTracker(GoogleAnalyticsConfig.TrackerName.APP_TRACKER);
tracker.setScreenName("HomeActivity");
tracker.send(new HitBuilders.AppViewBuilder().build());
}catch(Exception ex)
{
ex.printStackTrace();
}
}
现在使用此代码,我在实时跟踪中看不到该应用程序但是如果我用这个改变上面的代码
Now with this code i don't see the app in Realtime trackingbut if i change the above code with this
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
try{
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
analytics.reportActivityStart(this);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
我可以看到一位用户在实时活动
I can see that one user is active in Realtime
GoogleAnalyticsConfig
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.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID ="UA-XXXXXXXX-1";
public synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = analytics.newTracker(PROPERTY_ID);
t.enableAutoActivityTracking(true);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
谁能告诉我为什么跟踪器不起作用?不仅是实时的,而且在我包含跟踪器的所有屏幕中,即使在 48 小时后,它也没有显示在 Google Analytics 的应用程序概览部分.
Can any one tell me why tracker is not working?Also not only realtime but also in all the screens where i have included the tracker its not showing in App Overview section of Google Analytics ,even after 48 hrs.
推荐答案
在res文件夹中创建xml文件夹,包含以下三个xml文件
in res folder create xml folder with below three xml file
app_tracker.xml
app_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Replace placeholder ID with your tracking ID -->
<string name="ga_trackingId">XX-XXXXXXXX-X</string>
<bool name="ga_autoActivityTracking">true</bool>
<bool name="ga_reportUncaughtExceptions">true</bool>
<string name="ga_appName">Serivce</string>
<string name="ga_appVersion">1.1.3</string>
<bool name="ga_debug">true</bool>
<item name="ga_dispatchPeriod" format="integer" type="integer">120</item>
<string name="ga_sampleFrequency">90</string>
<bool name="ga_anonymizeIp">true</bool>
<bool name="ga_dryRun">false</bool>
<!-- Percentage of events to include in reports -->
<string name="ga_sampleFrequency">100.0</string>
<!-- How long a session exists before giving up -->
<integer name="ga_sessionTimeout">-1</integer>
<string name="com.example.ui.MainActivity">MainActivity</string>
ecommerce_tracker.xml
ecommerce_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">60</integer>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">XX-XXXXXXX-1</string>
</resources>
global_tracker.xml
global_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">XX-XXXXXXX-1</string>
<integer name="ga_sessionTimeout">300</integer>
<bool name="ga_autoActivityTracking">true</bool>
<!-- the Local LogLevel for Analytics -->
<string name="ga_logLevel">verbose</string>
<!-- how often the dispatcher should fire -->
<integer name="ga_dispatchPeriod">30</integer>
<!-- Treat events as test events and don't send to google -->
<bool name="ga_dryRun">false</bool>
<screenName name="com.example.ui.MainActivity">MainActivity</screenName>
创建java文件AnalyticsSampleApp.java 并在menifest 中注册此文件,就像在应用程序标签中使用android:name="com.example.ui.AnalyticsSampleApp"
create java file withAnalyticsSampleApp.java and register this file in menifest like in application tag with android:name="com.example.ui.AnalyticsSampleApp"
public class AnalyticsSampleApp extends Application {
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "XX-XXXXXX-1";
public static int GENERAL_TRACKER = 0;
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.
ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}
public HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public AnalyticsSampleApp() {
super();
}
public 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);
}
}
在 MainActivity onCreate 下面放
in MainActivity onCreate put below
Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName(TAG);
t.send(new HitBuilders.AppViewBuilder().build());
这篇关于谷歌分析跟踪器不适用于安卓应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!