我是Google Analytics的新手。初始化跟踪器时出现问题。我想在EasyTracker.getInstance(this);
方法中使用onCreate()
初始化跟踪器,但是当我运行它并访问已设置跟踪器的菜单时,我在logcat中收到了警告消息,即"missing tracking id (&id) parameter."
。但是,如果我使用GoogleAnalytics.getInstance(this).getTracker("UA-xxxxxxxx-y");
初始化跟踪器,则不会显示该消息。我在EasyTracker.getInstance(this).activityStart(this);
方法中设置了onStart()
,在EasyTracker.getInstance(this).activityStop(this);
方法中设置了onStop()
。
这是我的analytics.xml。顺便说一句,我已经设定了身份证。
<?xml version="1.0" encoding="utf-8"?>
<!-- The comicjp app Analytics Tracking Id -->
<string name="ga_trackingId">UA-xxxxxxxx-y</string>
<!-- Enable automatic activity tracking -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- Enable automatic exception tracking -->
<bool name="ga_reportUncaughtException">true</bool>
<!-- Set the log level to verbose -->
<string name="ga_logLevel">verbose</string>
<!-- The interval of time after all the collected data
should be sent to the server, in seconds -->
<integer name="ga_dispatchPeriod">30</integer>
<!-- The screen name that will appear in reports -->
<string name="com.example.gaaplication.MainActivity">MainActivity</string>
如果我使用EasyTracker.getInstance(this);
,为什么始终会显示警告消息?如何正确使用EasyTracker.getInstance(this);
?
答案 0 :(得分:3)
在第4版中,我得到的是GAv4: Discarding hit. Missing tracking id parameter
,而不是:
GoogleAnalytics.getInstance(context).newTracker(R.xml.app_tracker);
我添加了
GoogleAnalytics.getInstance(context).newTracker(R.string.ga_trackingId);
请注意差异 xml.app_tracker vs string.ga_trackingId !
使用xml.app_tracker
配置文件!
答案 1 :(得分:1)
你需要在这里设置自己的身份
<!-- The comicjp app Analytics Tracking Id -->
<string name="ga_trackingId">place your tracking id here</string>
答案 2 :(得分:1)
对于谷歌分析,你需要在这里注册你的应用程序Google Analytics作为所有移动应用程序然后它会给你ga_trackingId id
现在将分析文件中的一个analytics.xml精细值文件夹放在内容
下面<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Replace placeholder ID with your tracking ID -->
<string name="ga_trackingId">UA-XXXXXXXX-X</string>
<!-- Enable automatic activity tracking -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- Enable automatic exception tracking -->
<bool name="ga_reportUncaughtExceptions">true</bool>
<string name="ga_appName">Location Serivce</string>
<string name="ga_appVersion">1.1.2</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>
<string name="com.example.HomeActivity">HomeActivity</string>
<string name="com.example.SettingActivity">SettingActivity</string>
现在在家庭活动中放置以下活动显示代码
private Tracker tracker;
private String TAG_Tracker="HomeActivity";
MyApp.getGaTracker().set(Fields.SCREEN_NAME, TAG_Tracker);
tracker = GoogleAnalytics.getInstance(this).getTracker(MyApp.GA_PROPERTY_ID);
HashMap<String, String> hitParameters = new HashMap<String, String>();
hitParameters.put(Fields.HIT_TYPE, "ActivityView");
hitParameters.put(Fields.SCREEN_NAME, TAG_Tracker);
tracker.send(hitParameters);
//for button click and other event fire you need to put below code:
public void onClick(View v) {
tracker.send(MapBuilder.createEvent("UI", "OnClick", "btnAdd", null).build());
}
现在MyApp.java
public class MyApp extends Application {
private static GoogleAnalytics mGa;
private static Tracker mTracker;
/*
* Google Analytics configuration values.
*/
// Placeholder property ID.
public static final String GA_PROPERTY_ID = "UA-XXXXXXXX-X";
// Dispatch period in seconds.
private static final int GA_DISPATCH_PERIOD = 30;
// Prevent hits from being sent to reports, i.e. during testing.
private static final boolean GA_IS_DRY_RUN = false;
// GA Logger verbosity.
private static final LogLevel GA_LOG_VERBOSITY = LogLevel.INFO;
// Key used to store a user's tracking preferences in SharedPreferences.
private static final String TRACKING_PREF_KEY = "trackingPreference";
/*
* Method to handle basic Google Analytics initialization. This call will
* not block as all Google Analytics work occurs off the main thread.
*/
@SuppressWarnings("deprecation")
private void initializeGa() {
mGa = GoogleAnalytics.getInstance(this);
mTracker = mGa.getTracker(GA_PROPERTY_ID);
// Set dispatch period.
GAServiceManager.getInstance().setLocalDispatchPeriod(GA_DISPATCH_PERIOD);
// Set dryRun flag.
mGa.setDryRun(GA_IS_DRY_RUN);
// Set Logger verbosity.
mGa.getLogger().setLogLevel(GA_LOG_VERBOSITY);
// Set the opt out flag when user updates a tracking preference.
SharedPreferences userPrefs = PreferenceManager.getDefaultSharedPreferences(this);
userPrefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(TRACKING_PREF_KEY)) {
GoogleAnalytics.getInstance(getApplicationContext()).setAppOptOut(sharedPreferences.getBoolean(key, false));
}
}
});
}
@Override
public void onCreate() {
super.onCreate();
initializeGa();
}
/*
* Returns the Google Analytics tracker.
*/
public static Tracker getGaTracker() {
return mTracker;
}
/*
* Returns the Google Analytics instance.
*/
public static GoogleAnalytics getGaInstance() {
return mGa;
}
}
你需要在像
这样的最明显的文件中定义这个myApp.java文件 <application
android:name="com.example.MyApp"
答案 3 :(得分:0)
我有一个类似的问题,非常奇怪,但我只是通过查看EasyTracker类的源代码来解决。它使用资源名称
来获取这样的内容 String s = mParameterFetcher.getString("ga_trackingId");
如果你正在使用包重命名的Android Maven插件,它会搞砸(稍后阅读:http://www.piwai.info/renaming-android-manifest-package/)...So以便修复它,你必须阅读链接+调用方法 EasyTracker.setResourcePackageName()强>
// ensures that EasyTracker uses the overrider package.
String appPackageName = getPackageNameAccordingToWhatIsWrittenInThatBlogPost();
// Log.i(TAG, "loading appPackageName: " + appPackageName);
EasyTracker.setResourcePackageName(appPackageName);
祝你好运!
答案 4 :(得分:0)
对于google analytics v4,您应该添加global_tracker.xml。我认为Google在他们的Github回购中错过了它。
<resources xmlns:tools="http://schemas.android.com/tools"
tools:ignore="TypographyDashes"
>
<integer name="ga_sessionTimeout">300</integer>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<bool name="ga_debug">true</bool>
<string name="ga_logLevel">verbose</string>
<string name="ga_dryrun">true</string>
<!-- The screen names that will appear in reports -->
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-xxxxxxx-1</string>
</resources>