Skip to content

Commit

Permalink
Support for installreferrer library
Browse files Browse the repository at this point in the history
Adding support for installreferrer library for getting  the referrer
info.
This is need `installreferrer` lib added to the application.
This will be working only with compatible version of Play store app. In
case of an error this fallback to the install-referrer broadcast
  • Loading branch information
sojanpr committed Jan 26, 2018
1 parent 061e06e commit 2de9bd8
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 44 deletions.
7 changes: 5 additions & 2 deletions Branch-SDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ dependencies {
compile 'com.android.support:support-annotations:22.2.0'
compile 'com.crashlytics.sdk.android:answers-shim:0.0.6'

// This is an optional dependency
//Please note that the Branch SDK does not require Firebase to operate. This dependency is listed here so there will not be build errors,
// --- optional dependencies -----
//Please note that the Branch SDK does not require any of the below optional dependencies to operate. This dependency is listed here so there will not be build errors,
// but the library is *not* added to your app unless you do so yourself. Please check the code in gradle-mvn-push script to see how this works

compile 'com.google.firebase:firebase-appindexing:10.0.1'
compile 'com.android.installreferrer:installreferrer:1.0'

}

android {
Expand Down
2 changes: 1 addition & 1 deletion Branch-SDK/src/io/branch/referral/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2314,7 +2314,7 @@ private void registerAppInit(BranchReferralInitListener
}
if (checkInstallReferrer_ && request instanceof ServerRequestRegisterInstall) {
request.addProcessWaitLock(ServerRequest.PROCESS_WAIT_LOCK.INSTALL_REFERRER_FETCH_WAIT_LOCK);
InstallListener.captureInstallReferrer(playStoreReferrerFetchTime, this);
InstallListener.captureInstallReferrer(context_, playStoreReferrerFetchTime, this);
}

registerInstallOrOpen(request, callback);
Expand Down
8 changes: 5 additions & 3 deletions Branch-SDK/src/io/branch/referral/Defines.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public enum Jsonkey {
LinkClickID("link_click_id"),
GoogleSearchInstallReferrer("google_search_install_referrer"),
GooglePlayInstallReferrer("install_referrer_extras"),
ClickedReferrerTimeStamp("clicked_referrer_ts"),
InstallBeginTimeStamp("install_begin_ts"),
FaceBookAppLinkChecked("facebook_app_link_checked"),
BranchLinkUsed("branch_used"),
ReferringBranchIdentity("referring_branch_identity"),
Expand Down Expand Up @@ -137,7 +139,7 @@ public enum Jsonkey {
Environment("environment"),
InstantApp("INSTANT_APP"),
NativeApp("FULL_APP"),

TransactionID("transaction_id"),
Currency("currency"),
Revenue("revenue"),
Expand All @@ -147,7 +149,7 @@ public enum Jsonkey {
Affiliation("affiliation"),
Description("description"),
SearchQuery("search_query"),

Name("name"),
CustomData("custom_data"),
EventData("event_data"),
Expand Down Expand Up @@ -175,7 +177,7 @@ public enum Jsonkey {
ImageCaptions("$image_captions"),
Condition("$condition"),
CreationTimestamp("$creation_timestamp");


private String key = "";

Expand Down
133 changes: 119 additions & 14 deletions Branch-SDK/src/io/branch/referral/InstallListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;

/**
* <p> Class for listening installation referrer params. Add this class to your manifest in order to get a referrer info </p>
* <p>
* <p> Add to InstallListener to manifest as follows
* <!-- <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
* <intent-filter>
* <action android:name="com.android.vending.INSTALL_REFERRER" />
* </intent-filter>
* </receiver> -->
* <p> Class for listening installation referrer params. Install params are captured by either of the following methods
* 1) Add the install referrer library to your application "com.android.installreferrer:installreferrer" (Recommended)
* dependencies {
* compile 'com.android.installreferrer:installreferrer:1.0'
* }
*
* 2) Add to a braodcast listener to manifest as follows to receive Install Referrer
* <receiver android:name="io.branch.referral.InstallListener" android:exported="true">
* <intent-filter>
* <action android:name="com.android.vending.INSTALL_REFERRER" />
* </intent-filter>
* </receiver> -->
* </p>
*
*/
public class InstallListener extends BroadcastReceiver {

Expand All @@ -30,16 +40,20 @@ public class InstallListener extends BroadcastReceiver {


private static boolean isWaitingForReferrer;
/* Specifies if the install referrer client is available */
private static boolean isReferrerClientAvailable;
// PRS : In case play store referrer get reported really fast as google fix bugs , this implementation will let the referrer parsed and stored
// This will be reported when SDK ask for it
private static boolean unReportedReferrerAvailable;

public static void captureInstallReferrer(final long maxWaitTime, IInstallReferrerEvents installReferrerFetch) {
public static void captureInstallReferrer(Context context, final long maxWaitTime, IInstallReferrerEvents installReferrerFetch) {
callback_ = installReferrerFetch;
if (unReportedReferrerAvailable) {
reportInstallReferrer();
} else {
isWaitingForReferrer = true;
ReferrerClientWrapper referrerClientWrapper = new ReferrerClientWrapper(context);
isReferrerClientAvailable = referrerClientWrapper.getReferrerUsingReferrerClient();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Expand All @@ -52,13 +66,97 @@ public void run() {
@Override
public void onReceive(Context context, Intent intent) {
String rawReferrerString = intent.getStringExtra("referrer");
processRawReferrer(context, rawReferrerString);
processReferrerInfo(context, rawReferrerString, 0, 0);
if (isWaitingForReferrer && !isReferrerClientAvailable) { // Still wait for referrer client to get the time stamps if it is active
reportInstallReferrer();
}
}

private static void onReferrerClientFinished(Context context, String rawReferrerString, long clickTS, long InstallBeginTS) {
processReferrerInfo(context, rawReferrerString, clickTS, InstallBeginTS);
if (isWaitingForReferrer) {
reportInstallReferrer();
}
}

private void processRawReferrer(Context context, String rawReferrerString) {
private static void onReferrerClientError() {
isReferrerClientAvailable = false;
}

/**
* <p>
* Class for getting the referrer info using the install referrer client. This is need `installreferrer` lib added to the application.
* This will be working only with compatible version of Play store app. In case of an error this fallback to the install-referrer broadcast
* </p>
*/
private static class ReferrerClientWrapper {
private Object mReferrerClient; // Class may be unknown at the time on load if the `installreferrer` is missing
private Context context_;

private ReferrerClientWrapper(Context context) {
this.context_ = context;
}

private boolean getReferrerUsingReferrerClient() {
boolean isReferrerClientAvailable = false;
try {
InstallReferrerClient referrerClient = InstallReferrerClient.newBuilder(context_).build();
mReferrerClient = referrerClient;
referrerClient.startConnection(new InstallReferrerStateListener() {
@Override
public void onInstallReferrerSetupFinished(int responseCode) {
switch (responseCode) {
case InstallReferrerClient.InstallReferrerResponse.OK:
try {
if (mReferrerClient != null) {
ReferrerDetails response = ((InstallReferrerClient) mReferrerClient).getInstallReferrer();
String rawReferrer = null;
long clickTimeStamp = 0L;
long installBeginTimeStamp = 0L;
if (response != null) {
rawReferrer = response.getInstallReferrer();
clickTimeStamp = response.getReferrerClickTimestampSeconds();
installBeginTimeStamp = response.getInstallBeginTimestampSeconds();
}
onReferrerClientFinished(context_, rawReferrer, clickTimeStamp, installBeginTimeStamp);
}
} catch (RemoteException ex) {
PrefHelper.Debug("BranchSDK", ex.getMessage());
onReferrerClientError();
}
break;
case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
// API not available on the current Play Store app
onReferrerClientError();
break;
case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
// Connection could not be established
onReferrerClientError();
break;
}
}

@Override
public void onInstallReferrerServiceDisconnected() {
onReferrerClientError();
}
});
isReferrerClientAvailable = true;
} catch (Throwable ex) {
PrefHelper.Debug("BranchSDK", ex.getMessage());
}
return isReferrerClientAvailable;
}
}

private static void processReferrerInfo(Context context, String rawReferrerString, long referrerClickTS, long installClickTS) {
PrefHelper prefHelper = PrefHelper.getInstance(context);
if (referrerClickTS > 0) {
prefHelper.setLong(PrefHelper.KEY_REFERRER_CLICK_TS, referrerClickTS);
}
if (installClickTS > 0) {
prefHelper.setLong(PrefHelper.KEY_REFERRER_CLICK_TS, installClickTS);
}
if (rawReferrerString != null) {
try {
rawReferrerString = URLDecoder.decode(rawReferrerString, "UTF-8");
Expand All @@ -77,9 +175,6 @@ private void processRawReferrer(Context context, String rawReferrerString) {
}
}
}

PrefHelper prefHelper = PrefHelper.getInstance(context);

if (referrerMap.containsKey(Defines.Jsonkey.LinkClickID.getKey())) {
installID_ = referrerMap.get(Defines.Jsonkey.LinkClickID.getKey());
prefHelper.setLinkClickIdentifier(installID_);
Expand Down Expand Up @@ -110,12 +205,22 @@ public static String getInstallationID() {
return installID_;
}

public static long getReferrerClickTS(Context context) {
return PrefHelper.getInstance(context).getLong(PrefHelper.KEY_REFERRER_CLICK_TS);
}

public static long getInstallBeginTS(Context context) {
return PrefHelper.getInstance(context).getLong(PrefHelper.KEY_INSTALL_BEGIN_TS);
}

private static void reportInstallReferrer() {
unReportedReferrerAvailable = true;
if (callback_ != null) {
callback_.onInstallReferrerEventsFinished();
callback_ = null;
unReportedReferrerAvailable = false;
isWaitingForReferrer = false;
isReferrerClientAvailable = false;
}
}

Expand Down
3 changes: 3 additions & 0 deletions Branch-SDK/src/io/branch/referral/PrefHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public class PrefHelper {
private static final String KEY_INSTALL_REFERRER = "bnc_install_referrer";
private static final String KEY_IS_FULL_APP_CONVERSION = "bnc_is_full_app_conversion";
private static final String KEY_LIMIT_FACEBOOK_TRACKING = "bnc_limit_facebook_tracking";

static final String KEY_REFERRER_CLICK_TS = "bnc_referrer_click_ts";
static final String KEY_INSTALL_BEGIN_TS = "bnc_install_begin_ts";


private static String Branch_Key = null;
Expand Down

0 comments on commit 2de9bd8

Please sign in to comment.