-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMPRoktLayout.swift
More file actions
100 lines (89 loc) · 3.88 KB
/
Copy pathMPRoktLayout.swift
File metadata and controls
100 lines (89 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// MPRoktLayout.swift
// mParticle-Rokt
//
// Copyright 2025 Rokt Pte Ltd
//
// Licensed under the Rokt Software Development Kit (SDK) Terms of Use
// Version 2.0 (the "License");
//
// You may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at https://rokt.com/sdk-license-2-0/
import SwiftUI
import Rokt_Widget
import mParticle_Apple_SDK
import mParticle_Rokt
@available(iOS 15, *)
public class MPRoktLayout {
public var roktLayout: RoktLayout? = nil
let mparticle = MParticle.sharedInstance()
public init(
sdkTriggered: Binding<Bool>,
viewName: String? = nil,
locationName: String = "",
attributes: [String: String],
config: RoktConfig? = nil,
onEvent: ((RoktEvent) -> Void)? = nil
) {
confirmUser(attributes: attributes) {
let preparedAttributes = MPKitRokt.prepareAttributes(attributes, filteredUser: Optional<FilteredMParticleUser>.none, performMapping: true)
self.roktLayout = RoktLayout.init(
sdkTriggered: sdkTriggered,
viewName: viewName,
locationName: locationName,
attributes: preparedAttributes,
config: config,
onEvent: onEvent
)
}
}
func confirmUser(
attributes: [String: String]?,
completion: @escaping () -> Void
) {
guard let user = mparticle.identity.currentUser else {
completion()
return
}
let email = attributes?["email"]
let hashedEmail = attributes?["emailsha256"]
let userEmailIdentity = user.identities[NSNumber(value: MPIdentity.email.rawValue)]
let userHashedEmailIdentity = user.identities[NSNumber(value: MPIdentity.other.rawValue)]
let emailMismatch = email != nil && email != userEmailIdentity
let hashedEmailMismatch = hashedEmail != nil && hashedEmail != userHashedEmailIdentity
if emailMismatch || hashedEmailMismatch {
// If there is an existing email or hashed email but it doesn't match what was passed in, warn the customer
if emailMismatch {
print("The existing email on the user (\(userEmailIdentity ?? "nil")) does not match the email passed in to `selectPlacements:` (\(email ?? "nil")). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
}
if hashedEmailMismatch {
print("The existing hashed email on the user (\(userHashedEmailIdentity ?? "nil")) does not match the email passed in to `selectPlacements:` (\(hashedEmail ?? "nil")). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
}
syncIdentities(user: user, email: email, hashedEmail: hashedEmail, completion: completion)
} else {
completion()
}
}
func syncIdentities(
user: MParticleUser,
email: String?,
hashedEmail: String?,
completion: @escaping () -> Void
) {
let identityRequest = MPIdentityApiRequest(user: user)
identityRequest.setIdentity(email, identityType: .email)
identityRequest.setIdentity(hashedEmail, identityType: .other)
mparticle.identity.identify(identityRequest) {apiResult, error in
if let error = error {
print("Failed to sync email from selectPlacement to user: \(error)")
completion()
} else {
if let identities = apiResult?.user.identities {
print("Updated user identity based off selectPlacement's attributes: \(identities)")
}
completion()
}
}
}
}