Skip to content
Merged
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
76 changes: 76 additions & 0 deletions ios/RNMParticle/RNMParticle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ @interface RNMParticle (DeviceConsent)
+ (NSDictionary *)consentStateToDictionary:(MPConsentState *)consentState;
@end

@interface RNMParticle (CommerceMapping)
- (void)applyCommerceEventMetadata:(MPCommerceEvent *)event fromDictionary:(NSDictionary *)dict;
- (void)addPromotionsFromDicts:(NSArray *)promotionDicts toCommerceEvent:(MPCommerceEvent *)event;
@end

// Forward declare so New Arch `logCommerceEvent` can use the same JS→native
// mappings as `RCTConvert (MPCommerceEvent)` (defined later in this file).
@interface RCTConvert (MPCommerceEvent)
+ (MPCommerceEventAction)MPCommerceEventAction:(id)json;
+ (MPPromotionAction)MPPromotionAction:(id)json;
+ (MPPromotion *)MPPromotion:(id)json;
+ (MPConsentState *)MPConsentState:(id)json;
@end

Expand Down Expand Up @@ -544,6 +550,21 @@ - (void)logCommerceEvent:(JS::NativeMParticle::CommerceEvent &)commerceEvent {
[RCTConvert MPPromotionAction:@(commerceEvent.promotionActionType().value())];
mpCommerceEvent.promotionContainer =
[[MPPromotionContainer alloc] initWithAction:promotionAction promotion:nil];

if (commerceEvent.promotions().has_value()) {
auto promotionsVector = commerceEvent.promotions().value();
NSMutableArray *promotionDicts = [[NSMutableArray alloc] init];
for (size_t i = 0; i < promotionsVector.size(); i++) {
auto promotionStruct = promotionsVector[i];
NSMutableDictionary *promotionDict = [[NSMutableDictionary alloc] init];
if (promotionStruct.id_()) promotionDict[@"id"] = promotionStruct.id_();
if (promotionStruct.name()) promotionDict[@"name"] = promotionStruct.name();
if (promotionStruct.creative()) promotionDict[@"creative"] = promotionStruct.creative();
if (promotionStruct.position()) promotionDict[@"position"] = promotionStruct.position();
[promotionDicts addObject:promotionDict];
}
[self addPromotionsFromDicts:promotionDicts toCommerceEvent:mpCommerceEvent];
}
}

if (commerceEvent.products().has_value()) {
Expand Down Expand Up @@ -636,6 +657,17 @@ - (void)logCommerceEvent:(JS::NativeMParticle::CommerceEvent &)commerceEvent {
RNMParticleEventAttributes((NSDictionary *)commerceEvent.customAttributes());
}

NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
if (commerceEvent.currency()) metadata[@"currency"] = commerceEvent.currency();
if (commerceEvent.checkoutOptions()) metadata[@"checkoutOptions"] = commerceEvent.checkoutOptions();
if (commerceEvent.productActionListName()) metadata[@"productActionListName"] = commerceEvent.productActionListName();
if (commerceEvent.productActionListSource()) metadata[@"productActionListSource"] = commerceEvent.productActionListSource();
if (commerceEvent.screenName()) metadata[@"screenName"] = commerceEvent.screenName();
if (commerceEvent.checkoutStep().has_value()) metadata[@"checkoutStep"] = @(commerceEvent.checkoutStep().value());
if (commerceEvent.nonInteractive().has_value()) metadata[@"nonInteractive"] = @(commerceEvent.nonInteractive().value());
if (commerceEvent.shouldUploadEvent().has_value()) metadata[@"shouldUploadEvent"] = @(commerceEvent.shouldUploadEvent().value());
[self applyCommerceEventMetadata:mpCommerceEvent fromDictionary:metadata];

[[MParticle sharedInstance] logEvent:mpCommerceEvent];
}

Expand Down Expand Up @@ -821,6 +853,50 @@ - (MPProduct *)createMPProductFromDict:(NSDictionary *)productDict {
return product;
}

- (void)applyCommerceEventMetadata:(MPCommerceEvent *)event fromDictionary:(NSDictionary *)dict {
if (event == nil || ![dict isKindOfClass:[NSDictionary class]]) {
return;
}

if (dict[@"checkoutOptions"] && dict[@"checkoutOptions"] != [NSNull null]) {
event.checkoutOptions = dict[@"checkoutOptions"];
}
if (dict[@"currency"] && dict[@"currency"] != [NSNull null]) {
event.currency = dict[@"currency"];
}
if (dict[@"productActionListName"] && dict[@"productActionListName"] != [NSNull null]) {
event.productListName = dict[@"productActionListName"];
}
if (dict[@"productActionListSource"] && dict[@"productActionListSource"] != [NSNull null]) {
event.productListSource = dict[@"productActionListSource"];
}
if (dict[@"screenName"] && dict[@"screenName"] != [NSNull null]) {
event.screenName = dict[@"screenName"];
}
if (dict[@"checkoutStep"] && dict[@"checkoutStep"] != [NSNull null]) {
event.checkoutStep = [dict[@"checkoutStep"] intValue];
}
if (dict[@"nonInteractive"] && dict[@"nonInteractive"] != [NSNull null]) {
event.nonInteractive = [dict[@"nonInteractive"] boolValue];
}
if (dict[@"shouldUploadEvent"] && dict[@"shouldUploadEvent"] != [NSNull null]) {
event.shouldUploadEvent = [dict[@"shouldUploadEvent"] boolValue];
}
}

