Search in sources :

Example 1 with OSOutcomeEventParams

use of com.onesignal.outcomes.domain.OSOutcomeEventParams in project OneSignal-Android-SDK by OneSignal.

the class OutcomeEventV2UnitTests method testOutcomeMultipleFailsSavedOnDB.

@Test
public void testOutcomeMultipleFailsSavedOnDB() throws Exception {
    service.setSuccess(false);
    // Init session should set UNATTRIBUTED influence
    sessionManager.initSessionFromCache();
    controller.sendOutcomeEvent(OUTCOME_NAME);
    // Set last influence ids
    sessionManager.onInAppMessageReceived(IAM_ID);
    sessionManager.onNotificationReceived(NOTIFICATION_ID);
    // Set DIRECT notification id influence and INDIRECT iam id influence because of upgrade
    sessionManager.onDirectInfluenceFromNotificationOpen(NOTIFICATION_ID);
    controller.sendOutcomeEvent(OUTCOME_NAME + "1");
    // Restart session by app open should set INDIRECT influence for both channels
    sessionManager.restartSessionIfNeeded(OneSignal.AppEntryAction.APP_OPEN);
    // Set DIRECT for iam id influence
    sessionManager.onDirectInfluenceFromIAMClick(IAM_ID);
    controller.sendOutcomeEvent(OUTCOME_NAME + "2");
    threadAndTaskWait();
    handler.setOutcomes(repository.getSavedOutcomeEvents());
    threadAndTaskWait();
    assertEquals(3, outcomeEvents.size());
    // DISABLED influence
    trackerFactory.saveInfluenceParams(disabledInfluenceParams);
    controller.sendOutcomeEvent(OUTCOME_NAME + "3");
    controller.sendOutcomeEvent(OUTCOME_NAME + "4");
    threadAndTaskWait();
    handler.setOutcomes(repository.getSavedOutcomeEvents());
    threadAndTaskWait();
    // Disables outcomes should not be sent
    assertEquals(3, outcomeEvents.size());
    for (OSOutcomeEventParams outcomeEvent : outcomeEvents) {
        // UNATTRIBUTED Case
        if (outcomeEvent.getOutcomeId().equals(OUTCOME_NAME)) {
            assertEquals("OSOutcomeEventParams{outcomeId='testing', outcomeSource=OSOutcomeSource{directBody=null, indirectBody=null}, weight=0.0, timestamp=" + outcomeEvent.getTimestamp() + "}", outcomeEvent.toString());
        } else if (outcomeEvent.getOutcomeId().equals(OUTCOME_NAME + "1")) {
            // DIRECT By Notification INDIRECT by iam
            assertEquals("OSOutcomeEventParams{outcomeId='testing1', outcomeSource=OSOutcomeSource{directBody=OSOutcomeSourceBody{notificationIds=[\"notification_id\"], inAppMessagesIds=[]}, " + "indirectBody=OSOutcomeSourceBody{notificationIds=[], inAppMessagesIds=[\"iam_id\"]}}, weight=0.0, timestamp=" + outcomeEvent.getTimestamp() + "}", outcomeEvent.toString());
        } else if (outcomeEvent.getOutcomeId().equals(OUTCOME_NAME + "2")) {
            // INDIRECT By Notification DIRECT by iam
            assertEquals("OSOutcomeEventParams{outcomeId='testing2', outcomeSource=OSOutcomeSource{directBody=OSOutcomeSourceBody{notificationIds=[], inAppMessagesIds=[\"iam_id\"]}, " + "indirectBody=OSOutcomeSourceBody{notificationIds=[\"notification_id\"], inAppMessagesIds=[]}}, weight=0.0, timestamp=" + outcomeEvent.getTimestamp() + "}", outcomeEvent.toString());
        }
    // DISABLED Case should not be save
    }
}
Also used : OSOutcomeEventParams(com.onesignal.outcomes.domain.OSOutcomeEventParams) Test(org.junit.Test)

Example 2 with OSOutcomeEventParams

use of com.onesignal.outcomes.domain.OSOutcomeEventParams in project OneSignal-Android-SDK by OneSignal.

the class OutcomeEventV2UnitTests method testOutcomeDirectFailSavedOnDB.

