title: android-GooglePlay安装来源追踪PlayInstallReferrer
categories: Android
tags: [android, referrer, googleplay]
date: 2020-07-22 15:28:59
comments: false
mathjax: true
toc: true

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


前篇

  • https://developer.android.com/google/play/installreferrer?hl=zh_CN
  • https://developer.android.com/google/play/installreferrer/library?hl=zh_CN#java

接入

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

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

    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 中调用

    GoogleReferrerHelper.getIns().start(this);

测试

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

  • 如何在Google Play商店发布之前测试google play referrer api? - https://www.thinbug.com/q/49127470

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

测试连接: https://play.google.com/store/apps/details?id=com.aaa.bbb&referrer=arg1=aaa&arg2=bbb&arg3=ccc

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

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

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

      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=aaa&arg2=bbb&arg3=ccc");
  2. 可以捕获到的信息

    --- args: {"instantExperienceLaunched":false,"installTime":0,"referrerClickTime":1595400534,"referrerUrl":"arg1=aaa"}

    不知道为啥 referrerUrl 字段只有 arg1=aaa, 而不是 arg1=aaa&arg2=bbb&arg3=ccc

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

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

更多相关文章

  1. 第12天android:短信发送+测试使用
  2. Android(安卓)uiautomator 使用入门官方教程
  3. Android复习笔记(3)-android下的junit
  4. [android]android自动化测试五之Robolectric
  5. 使用 Android(安卓)Studio 进行测试 (二) UI 测试
  6. Android(安卓)GMS认证总结01
  7. Android(安卓)Camera2 API 学习笔记2
  8. android cts测试失败项以及原因
  9. Android自动化测试之MonkeyRunner工具(六)

随机推荐

  1. Mac完整卸载Android Studio的方法
  2. 【Android 设计】:模式_ Android新特性
  3. ListView的一些属性
  4. android adb bundle下载
  5. Android之父Andy Rubin:生而Geek
  6. Android Instrumentation Test
  7. Android 7.0 SystemUI(2)--Multi-Window
  8. Android 读取U盘或SD卡中的所有.txt文件
  9. Android Studio——理解Intent和Intent F
  10. Android 开发中遇到的 bug(5)