Android应用程序A要跟踪谷歌播放推荐数据为Android App B就安装要跟、应用程序、数据、Android

2023-09-12 23:57:37 作者:以、亡心祭城

让我们说我有一个安装在用户的设备的Andr​​oid应用程序A和我有一个AppWidget与我的应用程序,我们让其他Android开发者发布他们的应用推广广告上每安装基础上的成本。因此,我们需要的,如果被安装在用户的应用程序,通过A的推荐设备上应用程序B至跟踪。

让我们假设Android应用程序B有它的广告对Android应用程序A的小部件,并通过它,我们重定向应用程序A的用户应用程序B的用户来谷歌播放的链路上运行的所有引用的数据。该URL看起来像这样(推荐here -

https://play.google.com/store/apps/details?id=com.joelapenna.foursquared&referrer=utm_source%3Dtooyoou%26utm_medium%3Dbanner%26utm_term%3Dfoursquare%26utm_content%3Dfoursquare-tooyoou%26utm_campaign%3Dfoursquare-android)

如果你打上面的链接在浏览器或进行QR code出来,并启动它会带你到附录二中的谷歌播放的需要提交的数据。现在,当安装在用户的设备上的应用程序B和发起的第一次广播的应用程序中的期望接收的 com.android.vending.INSTALL_REFERRER 。如果广播接收和utm_source是应用程序A,则记录和处理交易。这就是我们的目标是做的。

下面是应用程序A的Andr​​oidManifest.xml中code段。

 <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:可调试=真正的>
        <活动
            机器人:名称=。LocateMeActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
       <接收器的Andr​​oid版本:NAME =com.locateme.android.ReferralReceiver机器人:出口=真正的>
      <意向滤光器>
        <作用机器人:名称=com.android.vending.INSTALL_REFERRER/>
      &所述; /意图滤光器>
    < /接收器>
    < /用途>
 
Android 体系结构介绍 转载

权限添加 -

 <使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>
 

下面是广播接收器类ReferralReceiver.java是应该处理的广播。

 进口java.io.UnsupportedEncodingException;
进口java.net.URLDe codeR;
进口的java.util.HashMap;
进口的java.util.Map;

进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.os.Bundle;

公共类ReferralReceiver扩展的BroadcastReceiver
{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图)
    {
        //解决方法对于Android的安全问题:HTTP://$c$c.google.com/p/android/issues/detail ID = 16006
        尝试
        {
            最终捆绑额外= intent.getExtras();
            如果(临时演员!= NULL){
                extras.containsKey(空);
            }
        }
        赶上(最终例外五){
            返回;
        }

        地图<字符串,字符串> referralParams =新的HashMap<字符串,字符串>();

        //返回如果这是不正确的意图。
        如果(!intent.getAction()。等于(com.android.vending.INSTALL_REFERRER)){// $ NON-NLS-1 $
            返回;
        }

        字符串引荐= intent.getStringExtra(推荐人); // $ NON-NLS-1 $
        如果(引荐== NULL || referrer.length()== 0){
            返回;
        }

        尝试
        {//删除所有URL编码
            引荐= URLDe coder.de code(引荐,X WWW的形式,urlen codeD); // $ NON-NLS-1 $
        }
        赶上(UnsupportedEncodingException E){返回; }

        //解析查询字符串,提取相关数据
        的String [] PARAMS = referrer.split(&安培;); // $ NON-NLS-1 $
        对于(字符串参数:PARAMS)
        {
            的String []对= param.split(=); // $ NON-NLS-1 $
            referralParams.put(对[0],配对[1]);
        }

        ReferralReceiver.storeReferralParams(背景下,referralParams);
    }

    私人最终静态的String [] EXPECTED_PARAMETERS = {
        utm_source
        utm_medium
        utm_term,
        的utm_content
        utm_campaign
    };

    私人最终静态字符串preFS_FILE_NAME =ReferralParamsFile;

    公共静态无效storeReferralParams(上下文的背景下,地图<字符串,字符串> PARAMS)
    {
        共享preferences存储= context.getShared preferences(ReferralReceiver preFS_FILE_NAME,Context.MODE_PRIVATE。);
        共享preferences.Editor编辑器= storage.edit();

        对于(字符串键:ReferralReceiver.EXPECTED_PARAMETERS)
        {
            字符串值= params.get(密钥);
            如果(值!= NULL)
            {
                editor.putString(键,值);
            }
        }

        editor.commit();
    }


    公共静态地图<字符串,字符串> retrieveReferralParams(上下文的背景下)
    {
        HashMap的<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
        共享preferences存储= context.getShared preferences(ReferralReceiver preFS_FILE_NAME,Context.MODE_PRIVATE。);

        对于(字符串键:ReferralReceiver.EXPECTED_PARAMETERS)
        {
            字符串值= storage.getString(键,NULL);
            如果(值!= NULL)
            {
                params.put(键,值);
            }
        }
        返回PARAMS;
    }
}
 

