简体   繁体   English

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

[英]How to test install referrer with Google's installreferrer library?

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.有很多示例如何测试“默认”检测安装引用的方式,但没有示例如何测试com.android.installreferrer:installreferrer库。

Examples like像这样的例子

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

do not work because we don't know receiver path.不起作用,因为我们不知道receiver路径。 So how to test it?那么如何测试呢?

With the InstallReferrerClient , there doesn't seem to be any BroadcastReceiver registered in the AndroidManifest.xml .使用InstallReferrerClient ,似乎没有在AndroidManifest.xml中注册任何BroadcastReceiver 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";

The client receives referrer utm_source=google-play&utm_medium=organic upon manual install.客户端在手动安装时收到推荐人utm_source=google-play&utm_medium=organic There is no BroadcastReceiver exposed (but the InstallReferrerService should have one).没有暴露BroadcastReceiver (但InstallReferrerService应该有一个)。

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

The documentation also states:该文档还指出:

The install referrer information will be available for 90 days and won't change unless the application is reinstalled.安装推荐人信息将在 90 天内可用,除非重新安装应用程序,否则不会更改。 To avoid unnecessary API calls in your app, you should invoke the API only once during the first execution after install.为避免在您的应用程序中调用不必要的 API,您应该在安装后的第一次执行期间仅调用一次 API。 Your app can listen to the system broadcast Intent.ACTION_PACKAGE_FIRST_LAUNCH to identify the app's first execution.您的应用可以通过收听系统广播Intent.ACTION_PACKAGE_FIRST_LAUNCH来识别应用的首次执行。


So this should be an intent-filter for action Intent.ACTION_PACKAGE_FIRST_LAUNCH , which subsequently connects the InstallReferrerClient to the InstallReferrerService .所以这应该是action Intent.ACTION_PACKAGE_FIRST_LAUNCHintent-filter ,它随后将InstallReferrerClient连接到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.无法使用adb触发Intent.ACTION_PACKAGE_FIRST_LAUNCH ,因为它会过滤“受保护的广播操作字符串”,因此它可能仅在从 Play 商店安装时触发。

The implementation, according to the documentation, might look alike:根据文档,实现可能看起来像:

AndroidManifest.xml : 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 : 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()");
    }
}

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

You can test install referrer by setting up internal test version of your app in Play Market console.您可以通过在 Play Market 控制台中设置应用的内部测试版本来测试安装引荐来源网址。 After that, use modified link from Play Market URL Builder.之后,使用来自 Play Market URL Builder 的修改后的链接。

I've used physical device via USB.我通过 USB 使用了物理设备。

1 put the app on Play Store approved 1将应用程序放在 Play 商店批准

2 Most important step: build right link on eg I used only last value . 2最重要的一步:建立正确的链接, 例如我只使用了最后一个值 Also important under Ad Network field pickup Custom , otherwize it will work on tests but wont when production w/o specified ad networkAd Network字段拾取Custom下也很重要,否则它将在测试中工作,但在没有指定广告网络的生产时不会

3 Send the link to email on the phone 3将链接发送到手机上的电子邮件

4 Click on the link just emailed. 4单击刚刚通过电子邮件发送的链接。 It runs Play Store (Don't Install from play Store)它运行Play 商店(不要从 Play 商店安装)

5 Add what's needed to the main activity (dont use depricated and outdated docs). 5需要的内容添加到主要活动中(不要使用过时和过时的文档)。 Launch with Android Studio the app just from Run button.使用Android Studio仅从运行按钮启动应用程序。 Press Home to turn app out the screen and press Recent button remove the app swipingHome将应用程序关闭屏幕并按最近按钮删除应用程序滑动

6 Play store "Install" button (now) should look as "Open" Tap the button. 6 Play 商店“安装”按钮(现在)应该看起来像“打开” 点击按钮。 Now you can handle response.getInstallReferrer() eg to see that in logs现在您可以处理response.getInstallReferrer()例如在日志中查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM