AddCustomEventAction extends Action { @NonNull @Override public ActionResult perform(@NonNull ActionArguments arguments) { JsonMap customEventMap = arguments.getValue().toJsonValue().optMap(); String eventName = customEventMap.opt(CustomEvent.EVENT_NAME).getString(); Checks.checkNotNull(eventName, "Missing event name"); String eventStringValue = customEventMap.opt(CustomEvent.EVENT_VALUE).getString(); double eventDoubleValue = customEventMap.opt(CustomEvent.EVENT_VALUE).getDouble(0); String transactionId = customEventMap.opt(CustomEvent.TRANSACTION_ID).getString(); String interactionType = customEventMap.opt(CustomEvent.INTERACTION_TYPE).getString(); String interactionId = customEventMap.opt(CustomEvent.INTERACTION_ID).getString(); JsonMap properties = customEventMap.opt(CustomEvent.PROPERTIES).getMap(); CustomEvent.Builder eventBuilder = CustomEvent.newBuilder(eventName) .setTransactionId(transactionId) .setAttribution((PushMessage) arguments.getMetadata().getParcelable(ActionArguments.PUSH_MESSAGE_METADATA)) .setInteraction(interactionType, interactionId); if (eventStringValue != null) { eventBuilder.setEventValue(eventStringValue); } else { eventBuilder.setEventValue(eventDoubleValue); } if (interactionId == null && interactionType == null) { String messageId = arguments.getMetadata().getString(ActionArguments.RICH_PUSH_ID_METADATA); if (messageId != null) { eventBuilder.setMessageCenterInteraction(messageId); } } if (properties != null) { eventBuilder.setProperties(properties); } CustomEvent event = eventBuilder.build(); event.track(); if (event.isValid()) { return ActionResult.newEmptyResult(); } else { return ActionResult.newErrorResult(new IllegalArgumentException("Unable to add custom event. Event is invalid.")); } }  @NonNull @Override ActionResult perform(@NonNull ActionArguments arguments); @Override boolean acceptsArguments(@NonNull ActionArguments arguments); @NonNull
    static final String DEFAULT_REGISTRY_NAME; }
