You can use the Google Play Store’s Install Referrer API to securely retrieve referral content from Google Play.
You can get the following information by using referral API as below:
- The referrer URL of the installed package.
- The timestamp, in seconds, of when the referrer click happened.
- The timestamp, in seconds, of when the installation began.
- The app’s version at the time when the app was first installed.
- Whether the user has interacted with your app’s instant experience in the past 7 days.
You can use Referrer data to know which campaigns and traffic sources are sending users to download your app from the Google Play Store.
Add Dependency
You need to add Play Store’s Install Referrer dependency in the module build Gradle file.
1 2 3 4 |
dependencies { //Play Store's Install Referrer dependency implementation 'com.android.installreferrer:installreferrer:2.1' } |
Referral API Implementations
You can add InstallReferrerClient in Activity or other class to retrieve referral data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.android.installreferrer.api.InstallReferrerClient import com.android.installreferrer.api.InstallReferrerClient.InstallReferrerResponse import com.android.installreferrer.api.InstallReferrerStateListener import com.android.installreferrer.api.ReferrerDetails class HomeActivity : AppCompatActivity() { private lateinit var referrerClient: InstallReferrerClient override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) referrerClient = InstallReferrerClient.newBuilder(this).build() referrerClient.startConnection(object : InstallReferrerStateListener { override fun onInstallReferrerSetupFinished(responseCode: Int) { when (responseCode) { InstallReferrerResponse.OK -> { // Connection established. getReferrerData() } 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. } }) } private fun getReferrerData() { val response: ReferrerDetails = referrerClient.installReferrer val referrerUrl: String = response.installReferrer val referrerClickTime: Long = response.referrerClickTimestampSeconds val appInstallTime: Long = response.installBeginTimestampSeconds val instantExperienceLaunched: Boolean = response.googlePlayInstantParam referrerClient.endConnection() } } |
Happy Learning 🙂
Reference –