72

I am trying to register a Broadcast Receiver that catches "com.android.vending.INSTALL_REFERRER" intents launched by Android after an app is installed from the Market.

I am following the details here: http://code.google.com/mobile/analytics/docs/android/#referrals

However, I cannot use Google Analytics so I have created my own solution. I have added the following to my manifest file:

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

and created a basic BroadcastReceiver class:

public class Receiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle extras = intent.getExtras();
        String referrerString = extras.getString("referrer");

        Log.w("TEST", "Referrer is: " + referrerString);
    }
}

However, when the app is installed the receiver doesn't seem to catch the Intent (if the Intent is even broadcast?) and I get no logging output.

Am I going wrong somewhere or is the Market no longer launching these Intents when an app is installed?

anticafe
  • 6,446
  • 9
  • 39
  • 71
Jake
  • 771
  • 1
  • 6
  • 5
  • 1
    I answered this same question here http://stackoverflow.com/questions/3817030/get-android-google-analytics-referrer-tag/3945100#3945100 – DougW Sep 26 '11 at 20:52
  • 1
    It's worth noting that, due to a bug in the newest version of the Android Market app, the referrer string does not currently work. You can star the bug here to help draw Google's attention to it: http://code.google.com/p/android/issues/detail?id=19247 – plowman Oct 03 '11 at 18:28
  • Does anyone knows if there is a way to receive this broadcast when OTHER apps, beside mine, is being installed? – Cassio Landim Jul 03 '14 at 14:23
  • How do you do the opposite? Meaning, how do you use an intent on an APK file, to mark it as the referrer of your choice? – android developer Jan 03 '15 at 22:20
  • Can you please tell me which parameters? Actually I have same issue like you. – Pratik Dasa Apr 15 '15 at 13:40
  • this can help. use adb. https://developers.google.com/analytics/solutions/testing-play-campaigns – Rajat Sharma Feb 12 '17 at 16:22

9 Answers9

56

I would try to help who, like me, fails to make install_referrer work and who don't find ANY useful information about these features.

Notes:

  1. The intent com.android.vending.INSTALL_REFERRER will be caught during the install process, not when the application starts for the first time.
  2. The referrer ...extras.getString("referrer").. is fixed but the contents can be any string value that respect the http get syntax ...referrer=thatsthevalue&thisisnot=xxx

The above code is okay, just some explanations to complete the info:

  1. Android Manifest. The <receiver> tags must be inside the <application> tags.
  2. The correct url to link the market is not the results of the famous google forms in sdk

but this one

http://market.android.com/details?id=your.application.package.name&referrer=my_referrer_finally_works_fine

Obviously, you need to follow the link from the mobile device and the only way for a complete test is to publish a test application in the market.

And a final and personal note.

I don't understand why those info are omitted completely and i hope that Google will act for detailing it.

onexf
  • 3,244
  • 3
  • 20
  • 34
Tobia
  • 647
  • 4
  • 3
  • 3
    To test, see this answer: http://stackoverflow.com/questions/5890914/how-to-test-android-referral-tracking/6966718#6966718 – emmby Oct 26 '11 at 14:59
  • 1
    INSTALL_REFERRER works as documented and there is nothing wrong with URLs generated by the "famous google forms" because they are designed be received by the GA SDK. Your code works only if you plan on posting to GA manually or integrating with another analytics platform. See the link above if you want to catch the referrer and still use the GA SDK. – Barry Fruitman Dec 09 '11 at 08:45
  • _the `` tag must be inside the `` tag_. This is not explained in analytics SDK dev guide, nor does the compilation fail if you do it otherwise. So, thank you Tobia! – Giorgos Kylafas Jun 27 '12 at 13:44
  • 1
    See my comment below - I believe your Note #1 is no longer true from Android >= 3.1. – Tom Sep 13 '12 at 15:58
  • You can test your app even before uploading it to the market. You can send from the shell: am broadcast -a com.android.vending.INSTALL_REFERRER -n com.yourapp.custom.referrer.receiver/.ReferrerReceiver --es "referrer" "your_referrer_data" – MikeL Oct 18 '16 at 22:47
20

This might be a little late, but you CAN test the install referrer without using Google Play, just use ADB :)

Run this from adb.exe

adb shell

am broadcast -a com.android.vending.INSTALL_REFERRER -n your.package/path.to.your.BroadcastReceiver --es "referrer" "test_referrer=test"

If you have logging setup in your BroadcastReceiver, you will see it popup in LogCat.

Hope this helps!

Machine Tribe
  • 771
  • 7
  • 13
  • 6
    on OS X key value pair of -e option need not be enclosed with double codes. So above command will be: `adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n your.package/path.to.your.BroadcastReceiver --es referrer test_referrer=test` – Alok Vaish Jun 20 '16 at 13:41
6

I think these answers must have been written pre-Android 3.1 - because things have changed in one import way.

The system now marks app as dormant when they are installed - they won't receive INSTALL_REFERRER or any other broadcast until the user explicitly activates the app by running it (or placing widget).

