Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOB-9560] implements identity resolution #830

Open
wants to merge 7 commits into
base: feature/itbl_track_anon_user
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions swift-sdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
5B5AA717284F1A6D0093FED4 /* MockNetworkSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B5AA710284F1A6D0093FED4 /* MockNetworkSession.swift */; };
5B6C3C1127CE871F00B9A753 /* NavInboxSessionUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B6C3C1027CE871F00B9A753 /* NavInboxSessionUITests.swift */; };
5B88BC482805D09D004016E5 /* NetworkSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B88BC472805D09D004016E5 /* NetworkSession.swift */; };
9F0616412C9CA9D400FE2E6A /* IterableIdentityResolution.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F0616402C9CA9D200FE2E6A /* IterableIdentityResolution.swift */; };
9F76FFFF2B17884900962526 /* EmbeddedHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F76FFFE2B17884900962526 /* EmbeddedHelper.swift */; };
9FF05EAC2AFEA5FA005311F7 /* MockAuthManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF05EAB2AFEA5FA005311F7 /* MockAuthManager.swift */; };
9FF05EAD2AFEA5FA005311F7 /* MockAuthManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF05EAB2AFEA5FA005311F7 /* MockAuthManager.swift */; };
Expand Down Expand Up @@ -638,6 +639,7 @@
5B6C3C1027CE871F00B9A753 /* NavInboxSessionUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavInboxSessionUITests.swift; sourceTree = "<group>"; };
5B88BC472805D09D004016E5 /* NetworkSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkSession.swift; sourceTree = "<group>"; };
5BFC7CED27FC9AF300E77479 /* inbox-ui-tests-app.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "inbox-ui-tests-app.entitlements"; sourceTree = "<group>"; };
9F0616402C9CA9D200FE2E6A /* IterableIdentityResolution.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IterableIdentityResolution.swift; sourceTree = "<group>"; };
9F76FFFE2B17884900962526 /* EmbeddedHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmbeddedHelper.swift; sourceTree = "<group>"; };
9FF05EAB2AFEA5FA005311F7 /* MockAuthManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockAuthManager.swift; sourceTree = "<group>"; };
AC02480722791E2100495FB9 /* IterableInboxNavigationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IterableInboxNavigationViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1667,6 +1669,7 @@
E9EA7C9A2C1EDE4400A9D6FB /* AnonymousTracking */ = {
isa = PBXGroup;
children = (
9F0616402C9CA9D200FE2E6A /* IterableIdentityResolution.swift */,
E9EA7C9E2C1EDE5800A9D6FB /* AnonymousUserManager.swift */,
E9EA7C9B2C1EDE5800A9D6FB /* AnonymousUserManager+Functions.swift */,
E9EA7C9D2C1EDE5800A9D6FB /* AnonymousUserManagerProtocol.swift */,
Expand Down Expand Up @@ -2202,6 +2205,7 @@
ACB8273F22372A5C00DB17D3 /* IterableHtmlMessageViewController.swift in Sources */,
AC5812F624F3A90F007E6D36 /* OfflineRequestProcessor.swift in Sources */,
5555425028BED1B400DB5D20 /* KeychainWrapper.swift in Sources */,
9F0616412C9CA9D400FE2E6A /* IterableIdentityResolution.swift in Sources */,
AC81918A22713A400014955E /* AbstractDiffCalculator.swift in Sources */,
ACA95D2D275494A100AF4666 /* InboxViewRepresentable.swift in Sources */,
AC684A88222F4FDD00F29749 /* InAppDisplayer.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion swift-sdk/Internal/AnonymousUserManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public class AnonymousUserManager: AnonymousUserManagerProtocol {
}

self.localStorage.userIdAnnon = userId
IterableAPI.setUserId(userId, nil, merge: false, nil, nil, true)
IterableAPI.implementation?.setUserId(userId, authToken: nil, successHandler: nil, failureHandler: nil, isAnon: true, identityResolution: nil)
self.syncNonSyncedEvents()
}
}
Expand Down
6 changes: 3 additions & 3 deletions swift-sdk/Internal/AnonymousUserMerge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

protocol AnonymousUserMergeProtocol {
func tryMergeUser(destinationUser: String?, isEmail: Bool, shouldMerge: Bool, onMergeResult: @escaping MergeActionHandler)
func tryMergeUser(destinationUser: String?, isEmail: Bool, merge: Bool, onMergeResult: @escaping MergeActionHandler)
}

class AnonymousUserMerge: AnonymousUserMergeProtocol {
Expand All @@ -23,10 +23,10 @@ class AnonymousUserMerge: AnonymousUserMergeProtocol {
self.localStorage = localStorage
}

func tryMergeUser(destinationUser: String?, isEmail: Bool, shouldMerge: Bool, onMergeResult: @escaping MergeActionHandler) {
func tryMergeUser(destinationUser: String?, isEmail: Bool, merge: Bool, onMergeResult: @escaping MergeActionHandler) {
let anonymousUserId = localStorage.userIdAnnon

if (anonymousUserId != nil && destinationUser != nil && shouldMerge) {
if (anonymousUserId != nil && destinationUser != nil && merge) {
let destinationEmail = isEmail ? destinationUser : nil
let destinationUserId = isEmail ? nil : destinationUser

Expand Down
24 changes: 13 additions & 11 deletions swift-sdk/Internal/InternalIterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,16 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
_payloadData = data
}

func setEmail(_ email: String?, authToken: String? = nil, merge: Bool = true, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
func setEmail(_ email: String?, authToken: String? = nil, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil, identityResolution: IterableIdentityResolution? = nil) {

ITBInfo()

//let shouldMerge = merge && localStorage.userIdAnnon != nil

let merge = identityResolution?.mergeOnAnonymousToKnown ?? config.identityResolution.mergeOnAnonymousToKnown
let replay = identityResolution?.replayOnVisitorToKnown ?? config.identityResolution.replayOnVisitorToKnown

if(config.enableAnonTracking) {
if(email != nil) {
attemptAndProcessMerge(shouldMerge: merge, destinationUser: email, isEmail: true, failureHandler: failureHandler)
attemptAndProcessMerge(merge: merge ?? true, replay: replay ?? true, destinationUser: email, isEmail: true, failureHandler: failureHandler)
}
self.localStorage.userIdAnnon = nil
}
Expand All @@ -164,14 +165,15 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {

}

func setUserId(_ userId: String?, authToken: String? = nil, merge: Bool = true, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil, isAnon: Bool = false) {
func setUserId(_ userId: String?, authToken: String? = nil, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil, isAnon: Bool = false, identityResolution: IterableIdentityResolution? = nil) {
ITBInfo()

//let shouldMerge = && localStorage.userIdAnnon != nil

let merge = identityResolution?.mergeOnAnonymousToKnown ?? config.identityResolution.mergeOnAnonymousToKnown
let replay = identityResolution?.replayOnVisitorToKnown ?? config.identityResolution.replayOnVisitorToKnown

if(config.enableAnonTracking) {
if(userId != nil && userId != localStorage.userIdAnnon) {
attemptAndProcessMerge(shouldMerge: merge, destinationUser: userId, isEmail: false, failureHandler: failureHandler)
attemptAndProcessMerge(merge: merge ?? true, replay: replay ?? true, destinationUser: userId, isEmail: false, failureHandler: failureHandler)
}

if(!isAnon) {
Expand Down Expand Up @@ -203,11 +205,11 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
logoutPreviousUser()
}

func attemptAndProcessMerge(shouldMerge: Bool, destinationUser: String?, isEmail: Bool, failureHandler: OnFailureHandler? = nil) {
anonymousUserMerge.tryMergeUser(destinationUser: destinationUser, isEmail: isEmail, shouldMerge: shouldMerge) { mergeResult, error in
func attemptAndProcessMerge(merge: Bool, replay: Bool, destinationUser: String?, isEmail: Bool, failureHandler: OnFailureHandler? = nil) {
anonymousUserMerge.tryMergeUser(destinationUser: destinationUser, isEmail: isEmail, merge: merge) { mergeResult, error in

if mergeResult == MergeResult.mergenotrequired || mergeResult == MergeResult.mergesuccessful {
if (shouldMerge) {
if (replay) {
self.anonymousUserManager.syncNonSyncedEvents()
}
} else {
Expand Down
23 changes: 23 additions & 0 deletions swift-sdk/Internal/IterableIdentityResolution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Untitled.swift
// swift-sdk
//
// Created by Evan Greer on 9/19/24.
// Copyright © 2024 Iterable. All rights reserved.
//

import Foundation
@objc public class IterableIdentityResolution: NSObject {

/// userId or email of the signed-in user
public var replayOnVisitorToKnown: Bool?

/// the authToken which caused the failure
public let mergeOnAnonymousToKnown: Bool?

public init(replayOnVisitorToKnown: Bool?,
mergeOnAnonymousToKnown: Bool?) {
self.replayOnVisitorToKnown = replayOnVisitorToKnown
self.mergeOnAnonymousToKnown = mergeOnAnonymousToKnown
}
}
22 changes: 11 additions & 11 deletions swift-sdk/IterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,21 @@ import UIKit

// MARK: - SDK

public static func setEmail(_ email: String?, _ authToken: String? = nil, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil) {
implementation?.setEmail(email, authToken: authToken, successHandler: successHandler, failureHandler: failureHandler)
public static func setEmail(_ email: String?, _ authToken: String? = nil, _ identityResolution: IterableIdentityResolution? = nil, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil) {
implementation?.setEmail(email, authToken: authToken, successHandler: successHandler, failureHandler: failureHandler, identityResolution: identityResolution)
}

public static func setUserId(_ userId: String?, _ authToken: String? = nil, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil) {
implementation?.setUserId(userId, authToken: authToken, successHandler: successHandler, failureHandler: failureHandler)
public static func setUserId(_ userId: String?, _ authToken: String? = nil, _ identityResolution: IterableIdentityResolution? = nil, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil) {
implementation?.setUserId(userId, authToken: authToken, successHandler: successHandler, failureHandler: failureHandler, identityResolution: identityResolution)
}

public static func setEmail(_ email: String?, _ authToken: String? = nil, merge: Bool = true, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil) {
implementation?.setEmail(email, authToken: authToken, merge: merge, successHandler: successHandler, failureHandler: failureHandler)
}

public static func setUserId(_ userId: String?, _ authToken: String? = nil, merge: Bool = true, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil, _ isAnon: Bool = false) {
implementation?.setUserId(userId, authToken: authToken, merge: merge,successHandler: successHandler, failureHandler: failureHandler, isAnon: isAnon)
}
// public static func setEmail(_ email: String?, _ authToken: String? = nil, _ identityResolution: IterableIdentityResolution? = nil, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil) {
// implementation?.setEmail(email, authToken: authToken, successHandler: successHandler, failureHandler: failureHandler, identityResolution: identityResolution)
// }
//
// public static func setUserId(_ userId: String?, _ authToken: String? = nil, _ identityResolution: IterableIdentityResolution? = nil, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil, _ isAnon: Bool = false) {
// implementation?.setUserId(userId, authToken: authToken, successHandler: successHandler, failureHandler: failureHandler, isAnon: isAnon, identityResolution: identityResolution)
// }

/// Handle a Universal Link
///
Expand Down
2 changes: 2 additions & 0 deletions swift-sdk/IterableConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,6 @@ public class IterableConfig: NSObject {

// How many events can be stored in the local storage. By default limt is 100.
public var eventThresholdLimit: Int = 100

public var identityResolution: IterableIdentityResolution = IterableIdentityResolution(replayOnVisitorToKnown: true, mergeOnAnonymousToKnown: true)
}
Loading