Skip to content

Commit 0429af8

Browse files
authored
feat: forward product custom attributes (#30)
1 parent 2b7eeff commit 0429af8

File tree

2 files changed

+54
-47
lines changed

2 files changed

+54
-47
lines changed

mParticle-Google-Analytics-Firebase-GA4/MPKitFirebaseGA4Analytics.m

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -506,54 +506,9 @@ - (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent para
506506
return parameters;
507507
}
508508

509-
- (NSDictionary<NSString *, id> *)getParameterForImpression:(NSString *)impressionKey commerceEvent:(MPCommerceEvent *)commerceEvent products:(NSSet<MPProduct *> *)products {
510-
NSMutableDictionary<NSString *, id> *parameters = [[self standardizeValues:commerceEvent.customAttributes forEvent:YES] mutableCopy];
511-
512-
[parameters setObject:impressionKey forKey:kFIRParameterItemListID];
513-
[parameters setObject:impressionKey forKey:kFIRParameterItemListName];
514-
515-
if (products.count > 0) {
516-
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
517-
for (MPProduct *product in products) {
518-
NSMutableDictionary<NSString *, id> *productParameters = [[NSMutableDictionary alloc] init];
519-
520-
if (product.quantity) {
521-
[productParameters setObject:product.quantity forKey:kFIRParameterQuantity];
522-
}
523-
if (product.sku) {
524-
[productParameters setObject:product.sku forKey:kFIRParameterItemID];
525-
}
526-
if (product.name) {
527-
[productParameters setObject:product.name forKey:kFIRParameterItemName];
528-
}
529-
if (product.category) {
530-
[productParameters setObject:product.category forKey:kFIRParameterItemCategory];
531-
}
532-
if (product.brand) {
533-
[productParameters setObject:product.brand forKey:kFIRParameterItemBrand];
534-
}
535-
if (product.price) {
536-
[productParameters setObject:product.price forKey:kFIRParameterPrice];
537-
}
538-
539-
[self limitDictionary:productParameters maxCount:FIR_MAX_ITEM_PARAMETERS];
540-
[itemArray addObject:productParameters];
541-
}
542-
543-
if (itemArray.count > 0) {
544-
[parameters setObject:itemArray forKey:kFIRParameterItems];
545-
}
546-
}
547-
548-
[self limitDictionary:parameters maxCount:FIR_MAX_EVENT_PARAMETERS_PROPERTIES];
549-
return parameters;
550-
}
551-
552-
- (NSDictionary<NSString *, id> *)getParameterForCommerceEvent:(MPCommerceEvent *)commerceEvent {
553-
NSMutableDictionary<NSString *, id> *parameters = [[self standardizeValues:commerceEvent.customAttributes forEvent:YES] mutableCopy];
554-
509+
- (NSMutableArray *)getParametersForProducts:(id)products {
555510
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
556-
for (MPProduct *product in commerceEvent.products) {
511+
for (MPProduct *product in products) {
557512
NSMutableDictionary<NSString *, id> *productParameters = [[NSMutableDictionary alloc] init];
558513

559514
if (product.quantity) {
@@ -574,11 +529,42 @@ - (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent para
574529
if (product.price) {
575530
[productParameters setObject:product.price forKey:kFIRParameterPrice];
576531
}
532+
if (product.userDefinedAttributes) {
533+
for (NSString *productCustomAttribute in product.userDefinedAttributes) {
534+
[productParameters setObject:product.userDefinedAttributes[productCustomAttribute] forKey:productCustomAttribute];
535+
}
536+
}
577537

578538
[self limitDictionary:productParameters maxCount:FIR_MAX_ITEM_PARAMETERS];
579539
[itemArray addObject:productParameters];
580540
}
581541

542+
return itemArray;
543+
}
544+
545+
- (NSDictionary<NSString *, id> *)getParameterForImpression:(NSString *)impressionKey commerceEvent:(MPCommerceEvent *)commerceEvent products:(NSSet<MPProduct *> *)products {
546+
NSMutableDictionary<NSString *, id> *parameters = [[self standardizeValues:commerceEvent.customAttributes forEvent:YES] mutableCopy];
547+
548+
[parameters setObject:impressionKey forKey:kFIRParameterItemListID];
549+
[parameters setObject:impressionKey forKey:kFIRParameterItemListName];
550+
551+
if (products.count > 0) {
552+
NSMutableArray *itemArray = [self getParametersForProducts:products];
553+
554+
if (itemArray.count > 0) {
555+
[parameters setObject:itemArray forKey:kFIRParameterItems];
556+
}
557+
}
558+
559+
[self limitDictionary:parameters maxCount:FIR_MAX_EVENT_PARAMETERS_PROPERTIES];
560+
return parameters;
561+
}
562+
563+
- (NSDictionary<NSString *, id> *)getParameterForCommerceEvent:(MPCommerceEvent *)commerceEvent {
564+
NSMutableDictionary<NSString *, id> *parameters = [[self standardizeValues:commerceEvent.customAttributes forEvent:YES] mutableCopy];
565+
566+
NSMutableArray *itemArray = [self getParametersForProducts:commerceEvent.products];
567+
582568
if (itemArray.count > 0) {
583569
[parameters setObject:itemArray forKey:kFIRParameterItems];
584570
}

mParticle-Google-Analytics-Firebase-GA4Tests/MPKitFirebaseGA4AnalyticsTests.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ - (NSString *)standardizeNameOrKey:(NSString *)nameOrKey forEvent:(BOOL)forEvent
1212
- (NSString *)standardizeValue:(id)value forEvent:(BOOL)forEvent;
1313
- (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent parameters:(NSDictionary<NSString *, id> *)parameters;
1414
- (NSDictionary<NSString *, id> *)getParameterForCommerceEvent:(MPCommerceEvent *)commerceEvent;
15+
- (NSMutableArray *)getParametersForProducts:(id)products;
1516
@end
1617

1718
@interface mParticle_Firebase_AnalyticsTests : XCTestCase
@@ -289,6 +290,26 @@ - (void)testSanitizationMax {
289290
XCTAssertTrue(execStatus.success);
290291
}
291292

293+
- (void)testProductParameters {
294+
MPKitFirebaseGA4Analytics *exampleKit = [[MPKitFirebaseGA4Analytics alloc] init];
295+
[exampleKit didFinishLaunchingWithConfiguration:@{}];
296+
297+
MPProduct *product = [[MPProduct alloc] initWithName:@"expensivePotato" sku:@"SKU123" quantity:@1 price:@40.0];
298+
NSMutableDictionary<NSString *, id> *testProductCustomAttributes = [[@{@"productCustomAttribute": @"potato", @"store": @"Target"} mutableCopy] mutableCopy];
299+
product.brand = @"LV";
300+
product.category = @"vegetable";
301+
product.userDefinedAttributes = testProductCustomAttributes;
302+
303+
MPCommerceEvent *event = [[MPCommerceEvent alloc] initWithImpressionName:@"suggested products list" product:product];
304+
NSSet<MPProduct *> *impressionProducts = event.impressions[@"suggested products list"];
305+
306+
NSArray *itemsArray = [exampleKit getParametersForProducts:impressionProducts];
307+
id item = itemsArray[0];
308+
309+
// The item inside itemsArray should include 8 parameters in total including the 2 product custom attributes
310+
XCTAssertEqual([item count], 8);
311+
}
312+
292313
- (void)testCommerceEventCheckoutOptions {
293314
MPKitFirebaseGA4Analytics *exampleKit = [[MPKitFirebaseGA4Analytics alloc] init];
294315
[exampleKit didFinishLaunchingWithConfiguration:@{}];

0 commit comments

Comments
 (0)