@Test
public void testOutcomeDirectFailSavedOnDB() throws Exception {
    service.setSuccess(false);
    sessionManager.initSessionFromCache();
    sessionManager.onNotificationReceived(NOTIFICATION_ID);
    sessionManager.onDirectInfluenceFromNotificationOpen(NOTIFICATION_ID);
    sessionManager.onInAppMessageReceived(IAM_ID);
    sessionManager.onDirectInfluenceFromIAMClick(IAM_ID);
    controller.sendOutcomeEvent(OUTCOME_NAME);
    threadAndTaskWait();
    handler.setOutcomes(repository.getSavedOutcomeEvents());
    threadAndTaskWait();
    assertTrue(outcomeEvents.size() > 0);
    OSOutcomeEventParams params = outcomeEvents.get(0);
    assertEquals(OUTCOME_NAME, params.getOutcomeId());
    assertEquals(new Float(0), (Float) params.getWeight());
    assertTrue(params.getTimestamp() > 0);
    assertNotNull(params.getOutcomeSource());
    // Direct body
    assertEquals(1, params.getOutcomeSource().getDirectBody().getInAppMessagesIds().length());
    assertEquals(IAM_ID, params.getOutcomeSource().getDirectBody().getInAppMessagesIds().get(0));
    assertEquals(1, params.getOutcomeSource().getDirectBody().getNotificationIds().length());
    assertEquals(NOTIFICATION_ID, params.getOutcomeSource().getDirectBody().getNotificationIds().get(0));
    // Indirect body
    assertNull(params.getOutcomeSource().getIndirectBody());
}
Also used : OSOutcomeEventParams(com.onesignal.outcomes.domain.OSOutcomeEventParams) Test(org.junit.Test)

Example 3 with OSOutcomeEventParams

use of com.onesignal.outcomes.domain.OSOutcomeEventParams in project OneSignal-Android-SDK by OneSignal.

the class OutcomeEventUnitTests method testOutcomeMultipleFailsSavedOnDB.

@Test
public void testOutcomeMultipleFailsSavedOnDB() throws Exception {
    service.setSuccess(false);
    // Restart session by app open should set UNATTRIBUTED influence
    sessionManager.restartSessionIfNeeded(OneSignal.AppEntryAction.APP_OPEN);
    controller.sendOutcomeEvent(OUTCOME_NAME);
    sessionManager.onNotificationReceived(NOTIFICATION_ID);
    // Set DIRECT notification id influence
    sessionManager.onDirectInfluenceFromNotificationOpen(NOTIFICATION_ID);
    controller.sendOutcomeEvent(OUTCOME_NAME + "1");
    // Restart session by app open should set INDIRECT influence
    sessionManager.restartSessionIfNeeded(OneSignal.AppEntryAction.APP_OPEN);
    controller.sendOutcomeEvent(OUTCOME_NAME + "2");
    threadAndTaskWait();
    new Thread(new Runnable() {

        @Override
        public void run() {
            handler.setOutcomes(repository.getSavedOutcomeEvents());
        }
    }, "OS_GET_SAVED_OUTCOMES_FAIL").start();
    threadAndTaskWait();
    assertEquals(3, outcomeEvents.size());
    // DISABLED influence
    trackerFactory.saveInfluenceParams(disabledInfluenceParams);
    controller.sendOutcomeEvent(OUTCOME_NAME + "3");
    controller.sendOutcomeEvent(OUTCOME_NAME + "4");
    threadAndTaskWait();
    new Thread(new Runnable() {

        @Override
        public void run() {
            handler.setOutcomes(repository.getSavedOutcomeEvents());
        }
    }, "OS_GET_SAVED_OUTCOMES_FAILS").start();
    threadAndTaskWait();
    assertEquals(3, outcomeEvents.size());
    for (OSOutcomeEventParams outcomeEvent : outcomeEvents) {
        // UNATTRIBUTED Case
        if (outcomeEvent.isUnattributed()) {
            assertEquals("OSOutcomeEventParams{outcomeId='testing', outcomeSource=OSOutcomeSource{directBody=null, indirectBody=null}, weight=0.0, timestamp=" + outcomeEvent.getTimestamp() + "}", outcomeEvent.toString());
        } else if (outcomeEvent.getOutcomeSource().getIndirectBody() != null) {
            // INDIRECT Case
            assertEquals("OSOutcomeEventParams{outcomeId='testing2', outcomeSource=OSOutcomeSource{directBody=null, indirectBody=OSOutcomeSourceBody{notificationIds=[\"testing\"], inAppMessagesIds=[]}}, weight=0.0, timestamp=" + outcomeEvent.getTimestamp() + "}", outcomeEvent.toString());
        } else {
            // DIRECT Case
            assertEquals("OSOutcomeEventParams{outcomeId='testing1', outcomeSource=OSOutcomeSource{directBody=OSOutcomeSourceBody{notificationIds=[\"testing\"], inAppMessagesIds=[]}, indirectBody=null}, weight=0.0, timestamp=" + outcomeEvent.getTimestamp() + "}", outcomeEvent.toString());
        }
    // DISABLED Case should not be save
    }
}
Also used : OSOutcomeEventParams(com.onesignal.outcomes.domain.OSOutcomeEventParams) Test(org.junit.Test)

Example 4 with OSOutcomeEventParams

use of com.onesignal.outcomes.domain.OSOutcomeEventParams in project OneSignal-Android-SDK by OneSignal.

the class OSOutcomeEventsController method sendSavedOutcomes.

