Deferred deep linking on Android

When someone taps your Onelink and installs your app from Google Play, the Google Play Install Referrer API makes the link they tapped, with extras such as ?campaign=3for2, available to your app for 90 days after the install. Deferred deep linking (Android) is included on the Team plan and above.

Requires changes to your app

  1. Step 1

    Turn it on

    In the Onelink editor, under Other settings, tick Enable Android deferred deep linking (Play install referrer) and save. Within 5 minutes, your app receives a referrer like this after an install:
    onelink_url=https%3A%2F%2Fonelink.to%2Fautumn%3Fcampaign%3D3for2
    onelink_url is the link the visitor tapped, with its extras. Put extras after ?: anything after # stays in the visitor's browser and never reaches Onelink.to. If the Google Play link or the tapped link already carries a referrer, that one reaches your app unchanged instead.
    Onelink.to keeps the referrer within 512 characters, URL-encoded, the limit Google documents. When the tapped link with its extras would make it longer, the extras are left out of onelink_url.
  2. Step 2

    Add the library

    Add the Play Install Referrer Library to the app module’s build.gradle.kts:
    implementation("com.android.installreferrer:installreferrer:2.2")
  3. Step 3

    Read the referrer on first launch

    Google Play keeps the referrer for 90 days after the install. Call this from your launch activity. It reads the referrer once and hands over the tapped link:
    fun readInstallReferrer(context: Context, open: (Uri) -> Unit) {
        val prefs = context.getSharedPreferences("install_referrer", Context.MODE_PRIVATE)
        if (prefs.getBoolean("read", false)) return
        val client = InstallReferrerClient.newBuilder(context).build()
        client.startConnection(object : InstallReferrerStateListener {
            override fun onInstallReferrerSetupFinished(responseCode: Int) {
                if (responseCode == InstallReferrerClient.InstallReferrerResponse.OK) {
                    val referrer = client.installReferrer.installReferrer
                    prefs.edit().putBoolean("read", true).apply()
                    Uri.Builder().encodedQuery(referrer).build()
                        .getQueryParameter("onelink_url")
                        ?.let { open(Uri.parse(it)) }
                }
                client.endConnection()
            }
    
            override fun onInstallReferrerServiceDisconnected() {}
        })
    }
  4. Step 4

    Act on the link

    Read what your app needs from the link: an extra such as campaign=3for2 to apply an offer, or the path to open a screen. If your app already opens links on your own domain, pass the link there — see Open links in your app (deep linking). Anyone can add extras to a link, so check them before acting on them, as you would any link from outside the app.
  5. Step 5

    Test with a real install

    Google Play sets the referrer only when the install follows a tap on your Onelink, and installs from internal and closed testing tracks may arrive without it. Once the version that reads the referrer is live on Google Play, remove the app from a phone, tap your Onelink on that phone, install, and open the app.

Features in this guide