|
20 | 20 | import io.branch.indexing.BranchUniversalObject;
|
21 | 21 | import io.branch.referral.Branch;
|
22 | 22 | import io.branch.referral.BranchError;
|
| 23 | +import io.branch.referral.BranchViewHandler; |
23 | 24 | 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; |
24 | 29 | import io.branch.referral.util.ShareSheetStyle;
|
25 | 30 |
|
26 | 31 | public class BranchSDK extends CordovaPlugin {
|
@@ -107,6 +112,13 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
|
107 | 112 | }
|
108 | 113 | cordova.getThreadPool().execute(r);
|
109 | 114 | 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; |
110 | 122 | } else if (action.equals("getFirstReferringParams")) {
|
111 | 123 | cordova.getThreadPool().execute(r);
|
112 | 124 | return true;
|
@@ -602,6 +614,91 @@ private void userCompletedAction(String action, JSONObject metaData, CallbackCon
|
602 | 614 |
|
603 | 615 | }
|
604 | 616 |
|
| 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 | + |
605 | 702 | /**
|
606 | 703 | * <p>Gets the credit history of the specified bucket and triggers a callback to handle the
|
607 | 704 | * response.</p>
|
@@ -645,6 +742,35 @@ public BranchUniversalObjectWrapper(BranchUniversalObject branchUniversalObj) {
|
645 | 742 | //----------- INNER CLASS LISTENERS ------------//
|
646 | 743 | //////////////////////////////////////////////////
|
647 | 744 |
|
| 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 | + |
648 | 774 | protected class SessionListener implements Branch.BranchReferralInitListener {
|
649 | 775 | private CallbackContext _callbackContext;
|
650 | 776 |
|
@@ -1104,6 +1230,8 @@ public void run() {
|
1104 | 1230 | } else if (this.args.length() == 1) {
|
1105 | 1231 | userCompletedAction(this.args.getString(0), this.callbackContext);
|
1106 | 1232 | }
|
| 1233 | + } else if (this.action.equals("sendCommerceEvent")) { |
| 1234 | + sendCommerceEvent(this.args.getJSONObject(0), this.args.getJSONObject(1), this.callbackContext); |
1107 | 1235 | } else if (this.action.equals("getFirstReferringParams")) {
|
1108 | 1236 | getFirstReferringParams(this.callbackContext);
|
1109 | 1237 | } else if (this.action.equals("getLatestReferringParams")) {
|
|
0 commit comments