okariotis
okariotis

Reputation: 479

Can my App know which QR code was used to download it

I am building an Android application (SDK 24+), using Google Cloud services like Firebase as my back-end.

What I would like to achieve is, having multiple, unique QR codes all of which will lead to my app being downloaded. Also, at the same time, I want my app to know from which QR it was downloaded.
In effect, besides using the different QRs for downloading my app, I also want them to serve as a unique ID inside my app.

Example:
Let's say we have 5 different QRs: QR-a, QR-b, QR-c, QR-d, QR-e. I want all 5 of them to lead to my app being downloaded and, once it has been downloaded, I want my app to be aware of the QR used to download it, be it QR-a, QR-b, or whichever else.

My own thoughts:
From my understanding the first part (different QRs downloading the same app) should be doable, but is the second one (the app being aware of which QR it was downloaded from) even feasible?
I know services like Google Analytics provide a lot of detail on the device/user that used a QR. Perhaps, after downloading the app using QR, the app could query Google Analytics about which QR the user used to download it.

It's the first time I'm working with QRs, so my understanding on them may be fundamentally flawed.
Any thoughts or ideas on how to approach this problem will be appreciated.

Upvotes: 2

Views: 2400

Answers (1)

Alexander Hoffmann
Alexander Hoffmann

Reputation: 6024

If all your links point to the Google Play Store, then this is doable by using the Google Play Store Install Referrer API.

Passing data to Google Play Store

Your QR codes need to point the user the store page of your app. Additionally, they need to pass more data in by using the referrer URL parameter.

One of your links could look like this:

https://play.google.com/store/apps/details?id=com.example.okariotisapp&referrer=qrdata%3DQR-a

This URL consists of the following parts:

  • Base URL for the store page of an app on the Google Play Store:
    • https://play.google.com/store/apps/details?id=
  • Your application ID:
    • com.example.okariotisapp
  • referrer URL parameter:
    • &referrer=
  • Your freely choosable key-value data. In this case with qrdata as a key and QR-a as a value (note, that %3D is the HTML URL enconding for =):
    • qrdata%3DQR-a

Access referrer data in the app

The easiest way to access the referrer data is using the Play Install Referrer Library.

Add the dependency to the library and establish a connection to the Play Store once your app has been installed and started. Inside the onInstallReferrerSetupFinished callback, access the referrer data if the responseCode for the connection is equal to InstallReferrerResponse.OK

private lateinit var referrerClient: InstallReferrerClient

referrerClient = InstallReferrerClient.newBuilder(this).build()
referrerClient.startConnection(object : InstallReferrerStateListener {

    override fun onInstallReferrerSetupFinished(responseCode: Int) {
        when (responseCode) {
            InstallReferrerResponse.OK -> {
                // Connection established, access your data
                val response: ReferrerDetails = referrerClient.installReferrer
                val referrerUrl: String = response.installReferrer
                //TODO: Evaluate which Play Store URL was used to install your app
            }
            InstallReferrerResponse.FEATURE_NOT_SUPPORTED -> {
                // API not available on the current Play Store app.
            }
            InstallReferrerResponse.SERVICE_UNAVAILABLE -> {
                // Connection couldn't be established.
            }
        }
    }

    override fun onInstallReferrerServiceDisconnected() {
        // Try to restart the connection on the next request to
        // Google Play by calling the startConnection() method.
    }
})

Source: https://developer.android.com/google/play/installreferrer/library

Testing

In order to test this, you will need to upload your app to the Play Store into the internal, alpha, beta or production track. As far as I am aware, this does not work with internal app sharing.

Upvotes: 3

Related Questions