google play 引荐流统计

来源:互联网 发布:淘宝 内容质量分 优质 编辑:程序博客网 时间:2024/06/07 06:19

google 提供了不同网站跳转的统计,对于android 和ios应用也提供了这方面的统计:


下方都将google play 简写为gp

需求:应用A 打开gp下载应用B,在应用B的统计后台希望能看到A引荐B的数据。


方案:使用gp引荐流统计(关于引荐流的说明:https://support.google.com/analytics/answer/2881627?hl=zh-Hans)

英文原文操作见链接https://developers.google.com/analytics/devguides/collection/android/v3/campaigns#google-play-url-builder


①下载GoogleAnalyticsServicesAndroid_3.02,将libGoogleAnalyticsServices.jar引入到工程中

②在AndroidManifest.xml文件中增加以下语句:

<!-- Used for Google Play Store Campaign Measurement-->;<service android:name="com.google.analytics.tracking.android.CampaignTrackingService" /><receiver android:name="com.google.analytics.tracking.android.CampaignTrackingReceiver" android:exported="true">  <intent-filter>    <action android:name="com.android.vending.INSTALL_REFERRER" />  </intent-filter></receiver>
③写一个自定义application为gp统计做准备,并在AndroidManifest.xml中配置好此application

package com.cyou.test;import com.google.analytics.tracking.android.GAServiceManager;import com.google.analytics.tracking.android.GoogleAnalytics;import com.google.analytics.tracking.android.Logger.LogLevel;import com.google.analytics.tracking.android.Tracker;import android.app.Application;import android.content.SharedPreferences;import android.preference.PreferenceManager;/* * An advanced Google Analytics implementation may be initialized * in a subclass of Application. Note that this example assumes data * only needs to be sent to a single Google Analytics property ID. */public class TestApplication extends Application {  private static GoogleAnalytics mGa;//  private static Tracker mTracker;  /*   * Google Analytics configuration values.   */  // Placeholder property ID.//  private static final String GA_PROPERTY_ID = "UA-XXXX-Y";  // Dispatch period in seconds.  private static final int GA_DISPATCH_PERIOD = 3;  // Prevent hits from being sent to reports, i.e. during testing.  private static final boolean GA_IS_DRY_RUN = false;  // GA Logger verbosity.  private static final LogLevel GA_LOG_VERBOSITY = LogLevel.VERBOSE;  // Key used to store a user's tracking preferences in SharedPreferences.  private static final String TRACKING_PREF_KEY = "trackingPreference";  /*   * Method to handle basic Google Analytics initialization. This call will not   * block as all Google Analytics work occurs off the main thread.   */  private void initializeGa() {    mGa = GoogleAnalytics.getInstance(this);//    mTracker = mGa.getTracker(GA_PROPERTY_ID);    // Set dispatch period.    GAServiceManager.getInstance().setLocalDispatchPeriod(GA_DISPATCH_PERIOD);    // Set dryRun flag.    mGa.setDryRun(GA_IS_DRY_RUN);    // Set Logger verbosity.    mGa.getLogger().setLogLevel(GA_LOG_VERBOSITY);    // Set the opt out flag when user updates a tracking preference.    SharedPreferences userPrefs = PreferenceManager.getDefaultSharedPreferences(this);    userPrefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener () {      @Override      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,          String key) {        if (key.equals(TRACKING_PREF_KEY)) {          GoogleAnalytics.getInstance(getApplicationContext()).setAppOptOut(sharedPreferences.getBoolean(key, false));        }      }    });  }  @Override  public void onCreate() {    super.onCreate();    initializeGa();  }  /*   * Returns the Google Analytics tracker.   *///  public static Tracker getGaTracker() {//    return mTracker;//  }  /*   * Returns the Google Analytics instance.   */  public static GoogleAnalytics getGaInstance() {    return mGa;  }}


③准备自己的跳转url 可以使用https://developers.google.com/analytics/devguides/collection/android/v3/campaigns#google-play-url-builder 来生成。Package Name 是B的包名,Campaign Source 是你自定义的字符串,用来标记来自A。其余信息可有可无,具体看文档说明。

④点击按钮跳转gp:

public static void startGooglePlayOrByBrowser(Context ctx, String url) {        try {            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));            browserIntent.setClassName("com.android.vending", "com.android.vending.AssetBrowserActivity");            browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            ctx.startActivity(browserIntent);            return;        } catch (Exception e) {            // can not start google play so we start it by Browser            Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(url));            it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            //it.setClassName("com.android.browser","com.android.browser.BrowserActivity");            ctx.startActivity(it);            e.printStackTrace();        }    }


测试:(国内需翻墙)

英文原文链接https://developers.google.com/analytics/solutions/testing-play-campaigns

假设我们链接为https://play.google.com/store/apps/details?id=com.test.lee&referrer=utm_source%3Dtester


方法①使用官方提供测测试方法:

手机安装A应用

连接手机运行:

<span style="font-family:Roboto, sans-serif;">C:\Users\***>adb -s **** shell am broadcast -a com.android.vending.INSTALL_REFERRER   --es "referrer" "utm_source%3D<span style="line-height: 22.3999996185303px; white-space: nowrap;">tester</span><span style="line-height: 22.3999996185303px; white-space: nowrap;">"</span></span>

查看logcat(如果没有数据可以重新打开A再次查看)出现以下信息则测试成功

Thread[GAThread,5,main]: Campaign found: utm_source=<span style="font-family: Roboto, sans-serif; font-size: 14px; line-height: 22.3999996185303px; white-space: nowrap;">tester</span>


————————————————————分割线——————————————————————

出现问题:(未解决,正在排查问题)

使用gp安装B应用后无法收到com.android.vending.INSTALL_REFERRER广播,原因未知

————————————————————分割线——————————————————————

补充:由于我制作的apk对大小要求较高,不准备使用GoogleAnalytics,因此考虑拦截com.android.vending.INSTALL_REFERRER广播,通过以下方法获取信息并上传到自己的服务器中:

<pre name="code" class="java">public class CustomReceiver extends BroadcastReceiver {  @Override  public void onReceive(Context context, Intent intent) {String testStr =.getStringExtra("referrer");        if("tester".equals(testStr)){              //push to your server          }  }}


<receiver android:name="com.cyou.test.CustomReceiver" android:exported="true">  <intent-filter>    <action android:name="com.android.vending.INSTALL_REFERRER" />  </intent-filter></receiver>

gp的统计后台需要第二天才能看到数据,大体如下图:




哦,对了,搜到几篇别人写的这种东西,可以借鉴下:

http://blog.kenyang.net/search/label/Google%20Analytic

http://support.appsflyer.com/entries/23635567-Testing-AppsFlyer-Android-SDK-Integration


0 0
原创粉丝点击