Google Analytics获取广告渠道信息
最近公司的项目需要用Google Analytics来获取广告渠道信息,老大当时把该需求丢给我时,我也是一脸懵逼,摸索了好几天,才摸清一丢丢套路。想着以后可能还会用到,就随笔记下,好了,废话不多说了,come baby!
首先
-获取google-services.json放至src目录
-在项目级build.gradle下放入google-services,一定要是最新版本
dependencies {
classpath 'com.google.gms:google-services:3.1.0'
}
-在应用级build.gradle末尾加上
apply plugin: 'com.google.gms.google-services'
-在应用级build.gradle的dependencies中加上
compile 'com.google.android.gms:play-services-analytics:11.0.4'
compile 'com.google.android.gms:play-services-places:11.0.4'
compile 'com.google.android.gms:play-services-ads:11.0.4'
其次
在AndroidManifest.xml里面加上:
<receiver android:name=".receiver.PlayCampaignReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.EDIT"/>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<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" />
<receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.AnalyticsService" android:enabled="true" android:exported="false"/>
com.google.android.gms.analytics.CampaignTrackingReceiver
com.google.android.gms.analytics.CampaignTrackingService
com.google.android.gms.analytics.AnalyticsReceiver
com.google.android.gms.analytics.AnalyticsService
以上四个配置是Google官方需要配置的
PlayCampaignReceiver是我自定义用来接收GooglePlay通过CampaignTrackingReceiver中的<action android:name="com.android.vending.INSTALL_REFERRER" />的“referrer”
发送渠道消息,所以com.android.vending.INSTALL_REFERRER一定要写。
最后
在自定义的PlayCampaignReceiver中接收“referrer”参数
public class PlayCampaignReceiver extends BroadcastReceiver {
private static final String LOGTAG = "PlayCampaignReceiver";
/**
* anid:广告网络
* utm_source:广告系列来源(原始引荐来源,例如 google、citysearch、newsletter4)
* utm_medium:广告系列媒介(营销媒介,例如 cpc、banner、email)
* utm_term:广告系列字词(付费关键字,例如 running+shoes)
* utm_content:广告系列内容(广告特有的内容,用于区分不同的广告)
* utm_campaign:广告系列名称(产品、促销代码或标语)
*/
@Override
public void onReceive(Context context, Intent intent) {
new CampaignTrackingReceiver().onReceive(context, intent);
Log.i(LOGTAG, "onReceive intent=" + intent);
//处理referrer参数
String referrer = "";
Bundle extras = intent.getExtras();
if (extras != null) {
referrer = extras.getString("referrer");
// 得到相应的广告信息 是url 参数格式,需要解析
// 格式: utm_source=testSource&utm_medium=testMedium&utm_term=testTerm&utm_content=11
Toast.makeText(context, "谷歌的Referer is: " + referrer, Toast.LENGTH_SHORT).show();
}
}
}
测试
通过adb测试,在Android\sdk\platform-tools中打开cmd即可
adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n 你的包名/你的包名.receiver.PlayCampaignReceiver --es "referrer" "utm_source%3Dgoogle%26utm_medium%3Dbanner%26utm_term%3Drunning%252Bshoes%26utm_content%3Dtextlink%26utm_campaign%3Dspring_sale"
adb测试结果
D:\AppData\Local\Android\sdk\platform-tools>adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n 应用包名/应用包名.receiver.PlayCampaignReceiver --es referrer "'utm_source=testSource&utm_medium=testMedium&utm_term=testTerm&utm_content=testContent&utm_campaign=testCampaign'"
Broadcasting: Intent { act=com.android.vending.INSTALL_REFERRER cmp=应用包名/.receiver.PlayCampaignReceiver (has extras) }
Broadcast completed: result=0
打印logcat(需要真机接入USB才能打印)
adb logcat -v time -s GAv4
将应用发送到Google play测试
这个时候,你就需要Google的 Google Play 网址构建工具了,然后官网会告诉你怎么填写信息,如何测试。
注意:只有通过生成工具生成出来的二维码或者点击网址下载下来的应用才能获取渠道信息。当然这个信息就是你通过Google Play 网址构建工具生成出来的信息,只有在下载下来第一次启动应用的时候才会获取到,有时候会不稳定,可能获取不到也有可能,如果刷新了你刚生成的信息,也有可能获取不到。我自定义的广播一直获取不到,我就在应用进来的时候手动调用了一下广播,具体想怎么实现,看需求和个人吧。
Google Play测试结果
10-28 21:52:03.797 D/GAv4 (32036): Received installation campaign: adNetworkId=applovin, content=textlink, keyword= running+shoes, medium=banner, name=spring_sale, source=applovin
10-28 21:52:03.807 W/GAv4 (32036): Ignoring duplicate install campaign
10-28 21:52:40.027 W/GAv4 (32036): CampaignTrackingReceiver received unexpected intent without referrer extra
10-28 21:53:41.307 I/GAv4 (32459): Google Analytics 11.0.20 is starting up. To enable debug logging on a device run:
10-28 21:53:41.307 I/GAv4 (32459): adb shell setprop log.tag.GAv4 DEBUG
10-28 21:53:41.307 I/GAv4 (32459): adb logcat -s GAv4
结尾
好啦,终于哔哩啪啦写完了,这个功能,我也是测试了两三天,注意需要翻墙的哦。如果有和我一样有这样需求的同学,送给你们,也送给我自己。
Google play 网址构建工具
Google Analytics官网
将 Google Analytics(分析)添加到 Android 应用中
Google play服务