Android - How to Get Install Referrer Programmatically

Android - Is it possible to get install referrer programmatically

You can use com.android.vending.INSTALL_REFERRER.

The Google Play com.android.vending.INSTALL_REFERRER Intent is
broadcast when an app is installed from the Google Play Store.

Add this receiver to AndroidManifest.xml

<receiver
android:name="com.example.android.InstallReferrerReceiver"
android:exported="true"
android:permission="android.permission.INSTALL_PACKAGES">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>

Create a BroadcastReceiver:

public class InstallReferrerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String referrer = intent.getStringExtra("referrer");

//Use the referrer
}
}

You can test the referral tracking following the steps of this answer.

How to test install referrer with Google's installreferrer library?

With the InstallReferrerClient, there doesn't seem to be any BroadcastReceiver registered in the AndroidManifest.xml. The library just binds to the system's install referrer service ...

private static final String SERVICE_PACKAGE_NAME = "com.android.vending";
private static final String SERVICE_NAME = "com.google.android.finsky.externalreferrer.GetInstallReferrerService";
private static final String SERVICE_ACTION_NAME = "com.google.android.finsky.BIND_GET_INSTALL_REFERRER_SERVICE";

The client receives referrer utm_source=google-play&utm_medium=organic upon manual install. There is no BroadcastReceiver exposed (but the InstallReferrerService should have one).

The keys of the raw Intent Bundle are: install_referrer, referrer_click_timestamp_seconds and install_begin_timestamp_seconds if you want to try emulating it - but the onInstallReferrerSetupFinished() callback will deliver the result indirectly.

The documentation also states:

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. Your app can listen to the system broadcast Intent.ACTION_PACKAGE_FIRST_LAUNCH to identify the app's first execution.


So this should be an intent-filter for action Intent.ACTION_PACKAGE_FIRST_LAUNCH, which subsequently connects the InstallReferrerClient to the InstallReferrerService. One cannot trigger Intent.ACTION_PACKAGE_FIRST_LAUNCH with adb, because it filters for a "protected broadcast action string", therefore it might only be triggered when installing from Play Store.

The implementation, according to the documentation, might look alike:

AndroidManifest.xml:

<receiver
android:name=".receiver.PackageStatusReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH"/>
</intent-filter>
</receiver>

PackageStatusReceiver.java:

public class PackageStatusReceiver extends BroadcastReceiver implements InstallReferrerStateListener {

protected static final String LOG_TAG = PackageStatusReceiver.class.getSimpleName();

private InstallReferrerClient referrerClient;

@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction() != null) {
if(intent.getAction().equals(Intent.ACTION_PACKAGE_FIRST_LAUNCH)) {
this.referrerClient = InstallReferrerClient.newBuilder(context).build();
this.referrerClient.startConnection(this);
}
}
}

@Override
public void onInstallReferrerSetupFinished(int responseCode) {
switch (responseCode) {
case InstallReferrerClient.InstallReferrerResponse.OK:
Log.d(LOG_TAG, "InstallReferrer Response.OK");
try {
ReferrerDetails response = referrerClient.getInstallReferrer();
String referrer = response.getInstallReferrer();
long clickTimestamp = response.getReferrerClickTimestampSeconds();
long installTimestamp = response.getInstallBeginTimestampSeconds();
Log.d(LOG_TAG, "InstallReferrer " + referrer);
referrerClient.endConnection();
} catch (RemoteException e) {
Log.e(LOG_TAG, "" + e.getMessage());
}
break;
case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
Log.w(LOG_TAG, "InstallReferrer Response.FEATURE_NOT_SUPPORTED");
break;
case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
Log.w(LOG_TAG, "InstallReferrer Response.SERVICE_UNAVAILABLE");
break;
case InstallReferrerClient.InstallReferrerResponse.SERVICE_DISCONNECTED:
Log.w(LOG_TAG, "InstallReferrer Response.SERVICE_DISCONNECTED");
break;
case InstallReferrerClient.InstallReferrerResponse.DEVELOPER_ERROR:
Log.w(LOG_TAG, "InstallReferrer Response.DEVELOPER_ERROR");
break;
}
}

