Android - 使用Google Analytics v4广告系列衡量标准

时间:2014-12-02 06:26:25

标签: android android-intent google-analytics android-service google-analytics-v4

我已根据this guide实施了 Google Analytics广告系列衡量指标。然后我想按照this guide测试一切是否正常。

我已在 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 收集到String中,因此我可以获得“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”。此外,我的Google Analytics上未检测到任何匹配。我的方法有什么问题吗? 如何收集推荐人附加内容?

修改

我已成功使用SharedPreferences收集引荐来源。但是,当我在Play商店发布我的应用时,推荐人额外内容似乎缺少。我确信我做的一切都正确,因为我正在使用URL Builder。此外,我尝试了一些应用程序演示,如this tutorial中的那个,但它不起作用。我的设备没有收到任何额外内容。

我做错了吗?或者,我错过了什么?

2 个答案:

答案 0 :(得分:3)

首先,您的包名是“com.example.gatestapp”吗?因为那是你在adb命令中写的。 其次,您是否在清单中的“CampaignTrackingReceiver”之前放置了“app.service.CustomReceiver”?也许意图是由CampaignTrackingReceiver拦截而不是你的接收者。

我通过以下方式获取自定义广播中的引用:

Bundle extras = intent.getExtras();
String referrerString = extras.getString("referrer");

答案 1 :(得分:1)

我发现你的问题对于复制该功能非常有用。

问题看起来像是包的名称,我遵循完全相同的程序。

android_manifest.xml

<receiver
        android:name="co.tpaga.poc.referer.poc_installreferer.CustomInstallRefererReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>

adb命令是:

echo 'am broadcast \
-a com.android.vending.INSTALL_REFERRER \
-n "co.tpaga.poc.referer.poc_installreferer/co.tpaga.poc.referer.poc_installreferer.CustomInstallRefererReceiver" \
--es "referrer" \
  "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name&tappsi_uuid=04442-123123-213213-2323"; \                                     
exit' | ./adb shell

logcat出口是:

03-22 21:58:04.469 22340-22340/co.tpaga.poc.referer.poc_installreferer D/YEES: IT WORKS!!!utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name
03-22 21:58:04.469 22340-22340/co.tpaga.poc.referer.poc_installreferer D/YES: IT WORKS!!
03-22 21:58:04.477 22340-22394/co.tpaga.poc.referer.poc_installreferer D/GAv4: Received installation campaign: content=test_content, keyword=test_term, medium=test_medium, name=test_name, source=test_source