Skip to content

Commit fd64e75

Browse files
fix: SwiftUI not Rendering for Unidentified Users (#35)
* fix: SwiftUI not rendering for Unidentified Users * Add More Comprehensive Debug Logging
1 parent ebec5c9 commit fd64e75

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

mParticle-Rokt-Swift/MPRoktLayout.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ public class MPRoktLayout {
2929
config: RoktConfig? = nil,
3030
onEvent: ((RoktEvent) -> Void)? = nil
3131
) {
32-
confirmUser(attributes: attributes) {
32+
MPLog.debug("Initializing MPRoktLayout with arguments:", sdkTriggered.wrappedValue, viewName ?? "nil", locationName, attributes)
33+
confirmUser(attributes: attributes) { identifyCalled in
3334
let preparedAttributes = MPKitRokt.prepareAttributes(attributes, filteredUser: Optional<FilteredMParticleUser>.none, performMapping: true)
34-
35+
36+
MPLog.debug("Initializing RoktLayout with arguments:", sdkTriggered.wrappedValue, viewName ?? "nil", locationName, preparedAttributes)
3537
self.roktLayout = RoktLayout.init(
3638
sdkTriggered: sdkTriggered,
3739
viewName: viewName,
@@ -40,15 +42,23 @@ public class MPRoktLayout {
4042
config: config,
4143
onEvent: onEvent
4244
)
45+
// The Binding variable provided by the client allows us to trigger a re-render of the UI but we only want to do this if the value was true to start
46+
if identifyCalled && sdkTriggered.wrappedValue {
47+
MPLog.debug("Triggering Rokt Swift UI re-render")
48+
DispatchQueue.main.async {
49+
sdkTriggered.wrappedValue = false
50+
sdkTriggered.wrappedValue = true
51+
}
52+
}
4353
}
4454
}
4555

4656
func confirmUser(
4757
attributes: [String: String]?,
48-
completion: @escaping () -> Void
58+
completion: @escaping (Bool) -> Void
4959
) {
5060
guard let user = mparticle.identity.currentUser else {
51-
completion()
61+
completion(false)
5262
return
5363
}
5464
let email = attributes?["email"]
@@ -76,15 +86,17 @@ public class MPRoktLayout {
7686
if emailMismatch || hashedEmailMismatch {
7787
// If there is an existing email or hashed email but it doesn't match what was passed in, warn the customer
7888
if emailMismatch {
79-
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")
89+
MPLog.warning("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")
8090
}
8191
if hashedEmailMismatch {
82-
print("The existing hashed email on the user (\(user.identities[hashedEmailIdentity ?? NSNumber(value: -1)] ?? "nil")) does not match the email passed in to `selectPlacements:` (\(hashedEmail ?? "nil")). Please remember to sync the hashed email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
92+
MPLog.warning("The existing hashed email on the user (\(user.identities[hashedEmailIdentity ?? NSNumber(value: -1)] ?? "nil")) does not match the email passed in to `selectPlacements:` (\(hashedEmail ?? "nil")). Please remember to sync the hashed email identity to mParticle as soon as you receive it. We will now identify the user before creating the layout")
8393
}
8494

85-
syncIdentities(user: user, email: email, hashedEmail: hashedEmail, hashedEmailKey: hashedEmailIdentity, completion: completion)
95+
syncIdentities(user: user, email: email, hashedEmail: hashedEmail, hashedEmailKey: hashedEmailIdentity) {
96+
completion(true)
97+
}
8698
} else {
87-
completion()
99+
completion(false)
88100
}
89101
}
90102

@@ -103,11 +115,11 @@ public class MPRoktLayout {
103115

104116
mparticle.identity.identify(identityRequest) {apiResult, error in
105117
if let error = error {
106-
print("Failed to sync email from selectPlacement to user: \(error)")
118+
MPLog.error("Failed to sync email from selectPlacement to user: \(error)")
107119
completion()
108120
} else {
109121
if let identities = apiResult?.user.identities {
110-
print("Updated user identity based off selectPlacement's attributes: \(identities)")
122+
MPLog.debug("Updated user identity based off selectPlacement's attributes: \(identities)")
111123
}
112124
completion()
113125
}

mParticle-Rokt/MPKitRokt.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ - (MPKitExecStatus *)didFinishLaunchingWithConfiguration:(NSDictionary *)configu
5353
NSString *kitVersion = @"8.3.1";
5454

5555
// Initialize Rokt SDK here
56+
[MPKitRokt MPLog:[NSString stringWithFormat:@"Attempting to initialize Rokt with Kit Version: %@", kitVersion]];
5657
[Rokt initWithRoktTagId:partnerId mParticleSdkVersion:sdkVersion mParticleKitVersion:kitVersion onInitComplete:^(BOOL InitComplete) {
5758
if (InitComplete) {
5859
[self start];
60+
[MPKitRokt MPLog:@"Rokt Init Complete"];
5961
NSDictionary *userInfo = @{mParticleKitInstanceKey:[[self class] kitCode]};
6062
[[NSNotificationCenter defaultCenter] postNotificationName:@"mParticle.Rokt.Initialized"
6163
object:nil
@@ -98,6 +100,7 @@ - (MPKitExecStatus *)executeWithIdentifier:(NSString * _Nullable)identifier
98100
config:(MPRoktConfig * _Nullable)mpRoktConfig
99101
callbacks:(MPRoktEventCallback * _Nullable)callbacks
100102
filteredUser:(FilteredMParticleUser * _Nonnull)filteredUser {
103+
[MPKitRokt MPLog:[NSString stringWithFormat:@"Rokt Kit recieved `executeWithIdentifier` method with the following arguments: \n identifier: %@ \n attributes: %@ \n embeddedViews: %@ \n config: %@ \n callbacks: %@ \n filteredUser identities: %@", identifier, attributes, embeddedViews, mpRoktConfig, callbacks, filteredUser.userIdentities]];
101104
NSDictionary<NSString *, NSString *> *finalAtt = [MPKitRokt prepareAttributes:attributes filteredUser:filteredUser performMapping:NO];
102105

103106
//Convert MPRoktConfig to RoktConfig
@@ -176,6 +179,7 @@ - (RoktFrameworkType)mapMPWrapperSdkToRoktFrameworkType:(MPWrapperSdk)wrapperSdk
176179
finalAttributes = [[NSMutableDictionary alloc] initWithDictionary:@{sandboxKey: sandboxValue}];
177180
}
178181

182+
[MPKitRokt MPLog:[NSString stringWithFormat:@"Sandbox value: %@", finalAttributes[sandboxKey]]];
179183
return finalAttributes;
180184
}
181185

@@ -208,6 +212,7 @@ - (RoktFrameworkType)mapMPWrapperSdkToRoktFrameworkType:(MPWrapperSdk)wrapperSdk
208212
[finalAtt addEntriesFromDictionary:@{sandboxKey: attributes[sandboxKey]}];
209213
}
210214

215+
[MPKitRokt MPLog:[NSString stringWithFormat:@"Attributes updated with mapped user Attributes and Identities: %@", finalAtt]];
211216
return [self confirmSandboxAttribute:finalAtt];
212217
}
213218

@@ -744,6 +749,14 @@ - (MPKitExecStatus *)setOptOut:(BOOL)optOut {
744749
return [self execStatus:MPKitReturnCodeSuccess];
745750
}
746751

752+
+ (void)MPLog:(NSString *)string {
753+
NSString *msg = [NSString stringWithFormat:@"%@%@", @"MPRokt -> ", string];
754+
if ([[MParticle sharedInstance] environment] == MPEnvironmentDevelopment) {
755+
NSLog(@"%@", msg);
756+
}
757+
}
758+
759+
747760
+ (MPRoktEvent * _Nullable)mapEvent:(RoktEvent *)event {
748761
if (!event) {
749762
return nil;

0 commit comments

Comments
 (0)