-
Notifications
You must be signed in to change notification settings - Fork 71
feat: Add user attribute mapping #343
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
Changes from all commits
9d93755
b47ed5e
8d163d0
627732d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,30 +163,47 @@ @implementation MPRokt | |
|
|
||
| - (void)selectPlacements:(NSString *)identifier | ||
| attributes:(NSDictionary<NSString *, NSString *> * _Nullable)attributes { | ||
| for (NSString *key in attributes) { | ||
| [[MParticle sharedInstance].identity.currentUser setUserAttribute:key value:attributes[key]]; | ||
| } | ||
|
|
||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| // Forwarding call to kits | ||
| MPForwardQueueParameters *queueParameters = [[MPForwardQueueParameters alloc] init]; | ||
| [queueParameters addParameter:identifier]; | ||
| [queueParameters addParameter:attributes]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| NSArray<NSDictionary<NSString *, NSString *> *> *attributeMap = [self getRoktPlacementAttributes]; | ||
|
|
||
| // If attributeMap is nil the kit hasn't been initialized | ||
| if (attributeMap) { | ||
| NSMutableDictionary *mappedAttributes = attributes.mutableCopy; | ||
| for (NSDictionary<NSString *, NSString *> *map in attributeMap) { | ||
| NSString *mapFrom = map[@"map"]; | ||
| NSString *mapTo = map[@"value"]; | ||
| if (mappedAttributes[mapFrom]) { | ||
| NSString * value = mappedAttributes[mapFrom]; | ||
| [mappedAttributes removeObjectForKey:mapFrom]; | ||
| mappedAttributes[mapTo] = value; | ||
| } | ||
| } | ||
| for (NSString *key in mappedAttributes) { | ||
| [[MParticle sharedInstance].identity.currentUser setUserAttribute:key value:mappedAttributes[key]]; | ||
| } | ||
|
Comment on lines
+170
to
+182
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be DRYed up into a separate function so that it's called from both |
||
|
|
||
| SEL roktSelector = @selector(executeWithViewName:attributes:placements:onLoad:onUnLoad:onShouldShowLoadingIndicator:onShouldHideLoadingIndicator:onEmbeddedSizeChange:filteredUser:); | ||
| [[MParticle sharedInstance].kitContainer_PRIVATE forwardSDKCall:roktSelector | ||
| event:nil | ||
| parameters:queueParameters | ||
| messageType:MPMessageTypeEvent | ||
| userInfo:nil | ||
| ]; | ||
| }); | ||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| // Forwarding call to kits | ||
| MPForwardQueueParameters *queueParameters = [[MPForwardQueueParameters alloc] init]; | ||
| [queueParameters addParameter:identifier]; | ||
| [queueParameters addParameter:mappedAttributes]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
| [queueParameters addParameter:nil]; | ||
|
|
||
| SEL roktSelector = @selector(executeWithViewName:attributes:placements:onLoad:onUnLoad:onShouldShowLoadingIndicator:onShouldHideLoadingIndicator:onEmbeddedSizeChange:filteredUser:); | ||
| [[MParticle sharedInstance].kitContainer_PRIVATE forwardSDKCall:roktSelector | ||
| event:nil | ||
| parameters:queueParameters | ||
| messageType:MPMessageTypeEvent | ||
| userInfo:nil | ||
| ]; | ||
| }); | ||
| } else { | ||
| MPILogVerbose(@"[MParticle.Rokt selectPlacements:attributes:] not performed due to kit not being configured"); | ||
| } | ||
|
Comment on lines
+184
to
+206
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also be DRYed up with the method below using all arguments and just pass either 2, or all arguments to it. |
||
| } | ||
|
|
||
| - (void)selectPlacements:(NSString *)identifier | ||
|
|
@@ -197,30 +214,90 @@ - (void)selectPlacements:(NSString *)identifier | |
| onShouldShowLoadingIndicator:(void (^ _Nullable)(void))onShouldShowLoadingIndicator | ||
| onShouldHideLoadingIndicator:(void (^ _Nullable)(void))onShouldHideLoadingIndicator | ||
| onEmbeddedSizeChange:(void (^ _Nullable)(NSString * _Nonnull, CGFloat))onEmbeddedSizeChange { | ||
| for (NSString *key in attributes) { | ||
| [[MParticle sharedInstance].identity.currentUser setUserAttribute:key value:attributes[key]]; | ||
| NSArray<NSDictionary<NSString *, NSString *> *> *attributeMap = [self getRoktPlacementAttributes]; | ||
|
|
||
| // If attributeMap is nil the kit hasn't been initialized | ||
| if (attributeMap) { | ||
| NSMutableDictionary *mappedAttributes = attributes.mutableCopy; | ||
| for (NSDictionary<NSString *, NSString *> *map in attributeMap) { | ||
| NSString *mapFrom = map[@"map"]; | ||
| NSString *mapTo = map[@"value"]; | ||
| if (mappedAttributes[mapFrom]) { | ||
| NSString * value = mappedAttributes[mapFrom]; | ||
| [mappedAttributes removeObjectForKey:mapFrom]; | ||
| mappedAttributes[mapTo] = value; | ||
| } | ||
| } | ||
| for (NSString *key in mappedAttributes) { | ||
| [[MParticle sharedInstance].identity.currentUser setUserAttribute:key value:mappedAttributes[key]]; | ||
| } | ||
|
|
||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| // Forwarding call to kits | ||
| MPForwardQueueParameters *queueParameters = [[MPForwardQueueParameters alloc] init]; | ||
| [queueParameters addParameter:identifier]; | ||
| [queueParameters addParameter:mappedAttributes]; | ||
| [queueParameters addParameter:placements]; | ||
| [queueParameters addParameter:onLoad]; | ||
| [queueParameters addParameter:onUnLoad]; | ||
| [queueParameters addParameter:onShouldShowLoadingIndicator]; | ||
| [queueParameters addParameter:onShouldHideLoadingIndicator]; | ||
| [queueParameters addParameter:onEmbeddedSizeChange]; | ||
|
|
||
| SEL roktSelector = @selector(executeWithViewName:attributes:placements:onLoad:onUnLoad:onShouldShowLoadingIndicator:onShouldHideLoadingIndicator:onEmbeddedSizeChange:filteredUser:); | ||
| [[MParticle sharedInstance].kitContainer_PRIVATE forwardSDKCall:roktSelector | ||
| event:nil | ||
| parameters:queueParameters | ||
| messageType:MPMessageTypeEvent | ||
| userInfo:nil | ||
| ]; | ||
| }); | ||
| } else { | ||
| MPILogVerbose(@"[MParticle.Rokt selectPlacements: not performed since Kit not configured"); | ||
| } | ||
| } | ||
|
|
||
| - (NSArray<NSDictionary<NSString *, NSString *> *> *)getRoktPlacementAttributes { | ||
| NSArray<NSDictionary<NSString *, NSString *> *> *attributeMap = nil; | ||
|
|
||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
| // Forwarding call to kits | ||
| MPForwardQueueParameters *queueParameters = [[MPForwardQueueParameters alloc] init]; | ||
| [queueParameters addParameter:identifier]; | ||
| [queueParameters addParameter:attributes]; | ||
| [queueParameters addParameter:placements]; | ||
| [queueParameters addParameter:onLoad]; | ||
| [queueParameters addParameter:onUnLoad]; | ||
| [queueParameters addParameter:onShouldShowLoadingIndicator]; | ||
| [queueParameters addParameter:onShouldHideLoadingIndicator]; | ||
| [queueParameters addParameter:onEmbeddedSizeChange]; | ||
| // Get the kit configuration | ||
| NSArray<NSDictionary *> *kitConfigs = [MParticle sharedInstance].kitContainer_PRIVATE.originalConfig.copy; | ||
| NSDictionary *roktKitConfig; | ||
| for (NSDictionary *kitConfig in kitConfigs) { | ||
| if (kitConfig[@"id"] != nil && [kitConfig[@"id"] integerValue] == 181) { | ||
| roktKitConfig = kitConfig; | ||
| } | ||
| } | ||
|
|
||
| // Get the placement attributes map | ||
| NSString *strAttributeMap; | ||
| NSData *dataAttributeMap; | ||
| if (roktKitConfig != nil) { | ||
| // Rokt Kit is available though there may not be an attribute map | ||
| attributeMap = @[]; | ||
| if (roktKitConfig[@"placementAttributes"] != [NSNull null]) { | ||
| strAttributeMap = [roktKitConfig[@"placementAttributes"] stringByRemovingPercentEncoding]; | ||
| dataAttributeMap = [strAttributeMap dataUsingEncoding:NSUTF8StringEncoding]; | ||
| } | ||
| } | ||
|
|
||
| if (dataAttributeMap != nil) { | ||
| // Convert it to an array of dictionaries | ||
| NSError *error = nil; | ||
|
|
||
| SEL roktSelector = @selector(executeWithViewName:attributes:placements:onLoad:onUnLoad:onShouldShowLoadingIndicator:onShouldHideLoadingIndicator:onEmbeddedSizeChange:filteredUser:); | ||
| [[MParticle sharedInstance].kitContainer_PRIVATE forwardSDKCall:roktSelector | ||
| event:nil | ||
| parameters:queueParameters | ||
| messageType:MPMessageTypeEvent | ||
| userInfo:nil | ||
| ]; | ||
| }); | ||
| @try { | ||
| attributeMap = [NSJSONSerialization JSONObjectWithData:dataAttributeMap options:kNilOptions error:&error]; | ||
| } @catch (NSException *exception) { | ||
| } | ||
|
|
||
| if (attributeMap && !error) { | ||
| NSLog(@"%@", attributeMap); | ||
| } else { | ||
| NSLog(@"%@", error); | ||
| } | ||
| } | ||
|
|
||
| return attributeMap; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test stubs the
getRoktPlacementAttributescall.Can you add a test for
getRoktPlacementAttributesitself, using a config like the one you put in the chat. passing in "[{"jsmap":null,"map":"f.name","maptype":"UserAttributeClass.Name","value":"firstname"},{"jsmap":null,"map":"zip","maptype":"UserAttributeClass.Name","value":"billingzipcode"},{"jsmap":null,"map":"l.name","maptype":"UserAttributeClass.Name","value":"lastname"}]"and expecting what you have in line 168 back?
(note that i copied and pasted what you had in #temp-rokt-client but updated the mapping from
f.name-->firstnameinstead of email