Tom
  • 15,404
  • 8
  • 60
  • 70
  • Any documentation to support this? I'm still failing to see any broadcast being sent before/after I launch my installed app despite opening the Play Store with the referrer param – rycfung Nov 15 '12 at 06:00
  • Tom, I think it works and have tested in 4.x. Since Android 3.1 Launch controls on stopped applications has been introduced. [http://developer.android.com/about/versions/android-3.1.html](http://developer.android.com/about/versions/android-3.1.html) Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.However A background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents that should be allowed to activate stopped applications. Hope this could be helpful. – androidyue Dec 17 '13 at 10:00
5

Okay so I found the reason why the Intent wasn't being launched. Apparently you MUST use the same parameter names as outlined here: http://code.google.com/mobile/analytics/docs/android/#referrals

You cant use your own parameter names as I was doing :S

Jake
  • 771
  • 1
  • 6
  • 5
  • can u help me in my question http://stackoverflow.com/questions/10431018/how-to-get-referrer-using-google-track-in-android – Khan May 03 '12 at 13:16
  • Updated link https://developers.google.com/analytics/devguides/collection/android/devguide#google-play-tracking – eveliotc Nov 01 '12 at 22:30
  • @Jake Can you please tell me which parameters? Actually I have same issue like you – Pratik Dasa Apr 15 '15 at 13:40
2
1) Broadcast receiver

public class InstallReferrerReceiver extends BroadcastReceiver {

    String referrer = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null) {
            if (intent.getAction().equals("com.android.vending.INSTALL_REFERRER")) {

                Bundle extras = intent.getExtras();
                if (extras != null)
                {
                    referrer = extras.getString("referrer");

                    Log.e("Receiver Referral", "===>" + referrer);                  
                    PrefUtils.putPrefString(context, PrefUtils.PRF_REFERRER, referrer);

                    /*
                    If you want split username and code use below code...
                    for ex referrer="username12345678890"
                    if(referrer!=null)
                    {
                        String[] referrerParts = referrer.split("(?<=\\D)(?=\\d)");
                        String strName = referrerParts[0];
                        String strCode = referrerParts[1];

                        Log.e("Receiver Referral Code", "===>" + strName);
                        Log.e("Receiver Referral Name", "===>" + strCode);

                        PrefUtils.putPrefString(context, PrefUtils.PRF_REFERRER_CODE, strName);
                        PrefUtils.putPrefString(context, PrefUtils.PRF_REFERRER_NAME, strCode);
                    }*/
                }
            }
        }
    }
}

2) Define a receiver in your app manifest.in which com.android.vending.INSTALL_REFERRER 

<receiver
            android:name=".services.InstallReferrerReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
</receiver>

3) Test your app configuration running this command

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n package_name/broadcast_receiver_path_with_packagename --es referrer "username1234567890"
Coldfin Lab
  • 311
  • 3
  • 8
2

I agree that Google's documentation isn't the best. However, I've only been able to get the intent to fire by actually uploading the app to the Market Place and then downloading/installing it. The intent does launch immediately after the download/instal - the user does not have to start the app. I'm using this to start a background service as well.

JavaCoderEx
  • 326
  • 2
  • 8
  • Can you indicate which version of android you were able to achieve this? Does this behaviour still hold for 2.3.3 & ICS (4.0)? – rycfung Nov 15 '12 at 06:04
2

Please notice that this is not the first start intent but only a android market related intent which is sent my the google android market. If you install the app through a different resource than the android market it will not fire.

Use the link which you can build there: http://code.google.com/mobile/analytics/docs/android/#android-market-tracking get the referrer from the intent and take it apart to get the different parameters

referrer = intent.getStringExtra("referrer");
Map<String, String> params = Toolbox.getQueryMap(referrer);

P.S. You don't need to read to read the deviceid/IMEI to do this, as some apps do. You shouldn't want to spy out your users.

white_gecko
  • 3,919
  • 3
  • 42
  • 66
1

Actually there could be any links like market://... or http://market... either work fine. Also it doesn't matter what kind of parameters will be in the referrer field. It works fine with any text in there.

The main issue that this event type "com.android.vending.INSTALL_REFERRER" doesn't send by broadcast. This event goes ONLY into the just installed application.

UPD: And there only one way to test it - deploy your App into the Market and then install it on the phone.

Vagif
  • 354
  • 3
  • 7
  • I believe the INSTALL_REFERRER doesn't work for the http protocol. At least, not anymore if it did before. Refer to this documentation, and note point #2 under known issues: https://developers.google.com/analytics/devguides/collection/android/v2/campaigns#known-issues – rycfung Nov 15 '12 at 06:01
  • @rycfung I think that only means it won't work when you click "install" from the web, not if you open the play store app from a web link – Zac Sweers Feb 20 '15 at 09:24
0

Use Google Play Referrer API (from 20 Nov 2017) very easily and securely as i answered here

Deven
  • 2,438
  • 1
  • 22
  • 30