5

我想实现安装引荐来源网址跟踪并希望引荐来源网址参数并存储在后端数据库中我已经看到了许多示例或问题,例如获取 Android Google Analytics 引荐来源标签Android Google Analytics Campaign 跟踪没有出现但没有获得我生成链接的方式并尝试代码

   package SimpleDemo.ReferralTrack; 

   import java.io.UnsupportedEncodingException;
   import java.net.URLDecoder;
   import java.util.HashMap;
   import java.util.Map;

   import android.content.BroadcastReceiver;
   import android.content.Context;
   import android.content.Intent;
   import android.content.SharedPreferences;
   import android.os.Bundle;

  public class ReferralReceiver extends BroadcastReceiver
   {
@Override
public void onReceive(Context context, Intent intent)
{
    // Workaround for Android security issue: http://code.google.com/p/android/issues/detail?id=16006
    try
    {
        final Bundle extras = intent.getExtras();
        if (extras != null) {
            extras.containsKey(null);
        }
    }
    catch (final Exception e) {
        return;
    }

    Map<String, String> referralParams = new HashMap<String, String>();

    // Return if this is not the right intent.
    if (! intent.getAction().equals("com.android.vending.INSTALL_REFERRER")) { //$NON-NLS-1$
        return;
    }

    String referrer = intent.getStringExtra("referrer"); //$NON-NLS-1$
    if( referrer == null || referrer.length() == 0) {
        return;
    }

    try
    {    // Remove any url encoding
        referrer = URLDecoder.decode(referrer, "x-www-form-urlencoded"); //$NON-NLS-1$
    }
    catch (UnsupportedEncodingException e) { return; }

    // Parse the query string, extracting the relevant data
    String[] params = referrer.split("&"); // $NON-NLS-1$
    for (String param : params)
    {
        String[] pair = param.split("="); // $NON-NLS-1$
        referralParams.put(pair[0], pair[1]);
    }

    ReferralReceiver.storeReferralParams(context, referralParams);
}

private final static String[] EXPECTED_PARAMETERS = {
    "utm_source",
    "utm_medium",
    "utm_term",
    "utm_content",
    "utm_campaign"
};
private final static String PREFS_FILE_NAME = "ReferralParamsFile";

/*
 * Stores the referral parameters in the app's sharedPreferences.
 * Rewrite this function and retrieveReferralParams() if a
 * different storage mechanism is preferred.
 */
public static void storeReferralParams(Context context, Map<String, String> params)
{
    SharedPreferences storage = context.getSharedPreferences(ReferralReceiver.PREFS_FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = storage.edit();

    for(String key : ReferralReceiver.EXPECTED_PARAMETERS)
    {
        String value = params.get(key);
        if(value != null)
        {
            editor.putString(key, value);
        }
    }

    editor.commit();
}

/*
 * Returns a map with the Market Referral parameters pulled from the sharedPreferences.
 */
public static Map<String, String> retrieveReferralParams(Context context)
{
    HashMap<String, String> params = new HashMap<String, String>();
    SharedPreferences storage = context.getSharedPreferences(ReferralReceiver.PREFS_FILE_NAME, Context.MODE_PRIVATE);

    for(String key : ReferralReceiver.EXPECTED_PARAMETERS)
    {
        String value = storage.getString(key, null);
        if(value != null)
        {
            params.put(key, value);
        }
    }
    return params;
}
}

之后我尝试了我的活动

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(DemoActivity.this);
              String  referrers1 =preferences.getString("ga_campaign", "0");
              Map<String, String> retrieveReferralParams=ReferralReceiver.retrieveReferralParams(DemoActivity.this);
              String  referrers2= retrieveReferralParams.get("utm_source");
              String  referrers3= retrieveReferralParams.get("utm_medium");
              String  referrers4= retrieveReferralParams.get("utm_term");
              String  referrers5= retrieveReferralParams.get("utm_content");
              String  referrers6= retrieveReferralParams.get("utm_campaign");
              tv.setText(referrers1+" "+referrers2+" "+referrers3+" "+referrers4+" "+referrers5+" "+referrers6+" ");

单击按钮但未获得所需的输出

我想要类似的东西

"https://play.google.com/store/apps/details?id=com.lifestreet.android.TestInstallationIntent&referrer=bb%3DAAAAAAAAAA&feature=search_result%22"  
 Ans     

   referrer=bb

任何帮助我都非常感谢提前谢谢。

4

3 回答 3

1

不确定 Google 是否允许您发送任意信息。尝试使用生成器创建 url。

https://developers.google.com/analytics/devguides/collection/android/devguide#google-play-builder

于 2012-06-12T07:22:45.650 回答
0

我有一个类似的问题。发现它是生命周期问题:在我的应用程序的 onResume() 之后,在同一主线程上调用 install_referrer 接收器的 onReceive,因此在 onResume() 期间读取引荐来源文件的任何尝试都会失败。这是证明它的 logcat,这在使用 Android 4.2.1 和 4.4.2 的 2 台设备上是 100% 可重复的:

首先,play store 在单独的(store)进程中广播指向包的referrer:

11-04 14:17:51.558: D/Finsky(1737): [1] ReferrerRebroadcaster.doBroadcastInstallReferrer: Delivered referrer for com.xxx.demo

应用程序 onResume() - 仍然没有激活广播接收器!小号

11-04 14:17:51.888: D/XXX Main Activity(22730): onResume

该应用程序尝试读取引荐来源网址(接收者应使用 getSharedPreferences.putString 存储该引荐来源网址):

11-04 14:17:51.958: I/XXX(22730): Extracted install referrer: 

只是现在接收器在主线程上被调用,并且很快就会尝试将引用者写入文件:

11-04 14:17:51.918: I/XXX(22730): Received install referrer: abcdefg

如您所见, onResume() 没有机会实际读取文件,因此提取不会产生任何结果。但是,如果我关闭应用程序并重新打开它,onResume 现在可以找到该文件并处理引荐来源字符串 - 但不是在首次启动时 :)

希望这可以帮助!

于 2014-11-04T13:35:58.777 回答
0

Google Analytics 在其 SDK 中使用任意常量。对于 campaing_source 是 &cs。

于 2015-02-03T17:09:48.613 回答