@Override
public void onInstallReferrerServiceDisconnected() {
Log.w(LOG_TAG, "InstallReferrer onInstallReferrerServiceDisconnected()");
}
}

To test this, you'd need referrer links to the Play Store and then install the package through them... else only the default referrer will be logged (besides the intent cannot even be triggered, when properly implementing the client).

How to pass install referrer string to my App

Try to store data in SharedPrefs using:

 SharedPreferences preferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
Editor preferencesEditor = preferences.edit();
preferencesEditor.putString(Constants.REFERRAL, referrer);
preferencesEditor.commit();

How to get Google Play referer in an Android application

You are looking for Campaign Measurement. In the documentation, it discusses how using INSTALL_REFERRER will help you determine which source is sending users to your App in the Google Play Store.

It's as simple as placing a receiver in your AndroidManifest and modifying your App's Google Play URLs.

From the docs:

Google Play Campaign Attribution

Google Play Campaign Measurement allows you to see which campaigns and traffic sources are sending users to download your app from the Google Play Store. It is recommended that all developers implement Google Play Store Campaign Measurement.

Implementing Google Play Campaign Attribution

When your app is downloaded from Google Play Store, the Play Store app broadcasts an INSTALL_REFERRER intent to your app during installation. This intent contains the value of the referrer parameter of the link used to reach your app's Google Play Store page, if one was present.

To attribute an app download to a campaign, you must add a referrer parameter to any links that point to Google Play Store, and add a BroadcastReceiver to your app to receive and set the campaign information contained in the intent on your Google Analytics tracker.

It is recommended that most developers use the BroadcastReceiver provided with the SDK. To implement Google Play Store Campaign Measurement using the included receiver:

1. Add the Google Analytics receiver to your AndroidManifest.xml file. To add the Google Analytics receiver to the manifest, copy and paste the following markup:

    <application>
<!-- Used for Google Play Store Campaign Measurement-->
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.CampaignTrackingService"
android:enabled="true"
android:exported="false" />
</application>


  1. Add Google Analytics Campaign Parameters to Google Play URLs

    Next, add a referrer parameter to any URLs that will be linking directly to Google Play Store and set the value of that parameter to a string of Google Analytics campaign parameters that describe the source, as in this example:

    https://play.google.com/store/apps/details?id=com.example.application
    &referrer=utm_source%3Dgoogle
    %26utm_medium%3Dcpc
    %26utm_term%3Drunning%252Bshoes
    %26utm_content%3Dlogolink
    %26utm_campaign%3Dspring_sale


To learn how to build a campaign parameter strings, use the Google Play URL Builder, or consult the Campaign Parameters reference section.

Testing Google Play Campaign Attribution

To verify that your Google Play Campaign Measurement implementation is working as expected before publishing your app, use the Testing Google Play Campaign Attribution Solution Guide.

Also, see this similar post.

Play Install Referrer Library

If you are using firebase-core SDK for Firebase Analytics then remove it & exclue play-services measurement sdk.

As per Firebase SDK release notes:

No longer add the Android library com.google.firebase:firebase-core.
This SDK included the Firebase SDK for Google Analytics.
Now, to use Analytics (or any of the Firebase products that require or recommend the use of Analytics),
you need to explicitly add the Analytics dependency:

implementation ("com.google.firebase:firebase-analytics:17.2.1"){
exclude group: 'com.google.android.gms', module: 'play-services-measurement'
exclude group: 'com.google.android.gms', module: 'play-services-measurement-sdk'
exclude group: 'com.google.android.gms', module: 'play-services-measurement-impl'
}

This might solve your issue.

Android sharing google play install referrer url in social networks?

Everything is correct. You can test your app configuration running this command in the terminal:

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER 
-n your.package.name/path.to.receiver --es referrer
--es referrer "EXTRA_STRING_VALUE"

For example if your package name is com.hellochatty and the path of your receiver is com.ex.MyReceiver the test command will be:

adb shell am broadcast -a com.android.vending.INSTALL_REFERRER 
-n "com.hellochatty/com.ex.MyReceiver"
--es referrer "tracking_id=123456789"


Related Topics



Leave a reply



Submit