@Test public void testPerform() throws JSONException { Map map = new HashMap(); map.put(CustomEvent.TRANSACTION_ID, "transaction id"); map.put(CustomEvent.EVENT_VALUE, "123.45"); map.put(CustomEvent.INTERACTION_TYPE, "interaction type"); map.put(CustomEvent.INTERACTION_ID, "interaction id"); map.put(CustomEvent.EVENT_NAME, "event name"); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, map); ActionResult result = action.perform(args); assertEquals("Action should've completed", ActionResult.STATUS_COMPLETED, result.getStatus()); ArgumentCaptor<CustomEvent> argumentCaptor = ArgumentCaptor.forClass(CustomEvent.class); verify(analytics).addEvent(argumentCaptor.capture()); CustomEvent event = argumentCaptor.getValue(); EventTestUtils.validateEventValue(event, CustomEvent.TRANSACTION_ID, "transaction id"); EventTestUtils.validateEventValue(event, CustomEvent.EVENT_VALUE, 123450000L); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_TYPE, "interaction type"); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_ID, "interaction id"); EventTestUtils.validateEventValue(event, CustomEvent.EVENT_NAME, "event name"); }
@Test public void testPerformMCRAP() throws JSONException { Map map = new HashMap(); map.put(CustomEvent.EVENT_NAME, "event name"); Bundle metadata = new Bundle(); metadata.putString(ActionArguments.RICH_PUSH_ID_METADATA, "message id"); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, map, metadata); ActionResult result = action.perform(args); assertEquals("Action should've completed", ActionResult.STATUS_COMPLETED, result.getStatus()); ArgumentCaptor<CustomEvent> argumentCaptor = ArgumentCaptor.forClass(CustomEvent.class); verify(analytics).addEvent(argumentCaptor.capture()); CustomEvent event = argumentCaptor.getValue(); EventTestUtils.validateEventValue(event, CustomEvent.EVENT_NAME, "event name"); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_TYPE, "ua_mcrap"); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_ID, "message id"); }
@Test public void testPerformMCRAPInteractionSet() throws JSONException { Map map = new HashMap(); map.put(CustomEvent.EVENT_NAME, "event name"); map.put(CustomEvent.INTERACTION_TYPE, "interaction type"); Bundle metadata = new Bundle(); metadata.putString(ActionArguments.RICH_PUSH_ID_METADATA, "message id"); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, map, metadata); ActionResult result = action.perform(args); assertEquals("Action should've completed", ActionResult.STATUS_COMPLETED, result.getStatus()); ArgumentCaptor<CustomEvent> argumentCaptor = ArgumentCaptor.forClass(CustomEvent.class); verify(analytics).addEvent(argumentCaptor.capture()); CustomEvent event = argumentCaptor.getValue(); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_TYPE, "interaction type"); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_ID, null); }
@Test public void testPerformPushMessageMetadata() throws JSONException { Map map = new HashMap(); map.put(CustomEvent.TRANSACTION_ID, "transaction id"); map.put(CustomEvent.EVENT_VALUE, "123.45"); map.put(CustomEvent.INTERACTION_TYPE, "interaction type"); map.put(CustomEvent.INTERACTION_ID, "interaction id"); map.put(CustomEvent.EVENT_NAME, "event name"); Bundle pushBundle = new Bundle(); pushBundle.putString(PushMessage.EXTRA_SEND_ID, "send id"); Bundle metadata = new Bundle(); metadata.putParcelable(ActionArguments.PUSH_MESSAGE_METADATA, new PushMessage(pushBundle)); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, map, metadata); ActionResult result = action.perform(args); assertEquals("Action should've completed", ActionResult.STATUS_COMPLETED, result.getStatus()); ArgumentCaptor<CustomEvent> argumentCaptor = ArgumentCaptor.forClass(CustomEvent.class); verify(analytics).addEvent(argumentCaptor.capture()); CustomEvent event = argumentCaptor.getValue(); EventTestUtils.validateEventValue(event, CustomEvent.TRANSACTION_ID, "transaction id"); EventTestUtils.validateEventValue(event, CustomEvent.EVENT_VALUE, 123450000L); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_TYPE, "interaction type"); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_ID, "interaction id"); EventTestUtils.validateEventValue(event, CustomEvent.EVENT_NAME, "event name"); EventTestUtils.validateEventValue(event, CustomEvent.CONVERSION_SEND_ID, "send id"); }
@Test public void testPerformWithProperties() throws JSONException { Map<String, Object> map = new HashMap<>(); map.put(CustomEvent.TRANSACTION_ID, "transaction id"); map.put(CustomEvent.EVENT_VALUE, "123.45"); map.put(CustomEvent.INTERACTION_TYPE, "interaction type"); map.put(CustomEvent.INTERACTION_ID, "interaction id"); map.put(CustomEvent.EVENT_NAME, "event name"); Map<String, Object> properties = new HashMap<>(); properties.put("boolean", true); properties.put("double", 124.49); properties.put("string", "some string value"); properties.put("int", Integer.MIN_VALUE); properties.put("long", Long.MAX_VALUE); properties.put("array", Arrays.asList("string value", true, 124)); map.put(CustomEvent.PROPERTIES, properties); ActionArguments args = ActionTestUtils.createArgs(Action.SITUATION_MANUAL_INVOCATION, map); ActionResult result = action.perform(args); assertEquals("Action should've completed", ActionResult.STATUS_COMPLETED, result.getStatus()); ArgumentCaptor<CustomEvent> argumentCaptor = ArgumentCaptor.forClass(CustomEvent.class); verify(analytics).addEvent(argumentCaptor.capture()); CustomEvent event = argumentCaptor.getValue(); EventTestUtils.validateEventValue(event, CustomEvent.TRANSACTION_ID, "transaction id"); EventTestUtils.validateEventValue(event, CustomEvent.EVENT_VALUE, 123450000L); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_TYPE, "interaction type"); EventTestUtils.validateEventValue(event, CustomEvent.INTERACTION_ID, "interaction id"); EventTestUtils.validateEventValue(event, CustomEvent.EVENT_NAME, "event name"); EventTestUtils.validateNestedEventValue(event, "properties", "boolean", "true"); EventTestUtils.validateNestedEventValue(event, "properties", "double", "124.49"); EventTestUtils.validateNestedEventValue(event, "properties", "string", "some string value"); EventTestUtils.validateNestedEventValue(event, "properties", "int", "-2147483648"); EventTestUtils.validateNestedEventValue(event, "properties", "long", "9223372036854775807"); JsonList array = EventTestUtils.getEventData(event).get("properties").getMap().get("array").getList(); assertEquals(3, array.size()); assertEquals("string value", array.get(0).getString()); assertTrue(array.get(1).getBoolean(false)); assertEquals(124.0, array.get(2).getDouble(0)); }