如何使用Google的installreferrer库测试安装引荐来源网址? [英] How to test install referrer with Google's installreferrer library?

查看:1231
本文介绍了如何使用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?

推荐答案

使用

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,随后将InstallReferrerClient连接到InstallReferrerService.一个人不能用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库测试安装引荐来源网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