com.android.vending.INSTALL_REFERRER isn't working

So xbakesx says that it seems to work if his receiver extends com.google.analytics.tracking.android.AnalyticsReceiver.

I think the key is that the intent has permissions for ...AnalyticsReceiver and so no other class that is not extending it can pick up the intent. If you look at their test broadcast https://developers.google.com/analytics/solutions/testing-play-campaigns it does appear specific for that class.

If you change that test broadcast so that your class replaces com.google.analytics.tracking.android.AnalyticsReceiver you can then receive it. The biggest problem is they seemed to have locked down this class in beta 4 or 5. If anyone has a link to beta 3 we could test this, or if xbakex could confirm with playing around with the new jars that would rock!

Update:

BAM! So permissions are not an issue. I created a test project and used the PlayStores alpha testing to test out referrer links, which you can build here: https://developers.google.com/analytics/devguides/collection/android/v2/campaigns.

The cool thing is you don't need any GA jar at all! Checkout my test project here: https://github.com/twotoasters/AnalyticsTest/ This project also shows you how to parse the link to get all of the information that you need.


After many failed attempts i could finally see the passed referral parameters in logcat.

Along the way, i figured out a few things, i am not too sure if i am doing it rite or wrong, but for some reasons, these worked. If someones still stuck, they can get some pointers from my learnings.

A. creating a custom BroadcastReceiver where you can ready the intent. (this will executed, only once you have successfully fired the Install_referrer intent from the ADB for testing). Also make sure, if you need to do a post back of the referrer information to a server, it will have to be on a separate thread.

    public class CustomBR extends BroadcastReceiver {

    private static final String D_TAG = "BR";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(D_TAG, "CustomReceiver onReceive (context, intent)");
        try {

            String referrer = intent.getStringExtra("referrer");
            // pass the referrer string to another singleton class to post it to server
            HandleServerComm.getInstance().postData(referrer);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

B. update the androidmanifest.xml file to reflect the custom receiver you have created

    <receiver android:exported="true" android:name="com.example.myapp.CustomBR" android:enabled="true">
        <intent-filter>
            <action android:name="com.android.vending.INSTALL_REFERRER" />
        </intent-filter>
    </receiver>

C. make sure you have adb installed correctly to test this on local environment. you will also need a device to be connected via USB with remote debugging enabled.

D. run the adb shell command to remotely broadcast a install_referrer on the device and pass it parameters.

The command is

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER -n com.example.myapp/.CustomBR --es  "token" "sample_token" --es  "source" "banner"

Note that the important parts of this command is com.example.myapp/.CustomBR and --es "token" "sample_token" where --es is the additional parameters that are being sent along with the intent. the first quote after --es is the querystring / parameter name and the second quote is the value. Similarly, if you have to add more than one value, replicate it as shown in the example above.

E. Finally the most important part that kept me frustrated all the while - the app installed on the device itself. Your app should be installed on the device but not running at all. To do this, you will have to "Force Close" the app and then fire the adb shell command to fire up the install_referrer. thats when, you should see the logcat light up with required data.

F. You might also want to uninstall the update on google play store app and restore it to factory settings. at times (not confirmed) the version of google play determines what data is being passed to the app via the install_referrer or if the referrer is called at all.

Hope this helps someone.


Registering a BroadcastReceiver in your app's AndroidManifest.xml with an intent filter is the correct solution to app install referrals whether using Google Analytics or not.

<receiver
    android:exported="true"
    android:name="com.yourcompany.package.receivers.InstallReceiver">
    <intent-filter>
        <action android:name="com.android.vending.INSTALL_REFERRER" />
    </intent-filter>
</receiver>

If you're already using Google Analytics you simply specify android:name="com.google.android.gms.analytics.CampaignTrackingReceiver" for your receiver and the following service as well. Both are included in the Google Play services client library so there isn't any Java code to write. If you haven't already, you will also have to go through the initial setup instructions for Google Analytics for your App.

 <service android:name="com.google.android.gms.analytics.CampaignTrackingService" />

If you're not using Google Analytics then you'll need to define your own BroadcastReceiver in your java code. You will want it to inspect the extras on the received intent when implementing onReceive.

The referrer parameter in the URL that is received by Google Play (the store) is the only parameter passed through to the Android app for the referral so it's very common to encode a few parameters within it. For Google Analytics that means industry standard utm_* parameters, but you can use others. Here's the test adb command I typically use for opening Google Play to install and test the full flow.

adb shell "am start -a android.intent.action.VIEW -d \"https://play.google.com/store/apps/details?id=com.somecompany.package&referrer=utm_source%253Dtest_campaign_source%2526utm_medium%253Dtest_campaign_medium%2526utm_term%253Dtest_campaign_term%2526utm_content%253Dtest_campaign_content%2526utm_campaign%253Dtest_campaign_name\""

Testing Notes:

  • When testing this flow it's very important to check that the above command resulted in an output where the Intent was logged to your console with the FULL referrer information still attached. It's very easy for the escaping to be incorrect and silently drop the referrer.
  • Remember that the APK must have been installed by Google Play (the Store) on to the device you're testing on (you can't side-load). Therefore, you usually need to use your Alpha distribution channel in Google Play to test this.
  • It's important to note that if the device is >= Honeycomb MR1 the INSTALL_REFERRER intent is broadcast after the App is first launched rather than after the app is installed.
  • You will need to reinstall your app every time you need to test the referrer flow.
  • Install referrals are tracked when the app is installed from the Android Google Play app, but not the web version of the store.