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

feat: forward product custom attributes #30

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
80 changes: 33 additions & 47 deletions mParticle-Google-Analytics-Firebase-GA4/MPKitFirebaseGA4Analytics.m
Original file line number Diff line number Diff line change
Expand Up @@ -506,54 +506,9 @@ - (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent para
return parameters;
}

- (NSDictionary<NSString *, id> *)getParameterForImpression:(NSString *)impressionKey commerceEvent:(MPCommerceEvent *)commerceEvent products:(NSSet<MPProduct *> *)products {
NSMutableDictionary<NSString *, id> *parameters = [[self standardizeValues:commerceEvent.customAttributes forEvent:YES] mutableCopy];

[parameters setObject:impressionKey forKey:kFIRParameterItemListID];
[parameters setObject:impressionKey forKey:kFIRParameterItemListName];

if (products.count > 0) {
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
for (MPProduct *product in products) {
NSMutableDictionary<NSString *, id> *productParameters = [[NSMutableDictionary alloc] init];

if (product.quantity) {
[productParameters setObject:product.quantity forKey:kFIRParameterQuantity];
}
if (product.sku) {
[productParameters setObject:product.sku forKey:kFIRParameterItemID];
}
if (product.name) {
[productParameters setObject:product.name forKey:kFIRParameterItemName];
}
if (product.category) {
[productParameters setObject:product.category forKey:kFIRParameterItemCategory];
}
if (product.brand) {
[productParameters setObject:product.brand forKey:kFIRParameterItemBrand];
}
if (product.price) {
[productParameters setObject:product.price forKey:kFIRParameterPrice];
}

[self limitDictionary:productParameters maxCount:FIR_MAX_ITEM_PARAMETERS];
[itemArray addObject:productParameters];
}

if (itemArray.count > 0) {
[parameters setObject:itemArray forKey:kFIRParameterItems];
}
}

[self limitDictionary:parameters maxCount:FIR_MAX_EVENT_PARAMETERS_PROPERTIES];
return parameters;
}

- (NSDictionary<NSString *, id> *)getParameterForCommerceEvent:(MPCommerceEvent *)commerceEvent {
NSMutableDictionary<NSString *, id> *parameters = [[self standardizeValues:commerceEvent.customAttributes forEvent:YES] mutableCopy];

- (NSMutableArray *)getParametersForProducts:(id)products {
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
for (MPProduct *product in commerceEvent.products) {
for (MPProduct *product in products) {
NSMutableDictionary<NSString *, id> *productParameters = [[NSMutableDictionary alloc] init];

if (product.quantity) {
Expand All @@ -574,11 +529,42 @@ - (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent para
if (product.price) {
[productParameters setObject:product.price forKey:kFIRParameterPrice];
}
if (product.userDefinedAttributes) {
for (NSString *productCustomAttribute in product.userDefinedAttributes) {
[productParameters setObject:product.userDefinedAttributes[productCustomAttribute] forKey:productCustomAttribute];
}
}

[self limitDictionary:productParameters maxCount:FIR_MAX_ITEM_PARAMETERS];
[itemArray addObject:productParameters];
}

return itemArray;
}

- (NSDictionary<NSString *, id> *)getParameterForImpression:(NSString *)impressionKey commerceEvent:(MPCommerceEvent *)commerceEvent products:(NSSet<MPProduct *> *)products {
NSMutableDictionary<NSString *, id> *parameters = [[self standardizeValues:commerceEvent.customAttributes forEvent:YES] mutableCopy];

[parameters setObject:impressionKey forKey:kFIRParameterItemListID];
[parameters setObject:impressionKey forKey:kFIRParameterItemListName];

if (products.count > 0) {
NSMutableArray *itemArray = [self getParametersForProducts:products];

if (itemArray.count > 0) {
[parameters setObject:itemArray forKey:kFIRParameterItems];
}
}

[self limitDictionary:parameters maxCount:FIR_MAX_EVENT_PARAMETERS_PROPERTIES];
return parameters;
}

- (NSDictionary<NSString *, id> *)getParameterForCommerceEvent:(MPCommerceEvent *)commerceEvent {
NSMutableDictionary<NSString *, id> *parameters = [[self standardizeValues:commerceEvent.customAttributes forEvent:YES] mutableCopy];

NSMutableArray *itemArray = [self getParametersForProducts:commerceEvent.products];

if (itemArray.count > 0) {
[parameters setObject:itemArray forKey:kFIRParameterItems];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ - (NSString *)standardizeNameOrKey:(NSString *)nameOrKey forEvent:(BOOL)forEvent
- (NSString *)standardizeValue:(id)value forEvent:(BOOL)forEvent;
- (NSString *)getEventNameForCommerceEvent:(MPCommerceEvent *)commerceEvent parameters:(NSDictionary<NSString *, id> *)parameters;
- (NSDictionary<NSString *, id> *)getParameterForCommerceEvent:(MPCommerceEvent *)commerceEvent;
- (NSMutableArray *)getParametersForProducts:(id)products;
@end

@interface mParticle_Firebase_AnalyticsTests : XCTestCase
Expand Down Expand Up @@ -289,6 +290,26 @@ - (void)testSanitizationMax {
XCTAssertTrue(execStatus.success);
}

- (void)testProductParameters {
MPKitFirebaseGA4Analytics *exampleKit = [[MPKitFirebaseGA4Analytics alloc] init];
[exampleKit didFinishLaunchingWithConfiguration:@{}];

MPProduct *product = [[MPProduct alloc] initWithName:@"expensivePotato" sku:@"SKU123" quantity:@1 price:@40.0];
NSMutableDictionary<NSString *, id> *testProductCustomAttributes = [[@{@"productCustomAttribute": @"potato", @"store": @"Target"} mutableCopy] mutableCopy];
product.brand = @"LV";
product.category = @"vegetable";
product.userDefinedAttributes = testProductCustomAttributes;

MPCommerceEvent *event = [[MPCommerceEvent alloc] initWithImpressionName:@"suggested products list" product:product];
NSSet<MPProduct *> *impressionProducts = event.impressions[@"suggested products list"];

NSArray *itemsArray = [exampleKit getParametersForProducts:impressionProducts];
id item = itemsArray[0];

// The item inside itemsArray should include 8 parameters in total including the 2 product custom attributes
XCTAssertEqual([item count], 8);
}

- (void)testCommerceEventCheckoutOptions {
MPKitFirebaseGA4Analytics *exampleKit = [[MPKitFirebaseGA4Analytics alloc] init];
[exampleKit didFinishLaunchingWithConfiguration:@{}];
Expand Down
Loading