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

fix: forward original event names to UI for kits with mapped events #301

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions UnitTests/MPKitContainerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1482,13 +1482,19 @@ - (void)testForwardAppsFlyerEvent {

MPKitRegister *kitRegister = [[MPKitRegister alloc] initWithName:@"AppsFlyer" className:@"MPKitAppsFlyerTest"];
MPKitFilter *kitFilter = [kitContainer filter:kitRegister forEvent:event selector:@selector(logEvent:)];
MPEvent *eventCopy = (MPEvent *)kitFilter.originalEventCopy;


XCTAssert([kitFilter.forwardEvent isKindOfClass:[MPEvent class]]);
MPEvent *forwardEvent = (MPEvent *)kitFilter.forwardEvent;
XCTAssertNotNil(forwardEvent);
XCTAssertEqualObjects(forwardEvent.name, @"new_premium_subscriber");
XCTAssertNotNil(forwardEvent.customAttributes);
XCTAssertEqual(forwardEvent.customAttributes.count, 3);
XCTAssertNotNil(kitFilter.appliedProjections);
XCTAssertEqualObjects(eventCopy.name, @"subscription_success");
XCTAssertNotNil(eventCopy.customAttributes);
XCTAssertEqual(eventCopy.customAttributes.count, 3);
}

- (void)testForwardAppsFlyerCommerceEvent {
Expand Down Expand Up @@ -1650,13 +1656,17 @@ - (void)testForwardAppsFlyerCommerceEvent {

MPKitRegister *kitRegister = [[MPKitRegister alloc] initWithName:@"AppsFlyer" className:@"MPKitAppsFlyerTest"];
MPKitFilter *kitFilter = [kitContainer filter:kitRegister forCommerceEvent:commerceEvent];
MPEvent *commerceEventCopy = (MPEvent *)kitFilter.originalCommerceEventCopy;

XCTAssert([kitFilter.forwardEvent isKindOfClass:[MPEvent class]]);
MPEvent *event = (MPEvent *)kitFilter.forwardEvent;
XCTAssertEqualObjects(event.customAttributes[@"af_quantity"], @"1");
XCTAssertEqualObjects(event.customAttributes[@"af_content_id"], @"OutATime");
XCTAssertEqualObjects(event.customAttributes[@"af_content_type"], @"Time Machine");
XCTAssertEqualObjects(event.name, @"af_add_to_cart");
XCTAssertNotNil(kitFilter.appliedProjections);
XCTAssertEqual(commerceEventCopy.type, MPEventTypeAddToCart);

}

- (void)testMatchArrayProjection {
Expand Down Expand Up @@ -1748,13 +1758,17 @@ - (void)testMatchArrayProjection {
XCTAssertNotNil(forwardEvent);
XCTAssertNotNil(forwardEvent.customAttributes);
XCTAssertEqual(forwardEvent.customAttributes.count, 2);
MPEvent *eventCopy = (MPEvent *)kitFilter.originalEventCopy;

[foundEventNames addObject:forwardEvent.name];

if (foundEventNames.count == 2) {
XCTAssertTrue([foundEventNames containsObject:@"X_NEW_SUBSCRIPTION"]);
XCTAssertTrue([foundEventNames containsObject:@"X_NEW_NOAH_SUBSCRIPTION"]);
}
XCTAssertNotNil(kitFilter.appliedProjections);
XCTAssertEqual(kitFilter.appliedProjections.count, 2);
XCTAssertEqualObjects(eventCopy.name, @"SUBSCRIPTION_END");
}

- (void)testScreenViewProjectionToBaseEvent {
Expand Down Expand Up @@ -1847,10 +1861,13 @@ - (void)testScreenViewProjectionToBaseEvent {
[(id <MPKitProtocol>)[kitWrapperMock reject] logScreen:OCMOCK_ANY];

MPKitFilter *kitFilter = [kitContainer filter:kitRegister forEvent:event selector:@selector(logScreen:)];
MPEvent *eventCopy = (MPEvent *)kitFilter.originalEventCopy;

[kitWrapperMock verifyWithDelay:5.0];

XCTAssert([kitFilter.forwardEvent isKindOfClass:[MPEvent class]]);
XCTAssertNotNil(kitFilter.appliedProjections);
XCTAssertEqualObjects(eventCopy.name, @"SUBSCRIPTION_END");
}

- (void)testNonMatchingMatchArrayProjection {
Expand Down
23 changes: 13 additions & 10 deletions mParticle-Apple-SDK/Data Model/MPForwardRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,22 @@ - (instancetype)initWithMessageType:(MPMessageType)messageType execStatus:(MPKit
if (kitFilter.appliedProjections.count > 0) {
NSMutableArray *projections = [[NSMutableArray alloc] initWithCapacity:kitFilter.appliedProjections.count];
NSMutableDictionary *projectionDictionary;
NSString *currentProjectionName = ((MPEvent *)kitFilter.originalEvent).name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check that originalEvent is an MPEvent before casting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added as well


for (MPEventProjection *eventProjection in kitFilter.appliedProjections) {
projectionDictionary = [[NSMutableDictionary alloc] initWithCapacity:4];
projectionDictionary[kMPFRProjectionId] = @(eventProjection.projectionId);
projectionDictionary[kMPMessageTypeKey] = NSStringFromMessageType(messageType);

projectionDictionary[kMPEventTypeKey] = NSStringFromEventType(eventProjection.eventType);

if (eventProjection.projectedName) {
projectionDictionary[kMPFRProjectionName] = eventProjection.projectedName;
if (eventProjection.projectedName == currentProjectionName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Objective-C, you can't use == to compare objects (including strings). The == will just compare if the pointers are equal, there's no operator overloading. So you need to compare using isEqual:

Suggested change
if (eventProjection.projectedName == currentProjectionName) {
if ([eventProjection.projectedName isEqual:currentProjectionName]) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jus added this change

projectionDictionary = [[NSMutableDictionary alloc] initWithCapacity:4];
projectionDictionary[kMPFRProjectionId] = @(eventProjection.projectionId);
projectionDictionary[kMPMessageTypeKey] = NSStringFromMessageType(messageType);

projectionDictionary[kMPEventTypeKey] = NSStringFromEventType(eventProjection.eventType);

if (eventProjection.projectedName) {
projectionDictionary[kMPFRProjectionName] = eventProjection.projectedName;
}

[projections addObject:projectionDictionary];
}

[projections addObject:projectionDictionary];
}

_dataDictionary[kMPFRProjections] = projections;
Expand Down
67 changes: 41 additions & 26 deletions mParticle-Apple-SDK/Kits/MPKitContainer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ - (MPKitFilter *)filter:(id<MPExtensionKitProtocol>)kitRegister forCommerceEvent

if (!projectedEvents.empty()) {
for (auto &projectedEvent : projectedEvents) {
kitFilter = [[MPKitFilter alloc] initWithEvent:projectedEvent shouldFilter:NO appliedProjections:appliedProjectionsArray];
kitFilter = [[MPKitFilter alloc] initWithEvent:projectedEvent shouldFilter:NO appliedProjections:appliedProjectionsArray eventCopy:nil commerceEventCopy:commerceEvent];
[self attemptToLogEventToKit:kitRegister kitFilter:kitFilter selector:@selector(logEvent:) parameters:nil messageType:MPMessageTypeEvent userInfo:[[NSDictionary alloc] init]];
}
}
Expand Down Expand Up @@ -905,7 +905,7 @@ - (MPKitFilter *)filter:(id<MPExtensionKitProtocol>)kitRegister forEvent:(MPEven
NSArray<MPEventProjection *> *appliedProjectionsArray = !appliedProjections.empty() ? [NSArray arrayWithObjects:&appliedProjections[0] count:appliedProjections.size()] : nil;

for (auto &projectedEvent : projectedEvents) {
kitFilter = [[MPKitFilter alloc] initWithEvent:projectedEvent shouldFilter:shouldFilter appliedProjections:appliedProjectionsArray];
kitFilter = [[MPKitFilter alloc] initWithEvent:projectedEvent shouldFilter:shouldFilter appliedProjections:appliedProjectionsArray eventCopy:event commerceEventCopy:nil];
SEL mutableSelector = selector;
if (selector == @selector(logScreen:)) {
for (int i = 0; i < appliedProjectionsArray.count; i += 1) {
Expand Down Expand Up @@ -2212,13 +2212,7 @@ - (void)attemptToLogCommerceEventToKit:(id<MPExtensionKitProtocol>)kitRegister k
if (execStatus.success && ![lastKit isEqualToNumber:currentKit]) {
lastKit = currentKit;

MPForwardRecord *forwardRecord = [[MPForwardRecord alloc] initWithMessageType:MPMessageTypeCommerceEvent
execStatus:execStatus
kitFilter:kitFilter
originalEvent:kitFilter.originalCommerceEvent];
dispatch_async([MParticle messageQueue], ^{
[[MParticle sharedInstance].persistenceController saveForwardRecord:forwardRecord];
});
[self forwardCommerceEventRecord:kitFilter execStatus:execStatus commerceEvent:kitFilter.originalCommerceEvent];
MPILogDebug(@"Forwarded logCommerceEvent call to kit: %@", kitRegister.name);
}
});
Expand Down Expand Up @@ -2339,28 +2333,49 @@ - (void)attemptToLogEventToKit:(id<MPExtensionKitProtocol>)kitRegister kitFilter
if (execStatus.success && ![lastKit isEqualToNumber:currentKit] && messageType != MPMessageTypeUnknown && messageType != MPMessageTypeMedia) {
lastKit = currentKit;

MPForwardRecord *forwardRecord = nil;

if (messageType == MPMessageTypeOptOut || messageType == MPMessageTypePushRegistration) {
forwardRecord = [[MPForwardRecord alloc] initWithMessageType:messageType
execStatus:execStatus
stateFlag:[userInfo[@"state"] boolValue]];
} else {
forwardRecord = [[MPForwardRecord alloc] initWithMessageType:messageType
execStatus:execStatus
kitFilter:kitFilter
originalEvent:kitFilter.originalEvent];
}

if (forwardRecord != nil) {
dispatch_async([MParticle messageQueue], ^{
[[MParticle sharedInstance].persistenceController saveForwardRecord:forwardRecord];
});
if (!kitFilter.appliedProjections) {
[self forwardEventRecord:kitFilter messageType:messageType userInfo:userInfo execStatus:execStatus event:kitFilter.originalEvent];
} else if (kitFilter.originalCommerceEventCopy) {
[self forwardCommerceEventRecord:kitFilter execStatus:execStatus commerceEvent:kitFilter.originalCommerceEventCopy];
} else if (kitFilter.originalEventCopy) {
[self forwardEventRecord:kitFilter messageType:messageType userInfo:userInfo execStatus:execStatus event:kitFilter.originalEventCopy];
}
}
});
}

- (void)forwardEventRecord:(MPKitFilter *)kitFilter messageType:(MPMessageType)messageType userInfo:(NSDictionary *)userInfo execStatus:(MPKitExecStatus*) execStatus event:(MPBaseEvent *)event{
MPForwardRecord *forwardRecord = nil;

if (messageType == MPMessageTypeOptOut || messageType == MPMessageTypePushRegistration) {
forwardRecord = [[MPForwardRecord alloc] initWithMessageType:messageType
execStatus:execStatus
stateFlag:[userInfo[@"state"] boolValue]];
} else {

forwardRecord = [[MPForwardRecord alloc] initWithMessageType:messageType
execStatus:execStatus
kitFilter:kitFilter
originalEvent:event];
}

if (forwardRecord != nil) {
dispatch_async([MParticle messageQueue], ^{
[[MParticle sharedInstance].persistenceController saveForwardRecord:forwardRecord];
});
}
}

- (void)forwardCommerceEventRecord:(MPKitFilter *)kitFilter execStatus:(MPKitExecStatus*) execStatus commerceEvent:(MPCommerceEvent *)commerceEvent{
MPForwardRecord *forwardRecord = [[MPForwardRecord alloc] initWithMessageType:MPMessageTypeCommerceEvent
execStatus:execStatus
kitFilter:kitFilter
originalEvent:commerceEvent];
dispatch_async([MParticle messageQueue], ^{
[[MParticle sharedInstance].persistenceController saveForwardRecord:forwardRecord];
});
}

- (void)forwardSDKCall:(SEL)selector userAttributeKey:(NSString *)key value:(id)value kitHandler:(void (^)(id<MPKitProtocol> kit, MPKitConfiguration * _Nonnull kitConfiguration))kitHandler {
NSArray<id<MPExtensionKitProtocol>> *activeKitsRegistry = [self activeKitsRegistry];

Expand Down
4 changes: 3 additions & 1 deletion mParticle-Apple-SDK/Kits/MPKitFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
@property (nonatomic, strong, readonly, nullable) NSDictionary *filteredAttributes;
@property (nonatomic, strong, readonly, nullable) MPCommerceEvent *originalCommerceEvent;
@property (nonatomic, strong, readonly, nullable) MPBaseEvent *originalEvent;
@property (nonatomic, strong, readonly, nullable) MPCommerceEvent *originalCommerceEventCopy;
@property (nonatomic, strong, readonly, nullable) MPBaseEvent *originalEventCopy;
@property (nonatomic, strong, readonly, nullable) MPCommerceEvent *forwardCommerceEvent;
@property (nonatomic, strong, readonly, nullable) MPBaseEvent *forwardEvent;
@property (nonatomic, strong, readonly, nullable) MPConsentState *forwardConsentState;
Expand All @@ -19,7 +21,7 @@
- (nonnull instancetype)initWithFilter:(BOOL)shouldFilter;
- (nonnull instancetype)initWithFilter:(BOOL)shouldFilter filteredAttributes:(nullable NSDictionary *)filteredAttributes;
- (nonnull instancetype)initWithEvent:(nonnull MPBaseEvent *)event shouldFilter:(BOOL)shouldFilter;
- (nonnull instancetype)initWithEvent:(nonnull MPBaseEvent *)event shouldFilter:(BOOL)shouldFilter appliedProjections:(nullable NSArray<MPEventProjection *> *)appliedProjections;
- (nonnull instancetype)initWithEvent:(nonnull MPBaseEvent *)event shouldFilter:(BOOL)shouldFilter appliedProjections:(nullable NSArray<MPEventProjection *> *)appliedProjections eventCopy:(nullable MPBaseEvent *)eventCopy commerceEventCopy:(nullable MPCommerceEvent *)commerceEventCopy;
- (nonnull instancetype)initWithCommerceEvent:(nonnull MPCommerceEvent *)commerceEvent shouldFilter:(BOOL)shouldFilter;
- (nonnull instancetype)initWithCommerceEvent:(nonnull MPCommerceEvent *)commerceEvent shouldFilter:(BOOL)shouldFilter appliedProjections:(nullable NSArray<MPEventProjection *> *)appliedProjections;
- (nonnull instancetype)initWithConsentState:(nonnull MPConsentState *)state shouldFilter:(BOOL)shouldFilter;
Expand Down
6 changes: 4 additions & 2 deletions mParticle-Apple-SDK/Kits/MPKitFilter.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ - (instancetype)initWithFilter:(BOOL)shouldFilter filteredAttributes:(NSDictiona
}

- (instancetype)initWithEvent:(MPBaseEvent *)event shouldFilter:(BOOL)shouldFilter {
return [self initWithEvent:event shouldFilter:shouldFilter appliedProjections:nil];
return [self initWithEvent:event shouldFilter:shouldFilter appliedProjections:nil eventCopy:nil commerceEventCopy:nil];
}

- (instancetype)initWithEvent:(MPEvent *)event shouldFilter:(BOOL)shouldFilter appliedProjections:(NSArray<MPEventProjection *> *)appliedProjections {
- (instancetype)initWithEvent:(MPEvent *)event shouldFilter:(BOOL)shouldFilter appliedProjections:(NSArray<MPEventProjection *> *)appliedProjections eventCopy:(MPEvent *)eventCopy commerceEventCopy:(MPCommerceEvent *)commerceEventCopy{
self = [self initWithFilter:shouldFilter filteredAttributes:event.customAttributes];
if (!self) {
return nil;
Expand All @@ -39,6 +39,8 @@ - (instancetype)initWithEvent:(MPEvent *)event shouldFilter:(BOOL)shouldFilter a
_originalEvent = event;
_forwardEvent = event;
_appliedProjections = appliedProjections;
_originalEventCopy = eventCopy;
_originalCommerceEventCopy = commerceEventCopy;

return self;
}
Expand Down