如何使用 Google 的 installreferrer 库测试安装引荐来源网址?

How to test install referrer with Google#39;s installreferrer library?(如何使用 Google 的 installreferrer 库测试安装引荐来源网址?)
本文介绍了如何使用 Google 的 installreferrer 库测试安装引荐来源网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多示例如何测试默认"方式检测安装引荐来源网址,但没有示例如何测试 com.android.installreferrer:installreferrer 库.

There are lots of examples how to test "default" way of detecting install referrer, but there is not example how to test com.android.installreferrer:installreferrer library.

例如

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER 
                       -n your.package.name/path.to.receiver --es referrer
                       --es referrer "EXTRA_STRING_VALUE"

不工作,因为我们不知道 receiver 路径.那么如何测试呢?

do not work because we don't know receiver path. So how to test it?

推荐答案

使用InstallReferrerClient,AndroidManifest.xml中似乎没有注册任何BroadcastReceiver.该库只是绑定到系统的安装引用服务...

With the InstallReferrerClient, there doesn't seem to be any BroadcastReceiver registered in the AndroidManifest.xml. The library just binds to the system's install referrer service ...

private static final String SERVICE_PACKAGE_NAME = "com.android.vending";
private static final String SERVICE_NAME = "com.google.android.finsky.externalreferrer.GetInstallReferrerService";
private static final String SERVICE_ACTION_NAME = "com.google.android.finsky.BIND_GET_INSTALL_REFERRER_SERVICE";

客户端在手动安装时收到推荐人 utm_source=google-play&utm_medium=organic.没有暴露 BroadcastReceiver(但 InstallReferrerService 应该有一个).

The client receives referrer utm_source=google-play&utm_medium=organic upon manual install. There is no BroadcastReceiver exposed (but the InstallReferrerService should have one).

原始 Intent Bundle 的键是:install_referrerreferrer_click_timestamp_secondsinstall_begin_timestamp_seconds 如果你想尝试模拟它 - 但 onInstallReferrerSetupFinished() 回调将间接传递结果.

The keys of the raw Intent Bundle are: install_referrer, referrer_click_timestamp_seconds and install_begin_timestamp_seconds if you want to try emulating it - but the onInstallReferrerSetupFinished() callback will deliver the result indirectly.

文档还指出:

安装引荐来源网址信息将在 90 天内可用,除非重新安装应用程序,否则不会更改.为避免在您的应用中调用不必要的 API,您应该在安装后的第一次执行期间只调用一次 API. 您的应用可以通过监听系统广播 Intent.ACTION_PACKAGE_FIRST_LAUNCH 来识别应用的第一次执行.

The install referrer information will be available for 90 days and won't change unless the application is reinstalled. To avoid unnecessary API calls in your app, you should invoke the API only once during the first execution after install. Your app can listen to the system broadcast Intent.ACTION_PACKAGE_FIRST_LAUNCH to identify the app's first execution.

<小时>

所以这应该是 action Intent.ACTION_PACKAGE_FIRST_LAUNCHintent-filter,随后连接 InstallReferrerClientInstallReferrerService.无法使用 adb 触发 Intent.ACTION_PACKAGE_FIRST_LAUNCH,因为它会过滤受保护的广播操作字符串",因此它可能仅在从 Play 商店安装时触发.


So this should be an intent-filter for action Intent.ACTION_PACKAGE_FIRST_LAUNCH, which subsequently connects the InstallReferrerClient to the InstallReferrerService. One cannot trigger Intent.ACTION_PACKAGE_FIRST_LAUNCH with adb, because it filters for a "protected broadcast action string", therefore it might only be triggered when installing from Play Store.

根据文档,实现可能看起来很相似:

The implementation, according to the documentation, might look alike:

AndroidManifest.xml:

<receiver
    android:name=".receiver.PackageStatusReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH"/>
    </intent-filter>
</receiver>

PackageStatusReceiver.java:

public class PackageStatusReceiver extends BroadcastReceiver implements InstallReferrerStateListener {

    protected static final String LOG_TAG = PackageStatusReceiver.class.getSimpleName();

    private InstallReferrerClient referrerClient;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction() != null) {
            if(intent.getAction().equals(Intent.ACTION_PACKAGE_FIRST_LAUNCH)) {
                this.referrerClient = InstallReferrerClient.newBuilder(context).build();
                this.referrerClient.startConnection(this);
            }
        }
    }

    @Override
    public void onInstallReferrerSetupFinished(int responseCode) {
        switch (responseCode) {
            case InstallReferrerClient.InstallReferrerResponse.OK:
                Log.d(LOG_TAG, "InstallReferrer Response.OK");
                try {
                    ReferrerDetails response = referrerClient.getInstallReferrer();
                    String referrer = response.getInstallReferrer();
                    long clickTimestamp = response.getReferrerClickTimestampSeconds();
                    long installTimestamp = response.getInstallBeginTimestampSeconds();
                    Log.d(LOG_TAG, "InstallReferrer " + referrer);
                    referrerClient.endConnection();
                } catch (RemoteException e) {
                    Log.e(LOG_TAG, "" + e.getMessage());
                }
                break;
            case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                Log.w(LOG_TAG, "InstallReferrer Response.FEATURE_NOT_SUPPORTED");
                break;
            case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                Log.w(LOG_TAG, "InstallReferrer Response.SERVICE_UNAVAILABLE");
                break;
            case InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED:
                Log.w(LOG_TAG, "InstallReferrer Response.SERVICE_DISCONNECTED");
                break;
            case InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR:
                Log.w(LOG_TAG, "InstallReferrer Response.DEVELOPER_ERROR");
                break;
        }
    }

    @Override
    public void onInstallReferrerServiceDisconnected() {
        Log.w(LOG_TAG, "InstallReferrer onInstallReferrerServiceDisconnected()");
    }
}

要对此进行测试,您需要指向 Play 商店的引荐来源链接,然后通过它们安装包...否则只会记录默认引荐来源网址(除了正确实施客户端时甚至无法触发意图之外).

To test this, you'd need referrer links to the Play Store and then install the package through them... else only the default referrer will be logged (besides the intent cannot even be triggered, when properly implementing the client).

这篇关于如何使用 Google 的 installreferrer 库测试安装引荐来源网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)