调试设备的Andr​​oid OS 2.3.4对

通过简单的TextView和Button在App的主要活动我尝试显示由接收器捕获的推荐数据。这里是电话和显示 -

referraltext.setText(ReferralReceiver.retrieveReferralParams(this.getApplicationContext()).toString());

当我启动上述应用程序B的推荐链接,去谷歌播放,安装它,打开它的第一次logcat中不显示广播com.android.vending.INSTALL_REFERRER所有,因此推荐数据显示时,显示一个空的字符串。

目前,当我使用adb命令下同广播接收器的工作原理就像一个魅力并显示推荐数据的同时。

  AM广播-a -n com.android.vending.INSTALL_REFERRER com.locateme.android/.ReferralReceiver --es引荐 "utm_source=tooyoou&utm_medium=banner&utm_term=foursquare&utm_content=foursquare-tooyoou&utm_campaign=foursquare-android"
 

这是否意味着谷歌播放不播放引用的数据和预期的目的呢?

还是意味着其广播的目的只为正在安装的附录二?

还是我做错了什么?

这是可能实现无App B就不必插入他们的应用程序我们跟踪SDK或code只是为了做广告与我们联系。

在此先感谢这么多的帮助。

其他信息 - 我们没有使用谷歌Analytics(分析)SDK所以我们用我们的定制广播接收器,而不是谷歌的BR

  

在code以上的工作就像一个魅力。我只是跟着卢卡斯的建议下面的答案标记为正确,取而代之的X WWW的形式urlen codeD为UTF-8。为什么我没有一开始做的,这是因为一个什么样的移动分析公司名为Localytics发布在这里 - http://www.localytics.com/docs/android-market-campaign-analytics/他们的code有同样的问题。所以基本上我有什么,现在是,上面的那块code在Android应用程序,我发表在谷歌播放,然后下载的应用程序从谷歌从我的运行ICS的Andr​​oid设备播放,打开它,点击检索推荐和它worked.Try它是免费的,这里是链接 - https://play.google.com/store/apps/details?id=com.locateme.android&referrer=utm_source%3Dtooyoou%26utm_medium%3Dbanner%26utm_term%3Dappdownload%26utm_content%3Dimage%26utm_campaign%3Dtooyooupromo

这是我想重申的另一个非常重要的事情是,如果你使用的是谷歌从网络上播放设备上远程安装一个应用程序,谷歌的播放活动的转诊追踪这种方法是行不通的。我已经测试过它,它是与谷歌开放的缺陷。

解决方案

源$ C ​​$ C以上是不正确的。

 引用= URLDe coder.de code(引荐,X WWW的形式,urlen codeD);
 

 引用= URLDe coder.de code(引荐,UTF-8);
 

在Android文档不明确这一点,但如果你使用的是X WWW的形式urlen $ C $光盘,它抛出一个不支持的编码异常....

