@Test public void unpackImpressionCommerceEvent() throws Exception { JSONObject object = new JSONObject(sampleCommerceEvent8); CommerceEvent commerceEvent = toCommerceEvent(object); assertEquals(commerceEvent.getEventName(), "eCommerce - Impression"); assertEquals(commerceEvent.getImpressions().size(), 1); Impression impression = commerceEvent.getImpressions().get(0); assertEquals(impression.getListName(), "impression-name"); assertEquals(impression.getProducts().size(), 1); Product product5 = impression.getProducts().get(0); assertEquals(product5.getName(), "iPhone"); assertEquals(product5.getSku(), "12345"); assertEquals(product5.getUnitPrice(), 400, .01); assertEquals(product5.getQuantity(), 1, .01); }
protected CommerceEvent toCommerceEvent(JSONObject jsonObject) { CommerceEvent.Builder builder = null; JSONObject productActionObj = jsonObject.optJSONObject(PRODUCT_ACTION); if (productActionObj != null) { String productActionEnum = productActionObj.optString(PRODUCT_ACTION_TYPE, null); String productAction = getProductAction(productActionEnum); JSONArray productArray = productActionObj.optJSONArray(PRODUCT_LIST); if (productAction != null && productArray != null) { for (int i = 0; i < productArray.length(); i++) { Product product = toProduct(productArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(productAction, product); } else { builder.addProduct(product); } } if (builder == null) { builder = new CommerceEvent.Builder(productAction, (Product)null); } TransactionAttributes transactionAttributes = getTransactionAttributes(productActionObj); if (transactionAttributes != null) { builder.transactionAttributes(transactionAttributes); } builder.checkoutStep(productActionObj.optInt(CHECKOUT_STEP)); builder.checkoutOptions(productActionObj.optString(CHECKOUT_OPTIONS)); } } JSONObject promotionActionObj = jsonObject.optJSONObject(PROMOTION_ACTION); if (promotionActionObj != null) { String promotionActionEnum = promotionActionObj.optString(PROMOTION_ACTION_TYPE); String promotionAction = getPromotionAction(promotionActionEnum); JSONArray promotionArray = promotionActionObj.optJSONArray(PROMOTION_LIST); if (promotionAction != null && promotionArray != null) { for (int i = 0; i < promotionArray.length(); i++) { Promotion promotion = toPromotion(promotionArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, promotion); } else { builder.addPromotion(promotion); } } if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, (Promotion)null); } } } JSONArray impressionList = jsonObject.optJSONArray(PRODUCT_IMPRESSIONS); if (impressionList != null) { for (int i = 0; i < impressionList.length(); i++) { Impression impression = toImpression(impressionList.optJSONObject(i)); if (impression != null) { if (builder == null) { builder = new CommerceEvent.Builder(impression); } else { builder.addImpression(impression); } } } } if (builder == null) { return null; } Map<String, String> customAttributes = convertToMap(jsonObject.optJSONObject(JS_KEY_EVENT_ATTRIBUTES)); if (customAttributes != null) { builder.customAttributes(customAttributes); } String currencyCode = jsonObject.optString(CURRENCY_CODE, null); if (currencyCode != null && !"null".equals(currencyCode)) { builder.currency(currencyCode); } builder.internalEventName(jsonObject.optString(EVENT_NAME, null)); return builder.build(); }
MParticleJSInterface { protected CommerceEvent toCommerceEvent(JSONObject jsonObject) { CommerceEvent.Builder builder = null; JSONObject productActionObj = jsonObject.optJSONObject(PRODUCT_ACTION); if (productActionObj != null) { String productActionEnum = productActionObj.optString(PRODUCT_ACTION_TYPE, null); String productAction = getProductAction(productActionEnum); JSONArray productArray = productActionObj.optJSONArray(PRODUCT_LIST); if (productAction != null && productArray != null) { for (int i = 0; i < productArray.length(); i++) { Product product = toProduct(productArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(productAction, product); } else { builder.addProduct(product); } } if (builder == null) { builder = new CommerceEvent.Builder(productAction, (Product)null); } TransactionAttributes transactionAttributes = getTransactionAttributes(productActionObj); if (transactionAttributes != null) { builder.transactionAttributes(transactionAttributes); } builder.checkoutStep(productActionObj.optInt(CHECKOUT_STEP)); builder.checkoutOptions(productActionObj.optString(CHECKOUT_OPTIONS)); } } JSONObject promotionActionObj = jsonObject.optJSONObject(PROMOTION_ACTION); if (promotionActionObj != null) { String promotionActionEnum = promotionActionObj.optString(PROMOTION_ACTION_TYPE); String promotionAction = getPromotionAction(promotionActionEnum); JSONArray promotionArray = promotionActionObj.optJSONArray(PROMOTION_LIST); if (promotionAction != null && promotionArray != null) { for (int i = 0; i < promotionArray.length(); i++) { Promotion promotion = toPromotion(promotionArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, promotion); } else { builder.addPromotion(promotion); } } if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, (Promotion)null); } } } JSONArray impressionList = jsonObject.optJSONArray(PRODUCT_IMPRESSIONS); if (impressionList != null) { for (int i = 0; i < impressionList.length(); i++) { Impression impression = toImpression(impressionList.optJSONObject(i)); if (impression != null) { if (builder == null) { builder = new CommerceEvent.Builder(impression); } else { builder.addImpression(impression); } } } } if (builder == null) { return null; } Map<String, String> customAttributes = convertToMap(jsonObject.optJSONObject(JS_KEY_EVENT_ATTRIBUTES)); if (customAttributes != null) { builder.customAttributes(customAttributes); } String currencyCode = jsonObject.optString(CURRENCY_CODE, null); if (currencyCode != null && !"null".equals(currencyCode)) { builder.currency(currencyCode); } builder.internalEventName(jsonObject.optString(EVENT_NAME, null)); return builder.build(); } }
MParticleJSInterface { protected CommerceEvent toCommerceEvent(JSONObject jsonObject) { CommerceEvent.Builder builder = null; JSONObject productActionObj = jsonObject.optJSONObject(PRODUCT_ACTION); if (productActionObj != null) { String productActionEnum = productActionObj.optString(PRODUCT_ACTION_TYPE, null); String productAction = getProductAction(productActionEnum); JSONArray productArray = productActionObj.optJSONArray(PRODUCT_LIST); if (productAction != null && productArray != null) { for (int i = 0; i < productArray.length(); i++) { Product product = toProduct(productArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(productAction, product); } else { builder.addProduct(product); } } if (builder == null) { builder = new CommerceEvent.Builder(productAction, (Product)null); } TransactionAttributes transactionAttributes = getTransactionAttributes(productActionObj); if (transactionAttributes != null) { builder.transactionAttributes(transactionAttributes); } builder.checkoutStep(productActionObj.optInt(CHECKOUT_STEP)); builder.checkoutOptions(productActionObj.optString(CHECKOUT_OPTIONS)); } } JSONObject promotionActionObj = jsonObject.optJSONObject(PROMOTION_ACTION); if (promotionActionObj != null) { String promotionActionEnum = promotionActionObj.optString(PROMOTION_ACTION_TYPE); String promotionAction = getPromotionAction(promotionActionEnum); JSONArray promotionArray = promotionActionObj.optJSONArray(PROMOTION_LIST); if (promotionAction != null && promotionArray != null) { for (int i = 0; i < promotionArray.length(); i++) { Promotion promotion = toPromotion(promotionArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, promotion); } else { builder.addPromotion(promotion); } } if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, (Promotion)null); } } } JSONArray impressionList = jsonObject.optJSONArray(PRODUCT_IMPRESSIONS); if (impressionList != null) { for (int i = 0; i < impressionList.length(); i++) { Impression impression = toImpression(impressionList.optJSONObject(i)); if (impression != null) { if (builder == null) { builder = new CommerceEvent.Builder(impression); } else { builder.addImpression(impression); } } } } if (builder == null) { return null; } Map<String, String> customAttributes = convertToMap(jsonObject.optJSONObject(JS_KEY_EVENT_ATTRIBUTES)); if (customAttributes != null) { builder.customAttributes(customAttributes); } String currencyCode = jsonObject.optString(CURRENCY_CODE, null); if (currencyCode != null && !"null".equals(currencyCode)) { builder.currency(currencyCode); } builder.internalEventName(jsonObject.optString(EVENT_NAME, null)); return builder.build(); } MParticleJSInterface(); }
MParticleJSInterface { protected CommerceEvent toCommerceEvent(JSONObject jsonObject) { CommerceEvent.Builder builder = null; JSONObject productActionObj = jsonObject.optJSONObject(PRODUCT_ACTION); if (productActionObj != null) { String productActionEnum = productActionObj.optString(PRODUCT_ACTION_TYPE, null); String productAction = getProductAction(productActionEnum); JSONArray productArray = productActionObj.optJSONArray(PRODUCT_LIST); if (productAction != null && productArray != null) { for (int i = 0; i < productArray.length(); i++) { Product product = toProduct(productArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(productAction, product); } else { builder.addProduct(product); } } if (builder == null) { builder = new CommerceEvent.Builder(productAction, (Product)null); } TransactionAttributes transactionAttributes = getTransactionAttributes(productActionObj); if (transactionAttributes != null) { builder.transactionAttributes(transactionAttributes); } builder.checkoutStep(productActionObj.optInt(CHECKOUT_STEP)); builder.checkoutOptions(productActionObj.optString(CHECKOUT_OPTIONS)); } } JSONObject promotionActionObj = jsonObject.optJSONObject(PROMOTION_ACTION); if (promotionActionObj != null) { String promotionActionEnum = promotionActionObj.optString(PROMOTION_ACTION_TYPE); String promotionAction = getPromotionAction(promotionActionEnum); JSONArray promotionArray = promotionActionObj.optJSONArray(PROMOTION_LIST); if (promotionAction != null && promotionArray != null) { for (int i = 0; i < promotionArray.length(); i++) { Promotion promotion = toPromotion(promotionArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, promotion); } else { builder.addPromotion(promotion); } } if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, (Promotion)null); } } } JSONArray impressionList = jsonObject.optJSONArray(PRODUCT_IMPRESSIONS); if (impressionList != null) { for (int i = 0; i < impressionList.length(); i++) { Impression impression = toImpression(impressionList.optJSONObject(i)); if (impression != null) { if (builder == null) { builder = new CommerceEvent.Builder(impression); } else { builder.addImpression(impression); } } } } if (builder == null) { return null; } Map<String, String> customAttributes = convertToMap(jsonObject.optJSONObject(JS_KEY_EVENT_ATTRIBUTES)); if (customAttributes != null) { builder.customAttributes(customAttributes); } String currencyCode = jsonObject.optString(CURRENCY_CODE, null); if (currencyCode != null && !"null".equals(currencyCode)) { builder.currency(currencyCode); } builder.internalEventName(jsonObject.optString(EVENT_NAME, null)); return builder.build(); } MParticleJSInterface(); @JavascriptInterface String getCurrentMpId(); @JavascriptInterface void login(); @JavascriptInterface void login(String json); @JavascriptInterface void logout(); @JavascriptInterface void logout(String json); @JavascriptInterface void modify(String json); @JavascriptInterface void logEvent(String json); @JavascriptInterface void setUserTag(String json); @JavascriptInterface void removeUserTag(String json); @JavascriptInterface void setUserAttribute(String json); @JavascriptInterface void removeUserAttribute(String json); @JavascriptInterface void setSessionAttribute(String json); @JavascriptInterface void setUserIdentity(String json); @JavascriptInterface void removeUserIdentity(String json); @JavascriptInterface void addToCart(String json); @JavascriptInterface void removeFromCart(String json); @JavascriptInterface void clearCart(); @JavascriptInterface void setUserAttributeList(String json); @JavascriptInterface void removeAllUserAttributes(); @JavascriptInterface String getUserAttributesLists(); @JavascriptInterface String getAllUserAttributes(); static void registerWebView(WebView webView, String workspaceToken); }
MParticleJSInterface { protected CommerceEvent toCommerceEvent(JSONObject jsonObject) { CommerceEvent.Builder builder = null; JSONObject productActionObj = jsonObject.optJSONObject(PRODUCT_ACTION); if (productActionObj != null) { String productActionEnum = productActionObj.optString(PRODUCT_ACTION_TYPE, null); String productAction = getProductAction(productActionEnum); JSONArray productArray = productActionObj.optJSONArray(PRODUCT_LIST); if (productAction != null && productArray != null) { for (int i = 0; i < productArray.length(); i++) { Product product = toProduct(productArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(productAction, product); } else { builder.addProduct(product); } } if (builder == null) { builder = new CommerceEvent.Builder(productAction, (Product)null); } TransactionAttributes transactionAttributes = getTransactionAttributes(productActionObj); if (transactionAttributes != null) { builder.transactionAttributes(transactionAttributes); } builder.checkoutStep(productActionObj.optInt(CHECKOUT_STEP)); builder.checkoutOptions(productActionObj.optString(CHECKOUT_OPTIONS)); } } JSONObject promotionActionObj = jsonObject.optJSONObject(PROMOTION_ACTION); if (promotionActionObj != null) { String promotionActionEnum = promotionActionObj.optString(PROMOTION_ACTION_TYPE); String promotionAction = getPromotionAction(promotionActionEnum); JSONArray promotionArray = promotionActionObj.optJSONArray(PROMOTION_LIST); if (promotionAction != null && promotionArray != null) { for (int i = 0; i < promotionArray.length(); i++) { Promotion promotion = toPromotion(promotionArray.optJSONObject(i)); if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, promotion); } else { builder.addPromotion(promotion); } } if (builder == null) { builder = new CommerceEvent.Builder(promotionAction, (Promotion)null); } } } JSONArray impressionList = jsonObject.optJSONArray(PRODUCT_IMPRESSIONS); if (impressionList != null) { for (int i = 0; i < impressionList.length(); i++) { Impression impression = toImpression(impressionList.optJSONObject(i)); if (impression != null) { if (builder == null) { builder = new CommerceEvent.Builder(impression); } else { builder.addImpression(impression); } } } } if (builder == null) { return null; } Map<String, String> customAttributes = convertToMap(jsonObject.optJSONObject(JS_KEY_EVENT_ATTRIBUTES)); if (customAttributes != null) { builder.customAttributes(customAttributes); } String currencyCode = jsonObject.optString(CURRENCY_CODE, null); if (currencyCode != null && !"null".equals(currencyCode)) { builder.currency(currencyCode); } builder.internalEventName(jsonObject.optString(EVENT_NAME, null)); return builder.build(); } MParticleJSInterface(); @JavascriptInterface String getCurrentMpId(); @JavascriptInterface void login(); @JavascriptInterface void login(String json); @JavascriptInterface void logout(); @JavascriptInterface void logout(String json); @JavascriptInterface void modify(String json); @JavascriptInterface void logEvent(String json); @JavascriptInterface void setUserTag(String json); @JavascriptInterface void removeUserTag(String json); @JavascriptInterface void setUserAttribute(String json); @JavascriptInterface void removeUserAttribute(String json); @JavascriptInterface void setSessionAttribute(String json); @JavascriptInterface void setUserIdentity(String json); @JavascriptInterface void removeUserIdentity(String json); @JavascriptInterface void addToCart(String json); @JavascriptInterface void removeFromCart(String json); @JavascriptInterface void clearCart(); @JavascriptInterface void setUserAttributeList(String json); @JavascriptInterface void removeAllUserAttributes(); @JavascriptInterface String getUserAttributesLists(); @JavascriptInterface String getAllUserAttributes(); static void registerWebView(WebView webView, String workspaceToken); static final String INTERFACE_BASE_NAME; }