- (void)addPromotionsFromDicts:(NSArray *)promotionDicts toCommerceEvent:(MPCommerceEvent *)event {
if (event.promotionContainer == nil || ![promotionDicts isKindOfClass:[NSArray class]]) {
return;
}

for (id promotionJSON in promotionDicts) {
MPPromotion *promotion = [RCTConvert MPPromotion:promotionJSON];
if (promotion) {
[event.promotionContainer addPromotion:promotion];
}
}
}

- (MPIdentityApiRequest *)MPIdentityApiRequestFromDict:(NSDictionary *)dict {
if ([dict isKindOfClass:MPIdentityApiRequest.class]) {
return (MPIdentityApiRequest*)dict;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ + (MPEvent *)MPEvent:(id)json;

@interface RNMParticle (ProductMappingTests)
- (MPProduct *)createMPProductFromDict:(NSDictionary *)productDict;
- (void)applyCommerceEventMetadata:(MPCommerceEvent *)event fromDictionary:(NSDictionary *)dict;
- (void)addPromotionsFromDicts:(NSArray *)promotionDicts toCommerceEvent:(MPCommerceEvent *)event;
@end

/**
Expand All @@ -32,8 +34,9 @@ - (MPProduct *)createMPProductFromDict:(NSDictionary *)productDict;
* JSON → `MPCommerceEvent` tests below exercise the same `+[RCTConvert MPCommerceEvent:]` pipeline
* used to assemble an `MPCommerceEvent` before `-[MParticle logCommerceEvent:]` (legacy bridge path),
* including `MPPromotionContainer:` wiring. That catches regressions such as casting JS ints in
* those helpers instead of calling the mappers. Product tests invoke the helper used by the New
* Architecture codegen struct path without requiring generated C++ values in this target.
* those helpers instead of calling the mappers. Product and commerce-metadata tests invoke the
* helpers used by the New Architecture codegen struct path without requiring generated C++ values
* in this target.
*/
@interface RCTConvertCommerceMappingTests : XCTestCase
@end
Expand Down Expand Up @@ -181,4 +184,82 @@ - (void)testMPCommerceEventFromJSON_promotionActionFlowsThroughMPPromotionContai
XCTAssertEqual(clickEvent.promotionContainer.action, MPPromotionActionClick);
}

- (void)testMPCommerceEventFromJSON_mapsCurrencyCheckoutStepAndCheckoutOptions
{
NSDictionary *json = @{
@"productActionType" : @(3), // Checkout in js/index.tsx
@"products" : @[ [self minimalProductJSON] ],
@"impressions" : @[],
@"currency" : @"USD",
@"checkoutStep" : @1,
@"checkoutOptions" : @"Visa",
@"productActionListName" : @"checkout-list",
@"productActionListSource" : @"app",
@"screenName" : @"Checkout",
@"nonInteractive" : @YES,
@"shouldUploadEvent" : @NO,
};

MPCommerceEvent *event = [RCTConvert MPCommerceEvent:json];
XCTAssertEqualObjects(event.currency, @"USD");
XCTAssertEqual(event.checkoutStep, 1);
XCTAssertEqualObjects(event.checkoutOptions, @"Visa");
XCTAssertEqualObjects(event.productListName, @"checkout-list");
XCTAssertEqualObjects(event.productListSource, @"app");
XCTAssertEqualObjects(event.screenName, @"Checkout");
XCTAssertTrue(event.nonInteractive);
XCTAssertFalse(event.shouldUploadEvent);
}

- (void)testApplyCommerceEventMetadata_copiesNativeCommerceFieldsForNewArchitecture
{
RNMParticle *module = [[RNMParticle alloc] init];
MPCommerceEvent *event = [[MPCommerceEvent alloc] initWithAction:MPCommerceEventActionCheckout];

[module applyCommerceEventMetadata:event
fromDictionary:@{
@"currency" : @"USD",
@"checkoutStep" : @1,
@"checkoutOptions" : @"Visa",
@"productActionListName" : @"checkout-list",
@"productActionListSource" : @"app",
@"screenName" : @"Checkout",
@"nonInteractive" : @YES,
@"shouldUploadEvent" : @NO,
}];

XCTAssertEqualObjects(event.currency, @"USD");
XCTAssertEqual(event.checkoutStep, 1);
XCTAssertEqualObjects(event.checkoutOptions, @"Visa");
XCTAssertEqualObjects(event.productListName, @"checkout-list");
XCTAssertEqualObjects(event.productListSource, @"app");
XCTAssertEqualObjects(event.screenName, @"Checkout");
XCTAssertTrue(event.nonInteractive);
XCTAssertFalse(event.shouldUploadEvent);
}

- (void)testAddPromotionsFromDicts_fillsPromotionContainerForNewArchitecture
{
RNMParticle *module = [[RNMParticle alloc] init];
MPCommerceEvent *event = [[MPCommerceEvent alloc] init];
event.promotionContainer = [[MPPromotionContainer alloc] initWithAction:MPPromotionActionView promotion:nil];

[module addPromotionsFromDicts:@[
@{
@"id" : @"promo-1",
@"name" : @"Sale",
@"creative" : @"banner",
@"position" : @"home-top",
}
]
toCommerceEvent:event];

XCTAssertEqual(event.promotionContainer.promotions.count, 1);
MPPromotion *promotion = event.promotionContainer.promotions.firstObject;
XCTAssertEqualObjects(promotion.promotionId, @"promo-1");
XCTAssertEqualObjects(promotion.name, @"Sale");
XCTAssertEqualObjects(promotion.creative, @"banner");
XCTAssertEqualObjects(promotion.position, @"home-top");
}

@end
Loading