Let's say I have an Android App A that is installed in the user's device and I have an AppWidget with my App where we let other Android developers publish their App promotion ads on a Cost Per Install basis. So we need to track if App B is being installed on the user's device through App A's referral.

Let's also assume Android App B has its advertisement running on Android App A's widget and the link through which we redirect App A's users to App B's user to Google Play is with all referrer data. The URL looks like this (as recommended here -

https://play.google.com/store/apps/details?id=com.joelapenna.foursquared&referrer=utm_source%3Dtooyoou%26utm_medium%3Dbanner%26utm_term%3Dfoursquare%26utm_content%3Dfoursquare-tooyoou%26utm_campaign%3Dfoursquare-android)

If you hit the above link in your browser or make a QR code out of it and launch it it will take you to App B in Google Play with the required referral data. Now when App B is installed on the user's device and launched for the first time the Broadcast that App A expects to receive is com.android.vending.INSTALL_REFERRER. If the broadcast is received and the utm_source is App A then record and process the transaction. This is all we aim to do.

Here is App A's AndroidManifest.xml code snippet.

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:debuggable="true" >
        <activity
            android:name=".LocateMeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       <receiver android:name="com.locateme.android.ReferralReceiver" android:exported="true">
      <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
      </intent-filter>
    </receiver> 
    </application>

Permissions added -

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Here is the Broadcast Receiver class ReferralReceiver.java that is supposed to process the broadcast.

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";

    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();
    }


    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;
    }
}

The Debugging device is on Android OS 2.3.4

Through a simple TextView and Button in the main activity of the App I try to display the referral data captured by the receiver. Here is the call and display -

referraltext.setText(ReferralReceiver.retrieveReferralParams(this.getApplicationContext()).toString());

When I launch App B's referral link shown above, go to Google Play, install it and open it for the first time logcat doesn't show the broadcast com.android.vending.INSTALL_REFERRER at all and hence the referral data when displayed shows an empty string.

At the same time when I use adb command below the same broadcast receiver works like a charm and displays the referral data.

am broadcast -a com.android.vending.INSTALL_REFERRER -n com.locateme.android/.ReferralReceiver --es "referrer" "utm_source=tooyoou&utm_medium=banner&utm_term=foursquare&utm_content=foursquare-tooyoou&utm_campaign=foursquare-android"

So does that mean the Google Play does not broadcast the referrer data and the expected intent at all?

Or does it mean that it broadcasts the intent only for App B that is being installed?

Or am I doing something wrong?

Is this possible to achieve without App B having to insert our tracking SDK or code in their App just to advertise with us.

Thanks so much in advance for your help.

Additional Info - We are not using the Google Analytics SDK so we are using our custom Broadcast Receiver rather than Google's BR

The code above works like a charm. I just followed Lucas' advice below in the answer marked correct and replaced x-www-form-urlencoded to UTF-8. Why I didn't do this at first is because of what a mobile analytics company called Localytics posted here - http://www.localytics.com/docs/android-market-campaign-analytics/ Their code has the same issue. So basically what I have right now is that the above piece of code in an Android App that I published in Google Play and then downloaded the App from Google Play from my Android device running ICS, opened it and clicked on Retrieve Referral and it worked.Try it for free, here is the link - https://play.google.com/store/apps/details?id=com.locateme.android&referrer=utm_source%3Dtooyoou%26utm_medium%3Dbanner%26utm_term%3Dappdownload%26utm_content%3Dimage%26utm_campaign%3Dtooyooupromo

Another very important thing that I want to reiterate is that the google play campaign referral tracking this way will not work if you are installing an app remotely on your device using google play from the web. I have tested it and it is an open defect with Google.

解决方案

the source code above is not correct.

the line

referrer = URLDecoder.decode(referrer, "x-www-form-urlencoded");

should be

referrer = URLDecoder.decode(referrer, "UTF-8");

the android documentation is not clear about this, but if you use x-www-form-urlencoded it will throw an unsupported encoding exception....