1.接入google play的Refrrer
implementation'com.google.android.gms:play-services-analytics:17.0.0'
implementation'com.google.android.gms:play-services-places:17.0.0'
在app的build.grade里引入,
2.然后写个工具类,代码如下:
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)) {
SpProvider.putJReferrer(referrer);
UploadUrl();
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)) {
Log.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) {
Log.e("onReceive:class:", (new StringBuilder("error in BroadcastReceiver ")).append(var4.activityInfo.name).toString(), var6);
}
}
}
}
client.endConnection();
}catch (RemoteException e) {
// omit exception
}
}
}
@Override
public void onInstallReferrerServiceDisconnected() {
}
});
}
在需要引入的地方传入context, 然后写个广播用来接收Refrrer的参数 ,这里我是接收然后传给自己写的广播
这里还需要解析下参数
然后需要创建引用渠道来源的地址,类似 utm_source%3Dgoogle%26utm_medium%3Dcpc 这样的参数,不过可能很多时候参数获取是不正确的,可能是utm_source =(not%20set)&utm_medium =(not%20set) 这里的原因可能是Google账号下载过引用渠道来源的app,也可能是需要手机下载个google play的商店。