/**
 * Any outcomes cached in local DB will be reattempted to be sent again
 * Cached outcomes come from the failure callback of the network request
 */
void sendSavedOutcomes() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND);
            List<OSOutcomeEventParams> outcomeEvents = outcomeEventsFactory.getRepository().getSavedOutcomeEvents();
            for (OSOutcomeEventParams event : outcomeEvents) {
                sendSavedOutcomeEvent(event);
            }
        }
    }, OS_SEND_SAVED_OUTCOMES).start();
}
Also used : OSOutcomeEventParams(com.onesignal.outcomes.domain.OSOutcomeEventParams)

Example 5 with OSOutcomeEventParams

use of com.onesignal.outcomes.domain.OSOutcomeEventParams in project OneSignal-Android-SDK by OneSignal.

the class OSOutcomeEventsController method sendAndCreateOutcomeEvent.

private void sendAndCreateOutcomeEvent(@NonNull final String name, @NonNull final float weight, @NonNull List<OSInfluence> influences, @Nullable final OneSignal.OutcomeCallback callback) {
    final long timestampSeconds = OneSignal.getTime().getCurrentTimeMillis() / 1000;
    final int deviceType = new OSUtils().getDeviceType();
    final String appId = OneSignal.appId;
    OSOutcomeSourceBody directSourceBody = null;
    OSOutcomeSourceBody indirectSourceBody = null;
    boolean unattributed = false;
    for (OSInfluence influence : influences) {
        switch(influence.getInfluenceType()) {
            case DIRECT:
                directSourceBody = setSourceChannelIds(influence, directSourceBody == null ? new OSOutcomeSourceBody() : directSourceBody);
                break;
            case INDIRECT:
                indirectSourceBody = setSourceChannelIds(influence, indirectSourceBody == null ? new OSOutcomeSourceBody() : indirectSourceBody);
                break;
            case UNATTRIBUTED:
                unattributed = true;
                break;
            case DISABLED:
                OneSignal.Log(OneSignal.LOG_LEVEL.VERBOSE, "Outcomes disabled for channel: " + influence.getInfluenceChannel());
                if (callback != null)
                    callback.onSuccess(null);
                // finish method
                return;
        }
    }
    if (directSourceBody == null && indirectSourceBody == null && !unattributed) {
        // Disabled for all channels
        OneSignal.Log(OneSignal.LOG_LEVEL.VERBOSE, "Outcomes disabled for all channels");
        if (callback != null)
            callback.onSuccess(null);
        return;
    }
    OSOutcomeSource source = new OSOutcomeSource(directSourceBody, indirectSourceBody);
    final OSOutcomeEventParams eventParams = new OSOutcomeEventParams(name, source, weight, 0);
    OneSignalApiResponseHandler responseHandler = new OneSignalApiResponseHandler() {

        @Override
        public void onSuccess(String response) {
            saveUniqueOutcome(eventParams);
            // The only case where an actual success has occurred and the OutcomeEvent should be sent back
            if (callback != null)
                callback.onSuccess(OSOutcomeEvent.fromOutcomeEventParamsV2toOutcomeEventV1(eventParams));
        }

        @Override
        public void onFailure(int statusCode, String response, Throwable throwable) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    Thread.currentThread().setPriority(Process.THREAD_PRIORITY_BACKGROUND);
                    // Only if we need to save and retry the outcome, then we will save the timestamp for future sending
                    eventParams.setTimestamp(timestampSeconds);
                    outcomeEventsFactory.getRepository().saveOutcomeEvent(eventParams);
                }
            }, OS_SAVE_OUTCOMES).start();
            OneSignal.onesignalLog(OneSignal.LOG_LEVEL.WARN, "Sending outcome with name: " + name + " failed with status code: " + statusCode + " and response: " + response + "\nOutcome event was cached and will be reattempted on app cold start");
            // Return null within the callback to determine not a failure, but not a success in terms of the request made
            if (callback != null)
                callback.onSuccess(null);
        }
    };
    outcomeEventsFactory.getRepository().requestMeasureOutcomeEvent(appId, deviceType, eventParams, responseHandler);
}
Also used : OSOutcomeEventParams(com.onesignal.outcomes.domain.OSOutcomeEventParams) OSOutcomeSourceBody(com.onesignal.outcomes.domain.OSOutcomeSourceBody) OSOutcomeSource(com.onesignal.outcomes.domain.OSOutcomeSource) OSInfluence(com.onesignal.influence.domain.OSInfluence)

Aggregations

OSOutcomeEventParams (com.onesignal.outcomes.domain.OSOutcomeEventParams)6 Test (org.junit.Test)4 OSInfluence (com.onesignal.influence.domain.OSInfluence)1 OSOutcomeSource (com.onesignal.outcomes.domain.OSOutcomeSource)1 OSOutcomeSourceBody (com.onesignal.outcomes.domain.OSOutcomeSourceBody)1