如何在 Android 应用程序中获取 Google Play referer

How to get Google Play referer in an Android application

我们正在开发 Android 应用程序。我们想跟踪我们的活动。基本上,我们想知道我们的用户是从哪里到达 Google play 的,方法是在 Google Play URL 中输入一个词与我们的合作伙伴并在应用程序中检索该词然后发送它到我们的服务器。

我们已经在我们的应用程序中安装了 Google Analytics,我们能够在 Google 统计面板上跟踪用户在应用程序中执行的操作。但是我们如何使用它来实现我们真正想要的呢?我们确实需要 link 我们的用户数据库中包含该词。

我听说过INSTALL_REFERER,但我真的不知道怎么用

您正在寻找 Campaign Measurement。在文档中,它讨论了使用 INSTALL_REFERRER 如何帮助您确定哪个来源将用户发送到 Google Play 商店中的您的应用。

这就像在您的 AndroidManifest 中放置一个 receiver 并修改您应用的 Google 播放 URL 一样简单。

来自文档:

Google Play Campaign Attribution

Google Play Campaign Measurement allows you to see which campaigns and traffic sources are sending users to download your app from the Google Play Store. It is recommended that all developers implement Google Play Store Campaign Measurement.

Implementing Google Play Campaign Attribution

When your app is downloaded from Google Play Store, the Play Store app broadcasts an INSTALL_REFERRER intent to your app during installation. This intent contains the value of the referrer parameter of the link used to reach your app's Google Play Store page, if one was present.

To attribute an app download to a campaign, you must add a referrer parameter to any links that point to Google Play Store, and add a BroadcastReceiver to your app to receive and set the campaign information contained in the intent on your Google Analytics tracker.

It is recommended that most developers use the BroadcastReceiver provided with the SDK. To implement Google Play Store Campaign Measurement using the included receiver:

1. Add the Google Analytics receiver to your AndroidManifest.xml file. To add the Google Analytics receiver to the manifest, copy and paste the following markup:

    <application>
    <!-- Used for Google Play Store Campaign Measurement-->
    <receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>
    <service android:name="com.google.android.gms.analytics.CampaignTrackingService"
        android:enabled="true"
        android:exported="false" />
</application>
  1. Add Google Analytics Campaign Parameters to Google Play URLs

    Next, add a referrer parameter to any URLs that will be linking directly to Google Play Store and set the value of that parameter to a string of Google Analytics campaign parameters that describe the source, as in this example:

    https://play.google.com/store/apps/details?id=com.example.application &referrer=utm_source%3Dgoogle %26utm_medium%3Dcpc %26utm_term%3Drunning%252Bshoes %26utm_content%3Dlogolink %26utm_campaign%3Dspring_sale

To learn how to build a campaign parameter strings, use the Google Play URL Builder, or consult the Campaign Parameters reference section.

Testing Google Play Campaign Attribution

To verify that your Google Play Campaign Measurement implementation is working as expected before publishing your app, use the Testing Google Play Campaign Attribution Solution Guide.

另请参阅此

尝试播放安装引荐库

It is a new, easy to use and reliable way to securely retrieve install referral content

https://android-developers.googleblog.com/2017/11/google-play-referrer-api-track-and.html

Also, switching to a new referrer API allows for deeper insights into the user conversion funnel, secures the Google Play Store referrer and finally (and most importantly), the additional data provided allows for the complete eradication of click injections.

(来源:https://www.adjust.com/blog/eliminating-click-injections-with-google-play-referrer-api/

示例:

final InstallReferrerClient referrerClient = InstallReferrerClient.newBuilder(YourActivity.this).build();
referrerClient.startConnection(new InstallReferrerStateListener() {

        @Override
        public void onInstallReferrerSetupFinished(int responseCode) {
            switch (responseCode) {
                case InstallReferrerClient.InstallReferrerResponse.OK:

                    try {
                        ReferrerDetails response = referrerClient.getInstallReferrer();
                        String installReferrer = response.getInstallReferrer();

                        // handle referrer string

                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;

                case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                    // API not available on the current Play Store app
                    break;
                case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                    // Connection could not be established
                    break;
            }
        }

        @Override
        public void onInstallReferrerServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
});

将以下行添加到应用的 build.gradle 文件的依赖项部分:

dependencies {
    ...
    compile 'com.android.installreferrer:installreferrer:1.0'
}