Search in sources :

Example 1 with OSOutcomeSourceBody

use of com.onesignal.outcomes.domain.OSOutcomeSourceBody 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

OSInfluence (com.onesignal.influence.domain.OSInfluence)1 OSOutcomeEventParams (com.onesignal.outcomes.domain.OSOutcomeEventParams)1 OSOutcomeSource (com.onesignal.outcomes.domain.OSOutcomeSource)1 OSOutcomeSourceBody (com.onesignal.outcomes.domain.OSOutcomeSourceBody)1