Android谷歌商店安装来源追踪Play Install Re
项目上线到google市场,但是推广人员有在百度和头条上面投放广告,需要统计下载渠道,这个时候就需要使用Google Play 广告系列衡量功能
1.用户通过手机浏览器点击带有referrer的Url(如https://play.google.com/store/apps/details?id=com.xxx.xxx&referrer=test) (正常此网页下方在Play商店打开)
2.当您的应用在 Google Play 商店中被下载时,Play 商店应用会在您的应用安装期间向其广播一条 INSTALL_REFERRER Intent。如果将用户引荐到您应用的 Google Play 商店页面的链接中有 referrer 参数,此 Intent 就会包含这个参数的值。
为了将某次应用下载归因到某个广告系列,您需要在指向 Google Play 商店的所有链接中添加 referrer 参数,并在您的应用中添加一个 BroadcastReceiver,以便使用您的 Google Analytics(分析)跟踪器来接收并设置 Intent 中所包含的广告系列信息。
我们建议大多数开发者使用 SDK 中提供的 BroadcastReceiver。要使用此内置接收器实现 Google Play 商店广告系列衡量功能,请执行以下操作:
<application>
<!-- Used for Google Play Store Campaign Measurement-->
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.CampaignTrackingService"
android:enabled="true"
android:exported="false" />
</application>
接下来,请将 referrer 参数添加到直接指向 Google Play 商店的所有网址,并将该参数的值设置为用来描述来源的 Google Analytics(分析)广告系列参数字符串,如下例所示:
https://play.google.com/store/apps/details?id=com.example.application
&referrer=utm_source%3Dgoogle
%26utm_medium%3Dcpc
%26utm_term%3Drunning%252Bshoes
%26utm_content%3Dlogolink
%26utm_campaign%3Dspring_sale
要了解如何构建广告系列参数字符串,请使用 Google Play 网址构建工具,或参阅广告系列参数一节
测试,使用adb工具
adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n "com.XXX.XXXX/gw.com.android.ui.broadcast.InstallReferrerReceiver" --es "referrer" "utm_source%3Dbaidu%26utm_medium%3D111%26utm_term%3D222%26utm_content%3D333%26utm_campaign%3D444%26anid%3Dadmob"
说如果配置正确的话就会返回
Broadcasting: Intent { act=com.android.vending.INSTALL_REFERRER cmp=[包名]/[CampaignTrackingReceiver路径] (has extras) }
Broadcast completed: result=0
上线Google play获取参数
public class TrackingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/**
utm_source=google&utm_medium=cpc&utm_term=test1&utm_content=test2&utm_campaign=test3&anid=admob
* anid:广告网络
* utm_source:广告系列来源(原始引荐来源,例如 google、citysearch、newsletter4)
* utm_medium:广告系列媒介(营销媒介,例如 cpc、banner、email)
* utm_term:广告系列字词(付费关键字,例如 running+shoes)
* utm_content:广告系列内容(广告特有的内容,用于区分不同的广告)
* utm_campaign:广告系列名称(产品、促销代码或标语)
*/
String referrer = intent == null ? null : intent.getStringExtra("referrer");
Log.d("ga_referrer", referrer == null ? "null" : referrer);
com.google.android.gms.analytics.CampaignTrackingReceiver googleReceiver = new com.google.android.gms.analytics.CampaignTrackingReceiver();
googleReceiver.onReceive(context, intent);
但是注意:
以上方案有问题,据说是Play Store 8.几之前才支持,而且在Google2020年已经停止使用了,所以使用替代方案(Play Install Referrer API),这个方案在google分析文档中为测试方案,但是亲测可用:
注明:Google Play要求在2020年3月1日前切换到Play Install Referrer API
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)的情况
这里有几点要注意
1.需要使用vpn,能够正常从Google play下载其他app,保证手机已经开启了Google服务等.
2.测试的时候,最好使用之前没有下载过集成统计APP的账号.网上有人说可能获取不到渠道等信息.
3.如果还是不行,可以考虑使用非大陆的地区直接测试,亲测有效