Search code examples
iosswiftfirebasefirebase-analytics

How to send list of items in an event in Firebase Analytics - iOS


I have a ecommerce iOS application to send events to firebase. Currently we have a requirement to send the list of product details in single event. From below docs its mention that parameter type will string or number. We wont be able to send as an array. Is there any possibilities to send array of items in a single event?

https://firebase.google.com/docs/reference/swift/firebaseanalytics/api/reference/Classes/Analytics

parameters : The dictionary of event parameters. Passing nil indicates that the event has no parameters. Parameter names can be up to 40 characters long and must start with an alphabetic character and contain only alphanumeric characters and underscores. Only NSString and NSNumber (signed 64-bit integer and 64-bit floating-point number) parameter types are supported. NSString parameter values can be up to 100 characters long. The “firebase_”, “google_”, and “ga_” prefixes are reserved and should not be used for parameter names.


Solution

  • Follow the doc here: https://firebase.google.com/docs/analytics/measure-ecommerce#objective-c

    You products:

    // A pair of jeggings
    NSMutableDictionary *jeggings = [@{
      kFIRParameterItemID: @"SKU_123",
      kFIRParameterItemName: @"jeggings",
      kFIRParameterItemCategory: @"pants",
      kFIRParameterItemVariant: @"black",
      kFIRParameterItemBrand: @"Google",
      kFIRParameterPrice: @9.99,
    } mutableCopy];
    
    // A pair of boots
    NSMutableDictionary *boots = [@{
      kFIRParameterItemID: @"SKU_456",
      kFIRParameterItemName: @"boots",
      kFIRParameterItemCategory: @"shoes",
      kFIRParameterItemVariant: @"brown",
      kFIRParameterItemBrand: @"Google",
      kFIRParameterPrice: @24.99,
    } mutableCopy];
    
    // A pair of socks
    NSMutableDictionary *socks = [@{
      kFIRParameterItemID: @"SKU_789",
      kFIRParameterItemName: @"ankle_socks",
      kFIRParameterItemCategory: @"socks",
      kFIRParameterItemVariant: @"red",
      kFIRParameterItemBrand: @"Google",
      kFIRParameterPrice: @5.99,
    } mutableCopy];
    

    Logging the product list:

    // Add item indexes
    jeggings[kFIRParameterIndex] = @1;
    boots[kFIRParameterIndex] = @2;
    socks[kFIRParameterIndex] = @3;
    
    // Prepare ecommerce parameters
    NSMutableDictionary *itemList = [@{
      kFIRParameterItemListID: @"L001",
      kFIRParameterItemListName: @"Related products",
    } mutableCopy];
    
    // Add items array
    itemList[kFIRParameterItems] = @[jeggings, boots, socks];
    
    // Log view item list event
    [FIRAnalytics logEventWithName:kFIREventViewItemList parameters:itemList];