获取Android Google Analytics引荐来源代码

我们计划使用Google Analytics(分析)通过Android电子市场跟踪广告点击后引荐,以便将其转到我们的应用程序。

根据Google文档 ,引荐来源代码是通过意图传递的,并由Google Analytics库自动logging。

这很好,但是我们需要为我们自己的内部分析提取推荐标签。 关于如何把它从最初的启动意图中解脱出来的细节以及如何在上线之前如何模拟这些文件的说明,这些文件是很害羞的。

有没有人有这方面的经验?

我继续发布一个坏的像素search应用程序玩意图窥探。 出于某种原因,当我注册两个不同的广播接收器(即com.google.android.apps.analytics.AnalyticsReceiver和我自己的),我从来没有收到过我自己的。

相反,我注册了自己的接收者,处理信息并将其传递给Google Analytics(分析)。 不知道这是怎么犹太教,但它的作品。 代码如下。

public class ZSGoogleInterceptor extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); String referrerString = extras.getString("referrer"); // Next line uses my helper function to parse a query (eg "a=b&c=d") into key-value pairs HashMap<String, String> getParams = Utility.getHashMapFromQuery(referrerString); String source = getParams.get("utm_campaign"); if (source != null) { SharedPreferences preferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE); Editor preferencesEditor = preferences.edit(); preferencesEditor.putString("ga_campaign", source); preferencesEditor.commit(); } // Pass along to google AnalyticsReceiver receiver = new AnalyticsReceiver(); receiver.onReceive(context, intent); } } 

然后,当您的应用程序实际启动时,您可以将该值从共享首选项中取出,并将其与用户注册或任何其他事件一起传递。 我将广告系列代码用于我的目的,但您可以从使用此工具创build的引荐来源string中获取所需的任何参数。

@DougW的答案针对Analytics SDK 4进行了更新

https://developers.google.com/analytics/devguides/collection/android/v4/campaigns

 import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import com.google.android.gms.analytics.CampaignTrackingReceiver; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.LinkedHashMap; import java.util.Map; /** * Created by dave on 15-05-05. */ public class ReferrerReceiver extends BroadcastReceiver { public static final String REFERRER = "REF"; public static final String UTM_CAMPAIGN = "utm_campaign"; public static final String UTM_SOURCE = "utm_source"; public static final String UTM_MEDIUM = "utm_medium"; public static final String UTM_TERM = "utm_term"; public static final String UTM_CONTENT = "utm_content"; private final String[] sources = { UTM_CAMPAIGN, UTM_SOURCE, UTM_MEDIUM, UTM_TERM, UTM_CONTENT }; @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); String referrerString = extras.getString("referrer"); try { Map<String, String> getParams = getHashMapFromQuery(referrerString); SharedPreferences preferences = context .getSharedPreferences(REFERRER, Context.MODE_PRIVATE); SharedPreferences.Editor preferencesEditor = preferences.edit(); for (String sourceType : sources) { String source = getParams.get(sourceType); if (source != null) { preferencesEditor.putString(sourceType, source); } } preferencesEditor.commit(); } catch (UnsupportedEncodingException e) { Log.e("Referrer Error", e.getMessage()); } finally { // Pass along to google CampaignTrackingReceiver receiver = new CampaignTrackingReceiver(); receiver.onReceive(context, intent); } } public static Map<String, String> getHashMapFromQuery(String query) throws UnsupportedEncodingException { Map<String, String> query_pairs = new LinkedHashMap<String, String>(); String[] pairs = query.split("&"); for (String pair : pairs) { int idx = pair.indexOf("="); query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8")); } return query_pairs; } } 

在你清单文件中:

  <service android:name="com.google.android.gms.analytics.CampaignTrackingService" /> <receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver" /> <!-- Make sure this points to the location of Referrer Receiver in your package --> <receiver android:name=".ReferrerReceiver" android:exported="true"> <intent-filter> <action android:name="com.android.vending.INSTALL_REFERRER" /> </intent-filter> </receiver> 

检查:

从Android电子市场安装应用后获取引荐来源

为解决scheme。

托比亚