Skip to content

Commit

Permalink
[CORE-1951] Add initial_referrer for every session (#918)
Browse files Browse the repository at this point in the history
* Add initial_referrer for every session

- Added initial_referrer-> EXTRA_REFERRER to every OPEN/INSTALL session request.

* removed unused package from import

* Formatted the code

* Code clean up

- Removed extra space and unused imports
- Added null check and sdk version check

* Added negation for if block

* Replace with Appcompact referrer

- Replaced Activity.referrer with ActivityCompat.referrer
- Used TextUtils for null check

* Added comment under Defines.IntentKeys

- Added comment about usage of intent keys EXTRA_REFERRER and EXTRA_REFERRER_NAME
  • Loading branch information
msiddiq-branch authored Jul 16, 2021
1 parent a6bfb33 commit 0fbae8f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ public void onInitFinished(JSONObject referringParams, BranchError error) {
}
}
}).reInit();


}

@Override
Expand All @@ -537,4 +539,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
startActivity(i);
}
}


}
7 changes: 7 additions & 0 deletions Branch-SDK/src/main/java/io/branch/referral/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -27,6 +28,7 @@
import android.text.TextUtils;
import android.view.View;

import androidx.core.app.ActivityCompat;
import io.branch.referral.Defines.PreinstallKey;
import io.branch.referral.ServerRequestGetLATD.BranchLastAttributedTouchDataListener;
import org.json.JSONArray;
Expand Down Expand Up @@ -2870,6 +2872,11 @@ public void init() {

Activity activity = branch.getCurrentActivity();
Intent intent = activity != null ? activity.getIntent() : null;

if (ActivityCompat.getReferrer(activity) != null) {
PrefHelper.getInstance(activity).setInitialReferrer(ActivityCompat.getReferrer(activity).toString());
}

if (uri != null) {
branch.readAndStripParam(uri, activity);
} else if (isReInitializing && branch.isRestartSessionRequested(intent)) {
Expand Down
5 changes: 5 additions & 0 deletions Branch-SDK/src/main/java/io/branch/referral/Defines.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum Jsonkey {
PlayAutoInstalls("play-auto-installs"), //UTM_Source set by Xiaomi
UTMCampaign("utm_campaign"),
UTMMedium("utm_medium"),
InitialReferrer("initial_referrer"),

Bucket("bucket"),
DefaultBucket("default"),
Expand Down Expand Up @@ -358,6 +359,10 @@ public enum IntentKeys {
/* Key to indicate whether the Activity was launched by Branch or not. */
AutoDeepLinked("io.branch.sdk.auto_linked");

// The below intent keys are also used to extract data from the intent (via ActivityCompact.getReferrer())
// public static final String EXTRA_REFERRER = "android.intent.extra.REFERRER";
// public static final String EXTRA_REFERRER_NAME = "android.intent.extra.REFERRER_NAME";

private final String key;

IntentKeys(String key) {
Expand Down
22 changes: 21 additions & 1 deletion Branch-SDK/src/main/java/io/branch/referral/PrefHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class PrefHelper {
private static final String KEY_INSTALL_PARAMS = "bnc_install_params";
private static final String KEY_USER_URL = "bnc_user_url";
private static final String KEY_LATD_ATTRIBUTION_WINDOW = "bnc_latd_attributon_window";
private static final String KEY_INITIAL_REFERRER = "bnc_initial_referrer";

private static final String KEY_BUCKETS = "bnc_buckets";
private static final String KEY_CREDIT_BASE = "bnc_credit_base_";
Expand Down Expand Up @@ -938,7 +939,26 @@ public int getLATDAttributionWindow(){
return getInteger(KEY_LATD_ATTRIBUTION_WINDOW,
ServerRequestGetLATD.defaultAttributionWindow);
}


/**
* Persist the android.intent.extra.REFERRER value
*
* @param initialReferrer android.intent.extra.REFERRER
*/
public void setInitialReferrer(String initialReferrer) {
setString(KEY_INITIAL_REFERRER, initialReferrer);
}

/**
* Get the persisted android.intent.extra.REFERRER value
*
* @return {@link String} android.intent.extra.REFERRER
*/
public String getInitialReferrer() {
return getString(KEY_INITIAL_REFERRER);
}


// ALL GENERIC CALLS

private String serializeArrayList(ArrayList<String> strings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.CallSuper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.locks.ReentrantLock;

import io.branch.referral.validators.DeepLinkRoutingValidator;

Expand Down Expand Up @@ -55,6 +52,9 @@ protected void setPost(JSONObject post) throws JSONException {
if (!DeviceInfo.isNullOrEmptyOrBlank(appVersion)) {
post.put(Defines.Jsonkey.AppVersion.getKey(), appVersion);
}
if(!TextUtils.isEmpty(prefHelper_.getInitialReferrer()) && !prefHelper_.getInitialReferrer().equals(PrefHelper.NO_STRING_VALUE)) {
post.put(Defines.Jsonkey.InitialReferrer.getKey(), prefHelper_.getInitialReferrer());
}
post.put(Defines.Jsonkey.FaceBookAppLinkChecked.getKey(), prefHelper_.getIsAppLinkTriggeredInit());
post.put(Defines.Jsonkey.Debug.getKey(), Branch.isDeviceIDFetchDisabled());

Expand Down Expand Up @@ -117,6 +117,7 @@ public void onRequestSucceeded(ServerResponse response, Branch branch) {
prefHelper_.setIsAppLinkTriggeredInit(false);
prefHelper_.setInstallReferrerParams(PrefHelper.NO_STRING_VALUE);
prefHelper_.setIsFullAppConversion(false);
prefHelper_.setInitialReferrer(PrefHelper.NO_STRING_VALUE);

if (prefHelper_.getLong(PrefHelper.KEY_PREVIOUS_UPDATE_TIME) == 0) {
prefHelper_.setLong(PrefHelper.KEY_PREVIOUS_UPDATE_TIME, prefHelper_.getLong(PrefHelper.KEY_LAST_KNOWN_UPDATE_TIME));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private void onTrackingDisabled(Context context) {
prefHelper.setInstallReferrerParams(PrefHelper.NO_STRING_VALUE);
prefHelper.setGooglePlayReferrer(PrefHelper.NO_STRING_VALUE);
prefHelper.setGoogleSearchInstallIdentifier(PrefHelper.NO_STRING_VALUE);
prefHelper.setInitialReferrer(PrefHelper.NO_STRING_VALUE);
prefHelper.setExternalIntentUri(PrefHelper.NO_STRING_VALUE);
prefHelper.setExternalIntentExtra(PrefHelper.NO_STRING_VALUE);
prefHelper.setSessionParams(PrefHelper.NO_STRING_VALUE);
Expand Down

0 comments on commit 0fbae8f

Please sign in to comment.