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.然后寫個(gè)工具類,代碼如下:
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, 然后寫個(gè)廣播用來接收Refrrer的參數(shù) ,這里我是接收然后傳給自己寫的廣播
這里還需要解析下參數(shù)
然后需要?jiǎng)?chuàng)建引用渠道來源的地址坝撑,類似??utm_source%3Dgoogle%26utm_medium%3Dcpc 這樣的參數(shù),不過可能很多時(shí)候參數(shù)獲取是不正確的粮揉,可能是utm_source =(not%20set)&utm_medium =(not%20set)? 這里的原因可能是Google賬號(hào)下載過引用渠道來源的app,也可能是需要手機(jī)下載個(gè)google play的商店巡李。