void handleGooglePlayReferrer() {
// Google recommends only calling this API the first time you need it:
// https://developer.android.com/google/play/installreferrer/library#install-referrer
// Store a boolean in SharedPreferences to ensure we only call it once.
final SharedPreferences prefs = getSharedPreferences("acquisition", 0);
if (prefs.getBoolean("referrerHasBeenProcessed", false)) {
final InstallReferrerClient referrerClient = InstallReferrerClient.newBuilder(getApplicationContext()).build();
referrerClient.startConnection(new InstallReferrerStateListener() {
private boolean complete = false;
public void onInstallReferrerSetupFinished(int responseCode) {
case InstallReferrerClient.InstallReferrerResponse.OK:
// connection is established
final ReferrerDetails details = referrerClient.getInstallReferrer();
// pass the install referrer url to the SDK
MobileServices.processGooglePlayInstallReferrerUrl(details.getInstallReferrer());
} catch (final RemoteException ex) {
Log.w("Acquisition - RemoteException while retrieving referrer information (%s)", ex.getLocalizedMessage() == null ? "unknown" : ex.getLocalizedMessage());
referrerClient.endConnection();
case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
// API not available in the Play Store app - nothing to do here
referrerClient.endConnection();
public void onInstallReferrerServiceDisconnected() {
// something went wrong trying to get a connection, try again
referrerClient.startConnection(this);
SharedPreferences.Editor editor = getSharedPreferences("acquisition", 0).edit();
editor.putBoolean("referrerHasBeenProcessed", true);