GooglePlay谷歌商店安装来源追踪INSTALL_REFERRER

理解

1.用户通过手机浏览器点击带有referrer的Url(如https://play.google.com/store/apps/details?id=com.xxx.xxx&referrer=test) (正常此网页下方在Play商店打开)
2.Google Play会记录附带的参数,(Finsky: [1] ifa.run(26): Capture referrer for com.xxx.xxx)
3.待用户下载安装之后,用户首次安装打开App时,Google Play将参数广播到App(Finsky: [1] ifb.run(12): Delivered referrer for com.xxx.xxx)。

代码实现(方式一,据说是Play Store 8.几之前才支持,未进行验证)

新建个类并继承BroadcastReceiver,用于接收referrer

1
2
3
4
5
6
7
8
9
10
11
public class InstallReferrerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String referrer = intent.getStringExtra("referrer");

Logger.e("install referrer:" + referrer);
if (referrer != null && referrer.length() > 0) {

}
}
}

AndroidManifest.xml

1
2
3
4
5
6
7
8
<receiver
android:name=".InstallReferrerReceiver"
android:exported="true"
android:permission="android.permission.INSTALL_PACKAGES" >
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>

代码实现(方式二)
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
public class InstallReferrerUtil {

private final static String INSTALL_REFERRER = "com.android.vending.INSTALL_REFERRER";

public static void setup(Context context) {
InstallReferrerClient client = InstallReferrerClient.newBuilder(context)
.build();
client.startConnection(new InstallReferrerStateListener() {
@Override
public void onInstallReferrerSetupFinished(int responseCode) {
if (responseCode == InstallReferrerClient.InstallReferrerResponse.OK) {
try {
ReferrerDetails details = client.getInstallReferrer();
String referrer = details.getInstallReferrer();
if (!TextUtils.isEmpty(referrer)) {
SharedPrefUtils.getInstance().putJReferrer(referrer);
Intent referrerReceived = new Intent(INSTALL_REFERRER);
referrerReceived.putExtra("referrer", referrer);
//向App本身其他Receiver广播
Iterator var8 = context.getPackageManager().queryBroadcastReceivers(new Intent(INSTALL_REFERRER), 0).iterator();
while (var8.hasNext()) {
ResolveInfo var4 = (ResolveInfo) var8.next();
String var5 = referrerReceived.getAction();
if (var4.activityInfo.packageName.equals(context.getPackageName()) && INSTALL_REFERRER.equals(var5) && !this.getClass().getName().equals(var4.activityInfo.name)) {
BuglyLog.e("onReceive:class:", (new StringBuilder("trigger onReceive: class: ")).append(var4.activityInfo.name).toString() + "");
try {
((BroadcastReceiver) Class.forName(var4.activityInfo.name).newInstance()).onReceive(context, referrerReceived);
} catch (Throwable var6) {
BuglyLog.e("onReceive:class:", (new StringBuilder("error in BroadcastReceiver ")).append(var4.activityInfo.name).toString(), var6);
}
}
}
}
AndroidSchedulers.mainThread().createWorker().schedule(() -> CrashReport.postCatchedException(new Throwable("referral:" + referrer)));
} catch (RemoteException e) {
// omit exception
AndroidSchedulers.mainThread().createWorker().schedule(() -> CrashReport.postCatchedException(new Throwable("RemoteException:" + e.getMessage())));
}
}
}
@Override
public void onInstallReferrerServiceDisconnected() {
AndroidSchedulers.mainThread().createWorker().schedule(() -> CrashReport.postCatchedException(new Throwable("referral:onInstallReferrerServiceDisconnected")));
}
});
}
}

关键神坑解决:
很多童鞋弄完可能收到

utm_source=(not%20set)&utm_medium=(not%20set)
很明显Google那边做了什么不见得人的处理,导致没把referrer传递过来
我们通过链接分析会发现:

  • 1.用户访问:https://play.google.com/store/apps/details?id=com.xxx.xxx&referrer=test
  • 2.点击该页面下面的按钮”在PLAY商店应用中打开”
  • 3.然后细心点就会发现Google做了短链中转处理啊(处理就处理嘛,Google直接把&referrer=test弄没了,弄没了,心塞塞~)
    https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.xxx.xxx&ddl=1&pcampaignid=web_ddl_1

所以,想实现可以传referrer,要么对上述的链接进行处理,如:
https://play.app.goo.gl/?link=https://play.google.com/store/apps/details?id=com.xxx.xxx
将link后面的链接加上&referrer=test,然后UrlEncode一下
https://play.app.goo.gl/?link=https%3a%2f%2fplay.google.com%2fstore%2fapps%2fdetails%3fid%3dcom.xxx.xxx%26referrer%3dtest

不然或者用这种链接
market://details?id=com.xxx.xxx&referrer=test

最后,你终于就能看到String referrer = details.getInstallReferrer();获取到referrer了(当然你还得先找梯子上架到Google Play才能验证到)。