android-GooglePlay安装来源追踪PlayInstallReferrer

android-PlayInstallReferrer来源追踪, 也就是 Google Play 安装来源追踪.


前篇


接入

  1. 模块级 build.gradle 引入库 installreferrer

    1
    2
    3
    4
    dependencies {
    ...
    implementation 'com.android.installreferrer:installreferrer:1.1'
    }
  2. GoogleReferrerHelper.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    package com.purestlake.vivo;

    import android.content.Context;
    import android.os.RemoteException;
    import android.util.Log;

    import com.android.installreferrer.api.InstallReferrerClient;
    import com.android.installreferrer.api.InstallReferrerClient.InstallReferrerResponse;
    import com.android.installreferrer.api.InstallReferrerStateListener;
    import com.android.installreferrer.api.ReferrerDetails;

    import org.json.JSONObject;

    import java.util.HashMap;
    import java.util.Map;

    public class GoogleReferrerHelper {
    private static GoogleReferrerHelper instance = null;

    public static GoogleReferrerHelper getIns() {
    if (instance == null) {
    instance = new GoogleReferrerHelper();
    }
    return instance;
    }

    private static final String TAG = "--- ReferrerHelper";
    private InstallReferrerClient mReferrerClient;

    public void start(Context context) {
    Log.d(TAG, "start");
    if (mReferrerClient != null) {
    end();
    }
    mReferrerClient = InstallReferrerClient.newBuilder(context).build();
    mReferrerClient.startConnection(new InstallReferrerStateListener() {
    @Override
    public void onInstallReferrerSetupFinished(int responseCode) {
    Log.d(TAG, String.format("onInstallReferrerSetupFinished, responseCode: %d", responseCode));
    switch (responseCode) {
    case InstallReferrerResponse.OK:
    // Connection established.
    getArgs();
    break;
    case InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
    // API not available on the current Play Store app.
    break;
    case InstallReferrerResponse.SERVICE_UNAVAILABLE:
    // Connection couldn't be established.
    break;
    }
    }

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

    public void getArgs() {
    try {
    ReferrerDetails response = mReferrerClient.getInstallReferrer();
    String referrerUrl = response.getInstallReferrer();
    long referrerClickTime = response.getReferrerClickTimestampSeconds();
    long appInstallTime = response.getInstallBeginTimestampSeconds();
    boolean instantExperienceLaunched = response.getGooglePlayInstantParam();

    Map<String, Object> args = new HashMap<>();
    args.put("referrerUrl", referrerUrl);
    args.put("referrerClickTime", referrerClickTime);
    args.put("installTime", appInstallTime);
    args.put("instantExperienceLaunched", instantExperienceLaunched);
    Log.d(TAG, String.format("--- args: %s", new JSONObject(args).toString()));
    // end();
    } catch (RemoteException e) {
    e.printStackTrace();
    }
    }

    public void end() {
    if (mReferrerClient != null) {
    mReferrerClient.endConnection();
    mReferrerClient = null;
    }
    }
    }
  3. 在 MainActivity 的 onCreate 中调用

    1
    GoogleReferrerHelper.getIns().start(this);

测试

可以在不上架的情况下, 测试 referrer 代码是否生效

测试包名: com.aaa.bbb, 此包名必须在 Google Play 存在 (可以不是自己的 app, 用别人的可以测试)

测试连接: https://play.google.com/store/apps/details?id=com.aaa.bbb&referrer=arg1%3Daaa%26arg2%3Dbbb%26arg3%3Dccc (referrer 后面的参数要用 url encode 一下, 才能获取到全部参数, 参考官网文档都是 url encode 过的 https://developers.google.com/app-conversion-tracking/third-party-trackers/android?hl=zh-cn)

  1. 跳转到 Google Play 商店. 有两种方式

    1. 链接跳转. 给自己发个邮件, 内容里带上 测试连接, 然后用 gmail 应用打开看邮件, 点击连接 会直接跳转到 Google Play

    2. 利用另一个测试 app, 调用 api 跳转到 Google Play

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      public static boolean gotoGooglePlay(Context context, String url) {
      try {
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
      intent.setPackage("com.android.vending"); //这里对应的是谷歌商店,跳转别的商店改成对应的即可
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      if (intent.resolveActivity(context.getPackageManager()) != null) {
      context.startActivity(intent);
      return true;
      }
      } catch (Exception e) {
      e.printStackTrace();
      }
      return false;
      }

      Tools.gotoGooglePlay(this, "market://details?id=com.aaa.bbb&referrer=arg1%3Daaa%26arg2%3Dbbb%26arg3%3Dccc");
  2. 可以捕获到的信息

    1
    --- args: {"instantExperienceLaunched":false,"installTime":0,"referrerClickTime":1595400534,"referrerUrl":"arg1%3Daaa%26arg2%3Dbbb%26arg3%3Dccc"}

    如果 referrer 后面的参数没有 url encode, 如: arg1=aaa&arg2=bbb&arg3=ccc, 将只能获取到第一个参数 arg1=aaa

    貌似只能捕获到一次, 之后多次测试捕获到的都是默认值

    1
    {"instantExperienceLaunched":false,"installTime":0,"referrerClickTime":0,"referrerUrl":"utm_source=google-play&utm_medium=organic"}

其他测试结果

从 Google Play 上下载实测

  • 有 url encode:

    1
    2
    3
    4
    5
    https://play.google.com/store/apps/details?id=com.aaa.bbb&referrer=myparam%3Dabc%26gclid%3D123%26googleparam1%3D456%26googleparam2%3D789
    --- txt: myparam=abc&gclid=123&googleparam1=456&googleparam2=789

    https://play.google.com/store/apps/details?id=com.aaa.bbb&referrer=pid%3D1054%26agent%3Dz1
    --- txt: pid=1054&agent=z1

    可以获取到所有参数

  • 无 url encode:

    1
    2
    https://play.google.com/store/apps/details?id=com.aaa.bbb&referrer=pid=1054&agent=z1
    --- txt: pid=1054

    只获取到第一个参数


获取 归因参数 的条件

  • 只有在点击跳转时直接跳到 Google Play 商店应用 下载 app, 才能获取到 referrer 参数.

    如果是先跳转到 浏览器 的 Google Play 商店, 再点击 打开商店应用 跳转到 Google Play 商店应用 下载 app, 就会丢失 referrer 参数.

    如果是使用 adjust 第三方统计的话, 即使是先跳 浏览器, 再跳 gp 商店应用, 也不会丢失 归因数据.