Skip to content

Commit 2639ffc

Browse files
committed
feat: added commerce events to android
1 parent 4a4c38c commit 2639ffc

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

src/android/io/branch/BranchSDK.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
import io.branch.indexing.BranchUniversalObject;
2121
import io.branch.referral.Branch;
2222
import io.branch.referral.BranchError;
23+
import io.branch.referral.BranchViewHandler;
2324
import io.branch.referral.SharingHelper;
25+
import io.branch.referral.util.CommerceEvent;
26+
import io.branch.referral.util.CurrencyType;
27+
import io.branch.referral.util.Product;
28+
import io.branch.referral.util.ProductCategory;
2429
import io.branch.referral.util.ShareSheetStyle;
2530

2631
public class BranchSDK extends CordovaPlugin {
@@ -107,6 +112,13 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
107112
}
108113
cordova.getThreadPool().execute(r);
109114
return true;
115+
} else if (action.equals("sendCommerceEvent")) {
116+
if (args.length() < 1 && args.length() > 2) {
117+
callbackContext.error(String.format("Parameter mismatched. 1-2 is required but %d is given", args.length()));
118+
return false;
119+
}
120+
cordova.getThreadPool().execute(r);
121+
return true;
110122
} else if (action.equals("getFirstReferringParams")) {
111123
cordova.getThreadPool().execute(r);
112124
return true;
@@ -602,6 +614,91 @@ private void userCompletedAction(String action, JSONObject metaData, CallbackCon
602614

603615
}
604616

617+
/**
618+
* <p>A void call to indicate that the user has performed a specific commerce event and for that to be
619+
* reported to the Branch API.</p>
620+
*
621+
* @param action A {@link String} value to be passed as an commerce event that the user has
622+
* carried out.
623+
* @param metaData A {@link JSONObject} containing app-defined meta-data to be attached to a
624+
* user action that has just been completed.
625+
* @param callbackContext A callback to execute at the end of this method
626+
*/
627+
private void sendCommerceEvent(JSONObject action, JSONObject metaData, CallbackContext callbackContext) throws JSONException {
628+
629+
CommerceEvent commerce = new CommerceEvent();
630+
Iterator<String> keys = action.keys();
631+
while(keys.hasNext()){
632+
String key = keys.next();
633+
String val;
634+
try {
635+
val = action.getString(key);
636+
} catch (JSONException e) {
637+
e.printStackTrace();
638+
callbackContext.error("Invalid key-value for " + key);
639+
return;
640+
}
641+
if (key.equals("revenue")) {
642+
commerce.setRevenue(Double.parseDouble(val));
643+
} else if (key.equals("currency")) {
644+
commerce.setCurrencyType(CurrencyType.values()[Integer.parseInt(val)]);
645+
} else if (key.equals("transactionID")) {
646+
commerce.setTransactionID(val);
647+
} else if (key.equals("coupon")) {
648+
commerce.setCoupon(val);
649+
} else if (key.equals("shipping")) {
650+
commerce.setShipping(Double.parseDouble(val));
651+
} else if (key.equals("tax")) {
652+
commerce.setTax(Double.parseDouble(val));
653+
} else if (key.equals("affiliation")) {
654+
commerce.setAffiliation(val);
655+
} else if (key.equals("products")) {
656+
JSONArray products = new JSONArray();
657+
try {
658+
products = action.getJSONArray(key);
659+
} catch (JSONException e) {
660+
e.printStackTrace();
661+
callbackContext.error("Invalid key-value for " + key);
662+
return;
663+
}
664+
for (int i = 0; i < products.length(); ++i) {
665+
Product product = new Product();
666+
JSONObject item = products.getJSONObject(i);
667+
keys = item.keys();
668+
while(keys.hasNext()) {
669+
key = keys.next();
670+
try {
671+
val = item.getString(key);
672+
} catch (JSONException e) {
673+
e.printStackTrace();
674+
callbackContext.error("Invalid key-value for product for " + key);
675+
return;
676+
}
677+
if (key.equals("sku")) {
678+
product.setSku(val);
679+
} else if (key.equals("name")) {
680+
product.setName(val);
681+
} else if (key.equals("price")) {
682+
product.setPrice(Double.parseDouble(val));
683+
} else if (key.equals("quantity")) {
684+
product.setQuantity(Integer.parseInt(val));
685+
} else if (key.equals("brand")) {
686+
product.setBrand(val);
687+
} else if (key.equals("category")) {
688+
product.setCategory(ProductCategory.values()[Integer.parseInt(val)]);
689+
} else if (key.equals("variant")) {
690+
product.setVariant(val);
691+
}
692+
}
693+
commerce.addProduct(product);
694+
}
695+
}
696+
}
697+
698+
this.instance.sendCommerceEvent(commerce, metaData, new BranchViewEventsListener(callbackContext));
699+
700+
}
701+
605702
/**
606703
* <p>Gets the credit history of the specified bucket and triggers a callback to handle the
607704
* response.</p>
@@ -645,6 +742,35 @@ public BranchUniversalObjectWrapper(BranchUniversalObject branchUniversalObj) {
645742
//----------- INNER CLASS LISTENERS ------------//
646743
//////////////////////////////////////////////////
647744

745+
protected class BranchViewEventsListener implements BranchViewHandler.IBranchViewEvents {
746+
747+
private CallbackContext _callbackContext;
748+
749+
public BranchViewEventsListener(CallbackContext callbackContext) {
750+
callbackContext.success("Success");
751+
}
752+
753+
@Override
754+
public void onBranchViewVisible(String action, String branchViewID) {
755+
756+
}
757+
758+
@Override
759+
public void onBranchViewAccepted(String action, String branchViewID) {
760+
761+
}
762+
763+
@Override
764+
public void onBranchViewCancelled(String action, String branchViewID) {
765+
766+
}
767+
768+
@Override
769+
public void onBranchViewError(int errorCode, String errorMsg, String action) {
770+
771+
}
772+
}
773+
648774
protected class SessionListener implements Branch.BranchReferralInitListener {
649775
private CallbackContext _callbackContext;
650776

@@ -1104,6 +1230,8 @@ public void run() {
11041230
} else if (this.args.length() == 1) {
11051231
userCompletedAction(this.args.getString(0), this.callbackContext);
11061232
}
1233+
} else if (this.action.equals("sendCommerceEvent")) {
1234+
sendCommerceEvent(this.args.getJSONObject(0), this.args.getJSONObject(1), this.callbackContext);
11071235
} else if (this.action.equals("getFirstReferringParams")) {
11081236
getFirstReferringParams(this.callbackContext);
11091237
} else if (this.action.equals("getLatestReferringParams")) {

0 commit comments

Comments
 (0)