82

I'm looking to migrate from listening to the Play Store's INSTALL_REFERRER intent, to using the new Google Play Install Referrer Library.

I am struggling to find a way to test this new library without first having to add my app to the Play Store. When listening for the INSTALL_REFERRER intent via a BroadcastReceiver, I could test by manually sending a broadcast via the Activity Manager to mimic the behavior. That is to say, I could test by following these steps from Google.

Does there also exist a way I can test this new library without having to first put my app on the Play Store?

Bart Kerfeld
  • 945
  • 1
  • 6
  • 7
  • 18
    Plus 1 for this. I also need a way to test this. – Niraj Gupta Mar 02 '18 at 00:57
  • Have you resolved your issue? do you have some working code for this? from what place have you start tracking your first installation? Have you added a broadcast receiver for listening to Intent.ACTION_PACKAGE_FIRST_LAUNCH or just wrote code in mainActivity? – yozhik Aug 10 '18 at 08:00
  • @NirajGupta You can try to use my solution below. – yozhik Aug 10 '18 at 10:57
  • Found this answer for a similar question very helpful: https://stackoverflow.com/a/58621059/1402641 – Marcin Koziński Jan 17 '20 at 11:18

6 Answers6

56

There's one old hack to test this.

Steps:

  1. Start Google Play on the device using campaign link, for example, https://play.google.com/store/apps/details?id=com.test.test_project&referrer=utm_source%3Dtest_source%26utm_medium%3Dtest_medium%26utm_term%3Dtest-term%26utm_content%3Dtest_content%26utm_campaign%3Dtest_name (You can use google play generator: https://developers.google.com/analytics/devguides/collection/android/v3/campaigns#google-play-url-builder)

  2. DON'T TAP ON INSTALL BUTTON

  3. Install your test build using adb. adb install -r app-debug.apk

Google Play will be returning your test campaign now.

Snowbases
  • 2,316
  • 2
  • 20
  • 26
