Google Play作為Android最大的應用市場,也存在這推廣等常用的行為,那麼如何統計呢,Google Analytics SDK或者其他的SDK都提供了方法,實際上是可以不需要任何sdk,完全可以自己實現的。
原理是這樣的:當來自Play Store上的應用被安裝之後,Play Store會發送一個指定的廣播給被安裝的包,廣播的action為com.android.vending.INSTALL_REFERRER。內容資料類似於 utm_source%3Dgoooooogggggggggggggle%26utm_medium%3Dcpc%26utm_term%3Dandroid%252Bbrowser%26utm_content%3Dmaxthon%2520browser%2520for%2520android%26utm_campaign%3DYou%2520never%2520know%2520fast.
我們只需要接收這個廣播處理即可。
首先在manifest中增加receiver來接收
<receiver android:name=".AnalyticsReceiver" android:exported="true" > <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" /> </intent-filter> </receiver>
這裡的AnalyticsReceiver是專門接收推廣廣播的。
代碼很簡單
package com.example.installreceiverdemo; import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log; public class AnalyticsReceiver extends BroadcastReceiver { private static final String LOGTAG = AnalyticsReceiver.class.getSimpleName(); @Overridepublic void onReceive(Context context, Intent intent) {Log.i(LOGTAG + "_demo", "analyticsReceiver = " + intent + "data in String = " + intent.getStringExtra("referrer"));} }
這樣基本上就完成了,關於簡單測試,可以使用下列代碼:
Intent it = new Intent("com.android.vending.INSTALL_REFERRER"); it.setPackage("com.example.installreceiverdemo"); it.putExtra("referrer", "utm_source%3Dgoooooogggggggggggggle%26utm_medium%3Dcpc%26utm_term%3Dandroid%252Bbrowser%26utm_content%3Dmaxthon%2520browser%2520for%2520android%26utm_campaign%3DYou%2520never%2520know%2520fast."); sendBroadcast(it);
相關文章推薦參考:
https://developers.google.com/analytics/devguides/collection/android/v2/campaigns
http://www.localytics.com/docs/android-market-campaign-analytics/
http://gyurigrell.com/2012/2/21/tracking-install-sources-android-apps