使用Google Analytics(分析)v4广告系列衡量

我已根据本指南实施Google Analytics(分析)广告系列衡量指标 。 然后,我想按照本指南测试一切是否正常。

我在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”值。

所以,我尝试使用Custom Receiver:

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。 此外,我尝试了一些应用演示,如本教程中的演示,但它不起作用。 我的设备没有收到任何附加内容。

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


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

我决定通过以下方式获得定制广播中的参考:

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

我发现你对复制该功能非常有用。

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

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
链接地址: http://www.djcxy.com/p/82769.html

上一篇: Using Google Analytics v4 Campaign Measurement

下一篇: Why doesn't my idea work in python2?