Quickern
  • 609
  • 6
  • 6
  • 1
    Yes it is working , But be careful utm_source will give you (not%20set) if your link didn't redirect you to google play app directly . – Islam Ahmed Jun 06 '20 at 17:03
  • 7
    Once you insall via adb you should see the Install button change to an "Open" button. You have to open the app via the button (not using adb am) to get the UTM payload. If you don't see the button text change it is because the application id of the installed app is not the same as the one in play store. This is likely if you release a 'prod' flavor of your app but are working with a 'debug' flavor, temporarily change your debug app's application id and the above should work! – Kevin Barron Jul 25 '20 at 15:53
  • Yes, this work but why install from adb? – Bipin Bharti Jun 30 '21 at 12:14
  • 1
    Build + install from AS UI creates an APK with `android:testOnly` flag, which cannot be tested in that regard. only using gradle's `assembleDebug` task then install manually with adb worked (for me at least). – Shlomi Katriel Sep 30 '21 at 07:19
  • @ShlomiKatriel Toda Achi! – Uriel Frankel Apr 25 '22 at 12:35
  • Be careful which email you are signed into on play store and on test device. If you are logged into your developer email (email on app listing), you will get (not set) for utm source and medium. So make sure you are signed into a different email on play store and on the device. The email you use has to be included in your list of testers if you are testing using Alpha or Beta channels. – Marline May 17 '22 at 01:53
  • instead of step 3, we can simply install from AS by pressing the green run button too. not sure why it didn't worked for @ShlomiKatriel, can't verify, but AS dolphin runs `:app:assembleDebug` and generates referral data for me when pressing the green run button – ansh sachdeva Nov 26 '22 at 19:58
  • I couldn't use this way to test when I needed to update the app (so the previous version of the app exists). That is, we pass the referrer to Google Play Store (to make the user update the app), user updates the app from the store, and then opens the updated app and receives the refererrer on the start of the updated app. – Alexey Timokhin Jun 15 '23 at 16:08
  • I'm not able to get the "Open" option from Google play store. It is always saying Update even when I've a more updated build version. I'm not sure what's wrong :( – Satty Jul 06 '23 at 01:57
7

This is the summary of my test:

  1. old broadcast way can test.But it's deprecated and not support for now
  2. U can test install referrer lib set up by use adb,and you will get utm_source=google-play&utm_medium=organic just like download form Google Play directly. But we can't get more info, it's can only test your library settings is correct
  3. https://stackoverflow.com/a/60342463/12716149 by @Quickern.To be honest, it works follow these tips
  4. Use Beta test provided by Google Play: https://stackoverflow.com/a/49750758/12716149. Without a doubt, it works
  5. Use emulator:https://stackoverflow.com/a/59744213/12716149 @Marilia. I didn't test this, because emulator with Google Play Store is just like a real device.And the answer said the condition is upload app to Google Play store,so I think it's just like article 4
likeme
  • 61
  • 1
  • 7
2

After reading the required steps in https://developers.google.com/analytics/solutions/testing-play-campaigns, I found it possible to test the app install referrer library before releasing it to the Play Store, using the ADB tools.

Please note - the following test is using the deprecated broadcast receiver, not with the new Play Install Referrer API. (Thanks to Peter Keefe who noted it).

Ensure the application is not running and run this shell code in your Terminal / CMD (while the device connected with adb) for triggering the installation intent:

 echo 'am broadcast \
-a com.android.vending.INSTALL_REFERRER \
-n "your.package.name/path.to.receiver" \
--es "referrer" \
  "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name"; \
exit' | ./adb shell

Replace the package name and path to the receiver:

your.package.name/path.to.receiver

Also, don't forget to replace the utm url params in order to track different install source:

utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name

weizenberg
  • 302
  • 2
  • 13
  • 2
    This tests using the deprecated broadcast receiver, NOT the new Play Install Referrer API. – Peter Keefe Dec 11 '19 at 16:04
  • Can you help with testing the library way ? – Darshan Pania Dec 29 '19 at 18:59
  • The only way to test it should be using the Beta release feature in the Play Store console. Launch beta testing to your device, and use the beta link to inject the referral parameters. I used firebase analytics to log the referral data as a custom event, so you can check if the data received correctly using the Firebase Analytics Debug dashboard. – weizenberg Jan 05 '20 at 10:10
2

I was able to test the Play Install Referrer Library using the emulator. Uninstalling the app and running it again would start a connection and give me the expected responseCode in onInstallReferrerSetupFinished.

Marilia
  • 1,911
  • 1
  • 19
  • 28
  • But all the emulators won't have play store app right? Then how can we test them? – Mohanakrrishna Feb 04 '20 at 05:49
  • To test the case when the Play Store is disabled, you can disable the Google Play Store in the settings. Go to Settings > Tap Apps & notifications. Choose Google Play Store (or first tap See all apps or App info to find it). Tap Disable option. If the Google Play Store is disabled I receive `InstallReferrerResponse.FEATURE_NOT_SUPPORTED` as `responseCode`. – Marilia Feb 04 '20 at 18:46
  • Thanks. I did accordingly and I am able to see the response codes. – Mohanakrrishna Feb 05 '20 at 05:30
  • 1
    btw did you try to mock the procedure of your app installing from an add and then after opening your app from play store, are you able to monitor the `installReferrerUrl` params like `utm_source` or `utm_medium`? – Mohanakrrishna Feb 05 '20 at 05:34
  • I didn't try installing from an ad, but installing from the play store gives me those parameters in the `referrerUrl`, like for example: `utm_source=google-play&utm_medium=organic` – Marilia Feb 06 '20 at 00:48
  • Install from play store means you uploaded to or updated your app on the play store? – Mohanakrrishna Feb 06 '20 at 05:18
  • Yes, organic source installs or building the app to the emulator should return those parameters and values I mentioned. – Marilia Feb 07 '20 at 04:10
2

Step 1

Testing Url
https://play.google.com/store/apps/details?id=com.test.test_project&referrer=utm_source%3Dtest_source%26utm_medium%3Dtest_medium%26utm_term%3Dtest-term%26utm_content%3Dtest_content%26utm_campaign%3Dtest_name

Step 2 Click on the above link. Open Play store (Don't Install from play Store)

Step 3 Install from Android studio. You get the result. Now if you check another Link you need to follow the above step the same manner bcz,

Note: Caution: The install referrer information will be available for 90 days and won't change unless the application is reinstalled. To avoid unnecessary API calls in your app, you should invoke the API only once during the first execution after install.

(From Here)

Note: My App right now Alpha Version(In Play Store)

Bipin Bharti
  • 1,034
  • 2
  • 14
  • 27
-5

Maby some will need this. I created test app and here is source code for only one needed activity. It's about how to add Play Install Referrer Library: https://developer.android.com/google/play/installreferrer/library

Also here is good description: https://android-developers.googleblog.com/2017/11/google-play-referrer-api-track-and.html

package com.cat.red.rsamazingapp;

import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

public class MainActivity extends AppCompatActivity implements InstallReferrerStateListener {

    private static final String TAG = "RSD";
    InstallReferrerClient mReferrerClient;
    TextView txtBody;
    StringBuilder stringBuilder;

    private int attemps = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtBody = (TextView) this.findViewById(R.id.txt_body);
        stringBuilder = new StringBuilder();
        stringBuilder.append("\nonCreate");

        mReferrerClient = InstallReferrerClient.newBuilder(this).build();
        stringBuilder.append("\n1. onCreate.isReady == " + mReferrerClient.isReady());
        mReferrerClient.startConnection(this);
        stringBuilder.append("\nstartConnection");
        stringBuilder.append("\n2. onCreate.isReady == " + mReferrerClient.isReady());
    }

    @Override
    public void onInstallReferrerSetupFinished(int responseCode) {
        stringBuilder.append("\nonInstallReferrerSetupFinished");

        switch (responseCode) {
            case InstallReferrerClient.InstallReferrerResponse.OK:
                // Connection established
                stringBuilder.append("\nonInstallReferrerSetupFinished. InstallReferrer conneceted. Success");
                stringBuilder.append("\nisReady == " + mReferrerClient.isReady());

                try {
                    ReferrerDetails installReferrerDetails = mReferrerClient.getInstallReferrer();
                    if (installReferrerDetails == null) {
                        stringBuilder.append("\ninstallReferrerDetails == NULL");
                    }

                    if (installReferrerDetails != null) {
                        stringBuilder.append("\ngetInstallReferrer = " + installReferrerDetails.getInstallReferrer());
                        stringBuilder.append("\ngetInstallBeginTimestampSeconds = " + installReferrerDetails.getInstallBeginTimestampSeconds());
                        stringBuilder.append("\ngetReferrerClickTimestampSeconds = " + installReferrerDetails.getReferrerClickTimestampSeconds());
                    }
                } catch (RemoteException e) {
                    stringBuilder.append("\nonInstallReferrerSetupFinished. exception: " + e.getMessage());
                    txtBody.setText(stringBuilder.toString());
                    e.printStackTrace();
                }
                break;
            case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                stringBuilder.append("\nonInstallReferrerSetupFinished. Install Referrer API not supported by the installed Play Store app.");
                stringBuilder.append("\nisReady == " + mReferrerClient.isReady());
                break;
            case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                // Connection could not be established
                stringBuilder.append("\nonInstallReferrerSetupFinished. Could not initiate connection to the Install Referrer service.");
                stringBuilder.append("\nisReady == " + mReferrerClient.isReady());
                break;
            case InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED:
                stringBuilder.append("\nonInstallReferrerSetupFinished. Play Store service is not connected now - potentially transient state");
                stringBuilder.append("\nisReady == " + mReferrerClient.isReady());
                break;
            case InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR:
                stringBuilder.append("\nonInstallReferrerSetupFinished. General errors caused by incorrect usage.");
                stringBuilder.append("\nisReady == " + mReferrerClient.isReady());
                break;
            default:
                stringBuilder.append("\nonInstallReferrerSetupFinished. responseCode not found. code = " + responseCode);
                stringBuilder.append("\nisReady == " + mReferrerClient.isReady());
        }

        stringBuilder.append("\nisReady == " + mReferrerClient.isReady());
        mReferrerClient.endConnection();
        stringBuilder.append("\nendConnection");
        stringBuilder.append("\nisReady == " + mReferrerClient.isReady());

        txtBody.setText(stringBuilder.toString());
    }

    @Override
    public void onInstallReferrerServiceDisconnected() {
        // Try to restart the connection on the next request to
        // Google Play by calling the startConnection() method.
        stringBuilder.append("\nonInstallReferrerServiceDisconnected. attemptCount = " + attemps);
        stringBuilder.append("\nisReady == " + mReferrerClient.isReady());

        if (attemps < 3) {
            attemps++;
            stringBuilder.append("\nonInstallReferrerServiceDisconnected. RE-startConnection");
            mReferrerClient.startConnection(this);
        } else {
            stringBuilder.append("\nonInstallReferrerServiceDisconnected. endConnection");
            stringBuilder.append("\nisReady == " + mReferrerClient.isReady());
            mReferrerClient.endConnection();
            stringBuilder.append("\nendConnection");
            stringBuilder.append("\nisReady == " + mReferrerClient.isReady());
        }

        txtBody.setText(stringBuilder.toString());
    }

    @Override
    protected void onResume() {
        super.onResume();
        stringBuilder.append("\nonResume. isReady == "+ mReferrerClient.isReady());
    }

    public static String format(GregorianCalendar calendar){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        fmt.setCalendar(calendar);
        String dateFormatted = fmt.format(calendar.getTime());
        return dateFormatted;
    }
}
yozhik
  • 4,644
  • 14
  • 65
  • 98
  • 4
    Above is just a "example" code, but didn't show how to test the APP before uploading to the play store. – Yingbo Miao Apr 17 '19 at 14:45
  • 1
    @YingboMiao True, however if you have the possibility to upload to play store, do you know if you can test it for all tracks (internal, alpha, beta, production) or just some? – riper Apr 24 '19 at 07:58