Search in sources :

Example 26 with OSInfluence

use of com.onesignal.influence.domain.OSInfluence in project OneSignal-Android-SDK by OneSignal.

the class SessionManagerUnitTests method testDirectIAMInitInfluence.

@Test
public void testDirectIAMInitInfluence() throws JSONException {
    trackerFactory.saveInfluenceParams(new OneSignalPackagePrivateHelper.RemoteOutcomeParams());
    sessionManager.initSessionFromCache();
    OSChannelTracker iamTracker = trackerFactory.getIAMChannelTracker();
    assertEquals(0, iamTracker.getLastReceivedIds().length());
    sessionManager.onInAppMessageReceived(IAM_ID);
    sessionManager.onDirectInfluenceFromIAMClick(IAM_ID);
    iamTracker = trackerFactory.getIAMChannelTracker();
    OSInfluence influence = iamTracker.getCurrentSessionInfluence();
    assertEquals(OSInfluenceType.DIRECT, iamTracker.getInfluenceType());
    assertEquals(IAM_ID, iamTracker.getLastReceivedIds().get(0));
    assertEquals(OSInfluenceChannel.IAM, influence.getInfluenceChannel());
    assertEquals(OSInfluenceType.DIRECT, influence.getInfluenceType());
    assertEquals(1, influence.getIds().length());
    assertEquals(IAM_ID, influence.getIds().get(0));
}
Also used : OSChannelTracker(com.onesignal.influence.data.OSChannelTracker) OSInfluence(com.onesignal.influence.domain.OSInfluence) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) Test(org.junit.Test)

Example 27 with OSInfluence

use of com.onesignal.influence.domain.OSInfluence in project OneSignal-Android-SDK by OneSignal.

the class OSOutcomeEventsController method sendUniqueOutcomeEvent.

/**
 * An unique outcome is considered unattributed when all channels are unattributed
 * If one channel is attributed is enough reason to cache attribution
 */
private void sendUniqueOutcomeEvent(@NonNull final String name, @NonNull List<OSInfluence> sessionInfluences, @Nullable OneSignal.OutcomeCallback callback) {
    List<OSInfluence> influences = removeDisabledInfluences(sessionInfluences);
    if (influences.isEmpty()) {
        OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "Unique Outcome disabled for current session");
        return;
    }
    boolean attributed = false;
    for (OSInfluence influence : influences) {
        if (influence.getInfluenceType().isAttributed()) {
            // At least one channel attributed this outcome
            attributed = true;
            break;
        }
    }
    // Special handling for unique outcomes in the attributed and unattributed scenarios
    if (attributed) {
        // Make sure unique Ids exist before trying to make measure request
        final List<OSInfluence> uniqueInfluences = getUniqueIds(name, influences);
        if (uniqueInfluences == null) {
            OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "Measure endpoint will not send because unique outcome already sent for: " + "\nSessionInfluences: " + influences.toString() + "\nOutcome name: " + name);
            // 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);
            return;
        }
        sendAndCreateOutcomeEvent(name, 0, uniqueInfluences, callback);
    } else {
        // Make sure unique outcome has not been sent for current unattributed session
        if (unattributedUniqueOutcomeEventsSentOnSession.contains(name)) {
            OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "Measure endpoint will not send because unique outcome already sent for: " + "\nSession: " + OSInfluenceType.UNATTRIBUTED + "\nOutcome name: " + name);
            // 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);
            return;
        }
        unattributedUniqueOutcomeEventsSentOnSession.add(name);
        sendAndCreateOutcomeEvent(name, 0, influences, callback);
    }
}
Also used : OSInfluence(com.onesignal.influence.domain.OSInfluence)

Example 28 with OSInfluence

use of com.onesignal.influence.domain.OSInfluence in project OneSignal-Android-SDK by OneSignal.

the class OSOutcomeEventsController method removeDisabledInfluences.

private List<OSInfluence> removeDisabledInfluences(List<OSInfluence> influences) {
    List<OSInfluence> availableInfluences = new ArrayList<>(influences);
    for (OSInfluence influence : influences) {
        if (influence.getInfluenceType().isDisabled()) {
            OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "Outcomes disabled for channel: " + influence.getInfluenceChannel().toString());
            availableInfluences.remove(influence);
        }
    }
    return availableInfluences;
}
Also used : ArrayList(java.util.ArrayList) OSInfluence(com.onesignal.influence.domain.OSInfluence)

Example 29 with OSInfluence

use of com.onesignal.influence.domain.OSInfluence 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)

Example 30 with OSInfluence

use of com.onesignal.influence.domain.OSInfluence in project OneSignal-Android-SDK by OneSignal.

the class OutcomeEventIntegrationTests method assertNotificationChannelIndirectInfluence.

private void assertNotificationChannelIndirectInfluence(int indirectIdsLength) {
    OSInfluence influence = trackerFactory.getNotificationChannelTracker().getCurrentSessionInfluence();
    assertTrue(influence.getInfluenceType().isIndirect());
    assertEquals(indirectIdsLength, influence.getIds().length());
}
Also used : OSInfluence(com.onesignal.influence.domain.OSInfluence)

Aggregations

OSInfluence (com.onesignal.influence.domain.OSInfluence)40 Test (org.junit.Test)25 OneSignalPackagePrivateHelper (com.onesignal.OneSignalPackagePrivateHelper)23 OSChannelTracker (com.onesignal.influence.data.OSChannelTracker)7 JSONArray (org.json.JSONArray)5 ArrayList (java.util.ArrayList)3 Config (org.robolectric.annotation.Config)2 Bundle (android.os.Bundle)1 FCMBroadcastReceiver_onReceived_withBundle (com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_onReceived_withBundle)1 OSOutcomeEventParams (com.onesignal.outcomes.domain.OSOutcomeEventParams)1 OSOutcomeSource (com.onesignal.outcomes.domain.OSOutcomeSource)1 OSOutcomeSourceBody (com.onesignal.outcomes.domain.OSOutcomeSourceBody)1 GenerateNotificationRunner.getBaseNotifBundle (com.test.onesignal.GenerateNotificationRunner.getBaseNotifBundle)1