7

我已经根据本指南实施了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。另外,我尝试了一些类似本教程中的应用程序演示,但它不起作用。我的设备没有收到任何额外内容。

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

4

2 回答 2

3

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

我通过以下方式获得了自定义广播中的refferels:

Bundle extras = intent.getExtras();
String referrerString = extras.getString("referrer");
于 2015-01-06T15:53:42.610 回答
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
于 2017-03-23T03:04:52.933 回答