Skip to content

Commit fe8f65f

Browse files
nag-roktNahuel Alan Grimaudo
andauthored
fix: Handle zero values appropriately for product position (#35)
* Fix product.position condition to handle zero values * Since NSUInteger is a primitive type, there's no concept of "null" * Add unit test for product.position = 0 --------- Co-authored-by: Nahuel Alan Grimaudo <[email protected]>
1 parent f7c50d1 commit fe8f65f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ - (NSMutableArray *)getParametersForProducts:(id)products {
536536
if (product.price) {
537537
[productParameters setObject:product.price forKey:kFIRParameterPrice];
538538
}
539-
if (product.position) {
539+
if (product.position >= 0) {
540540
id indexParameter = @(product.position);
541541
[productParameters setObject:indexParameter forKey:kFIRParameterIndex];
542542
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,27 @@ - (void)testProductParameters {
312312
XCTAssertEqual([item count], 9);
313313
}
314314

315+
- (void)testProductPositionEqualZero {
316+
MPKitFirebaseGA4Analytics *exampleKit = [[MPKitFirebaseGA4Analytics alloc] init];
317+
[exampleKit didFinishLaunchingWithConfiguration:@{}];
318+
319+
MPProduct *product = [[MPProduct alloc] initWithName:@"expensivePotato" sku:@"SKU123" quantity:@1 price:@40.0];
320+
NSMutableDictionary<NSString *, id> *testProductCustomAttributes = [[@{@"productCustomAttribute": @"potato", @"store": @"Target"} mutableCopy] mutableCopy];
321+
product.brand = @"LV";
322+
product.category = @"vegetable";
323+
product.position = 0;
324+
product.userDefinedAttributes = testProductCustomAttributes;
325+
326+
MPCommerceEvent *event = [[MPCommerceEvent alloc] initWithImpressionName:@"suggested products list" product:product];
327+
NSSet<MPProduct *> *impressionProducts = event.impressions[@"suggested products list"];
328+
329+
NSArray *itemsArray = [exampleKit getParametersForProducts:impressionProducts];
330+
id item = itemsArray[0];
331+
332+
// The item inside itemsArray should include 9 parameters in total including the 2 product custom attributes
333+
XCTAssertEqual([item count], 9);
334+
}
335+
315336
- (void)testCommerceEventCheckoutOptions {
316337
MPKitFirebaseGA4Analytics *exampleKit = [[MPKitFirebaseGA4Analytics alloc] init];
317338
[exampleKit didFinishLaunchingWithConfiguration:@{}];

0 commit comments

Comments
 (0)