我已经根据本指南实施了Google Analytics Campaign Measurement。然后我想按照本指南测试一切是否正常。
我在AndroidManifest.xml上添加了这些:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data android:name="com.google.android.gms.analytics.globalConfigResource"
android:resource="@xml/global_tracker" />
<!-- Used for Google Play Store Campaign Measurement-->;
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
并将这些放在MainActivity.java上:
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.
}
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("UA-xxxxxxxx-1")
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
: analytics.newTracker("UA-xxxxxxxx-1");
mTrackers.put(trackerId, t);
} return mTrackers.get(trackerId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleAnalytics.getInstance(this).getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
// Get tracker.
Tracker t = getTracker(TrackerName.APP_TRACKER);
// Set screen name.
t.setScreenName("Test Track");
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
}
当我运行以下命令时:
adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.example.gatestapp/com.google.android.gms.analytics.CampaignTrackingReceiver --es "referrer" "utm_source=testSource&utm_medium=testMedium&utm_term=testTerm&utm_content=testContent&utm_campaign=testCampaign"
我得到了这个回应:
Broadcast completed: result=0
这在我的 Logcat 上:
I/GAV4﹕ Thread[GAThread,5,main]: Campaign found: utm_source=testSource
但我不知道如何将utm_source收集到一个字符串中,所以我可以拥有“testSource”值。
所以,我尝试使用自定义接收器:
public class CustomReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
handleIntent(context, intent);
Log.d("YES", "IT WORKS!!");
new CampaignTrackingReceiver().onReceive(context, intent);
}
// Handle the intent data
public void handleIntent(Context context, Intent intent) {
String referrer = intent.getStringExtra("referrer");
Log.d("YEES", "IT WORKS!!!");
}
}
将我在 AndroidManifest.xml 上的接收器标记更改为:
<receiver android:name=".app.service.CustomReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
并使用以下命令更改adb shell命令:
adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.example.gatestapp/.app.service.CustomReceiver --es "referrer" "utm_source=testSource&utm_medium=testMedium&utm_term=testTerm&utm_content=testContent&utm_campaign=testCampaign"
广播是成功的,但我的CustomReceiver什么也没收到!Logcat 上没有“YES”或“YEES”。我的谷歌分析也没有检测到命中。我的方法有什么问题吗?我如何收集推荐人额外内容?
编辑:
我已经使用. SharedPreferences
但是,当我在 Play Store 上发布我的应用程序时,似乎缺少引荐来源网址。我确信我做的一切都是正确的,因为我使用的是URL Builder。另外,我尝试了一些类似本教程中的应用程序演示,但它不起作用。我的设备没有收到任何额外内容。
我做错了吗?或者,我错过了什么?