dusanmiha

GA4 - iOS - Swift - add_to_cart

Oct 8th, 2021 (edited)
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.12 KB | None | 0 0
  1. // Products can be instrumented as an array of items that can be added to prescribed ecommerce events.
  2.  
  3. //Measure a product being added to a cart by logging an add_to_cart event with the relevant products in an items array parameter.
  4.  
  5.  
  6. // A pair of jeggings (product1)
  7. var jeggings: [String: Any] = [
  8.   AnalyticsParameterItemID: "SKU_123",                      //Item ID (context-specific) (NSString).
  9.   AnalyticsParameterItemName: "jeggings",                   //Item Name (context-specific) (NSString).
  10.   AnalyticsParameterItemCategory: "pants",                  //Item category (context-specific) (NSString).
  11.   AnalyticsParameterItemCategory2: "sub-cat1",              //Item sub-category (context-specific) (NSString).
  12.   AnalyticsParameterItemCategory3: "sub-cat2",              //Item sub-category (context-specific) (NSString).
  13.   AnalyticsParameterItemCategory4: "sub-cat3",              //Item sub-category (context-specific) (NSString).
  14.   AnalyticsParameterItemCategory5: "sub-cat4",              //Item sub-category (context-specific) (NSString).
  15.   AnalyticsParameterItemVariant: "black",                   //Item variant (NSString).
  16.   AnalyticsParameterItemBrand: "Google",                    //Item brand (NSString).
  17.   AnalyticsParameterPrice: 9.99,                            //Product price (double as NSNumber).                          
  18. ]
  19.  
  20. // Specify order quantity
  21. jeggings[AnalyticsParameterQuantity] = 2                    //Product quantity (signed 64-bit integer as NSNumber).
  22.  
  23. // Prepare item detail params
  24. var itemDetails: [String: Any] = [
  25.   AnalyticsParameterCurrency: "USD",                        //Currency of the product or items associated with the event, in 3-letter ISO_4217 format (NSString).
  26.   AnalyticsParameterValue: 19.98                            //A context-specific numeric value which is accumulated automatically for each event type.
  27. ]
  28.  
  29. // Add items
  30. itemDetails[AnalyticsParameterItems] = [jeggings]           //The list of items involved in the e-commerce action. (NSArray).
  31.  
  32. // Log an event when product is added to cart
  33. Analytics.logEvent(AnalyticsEventAddToCart, parameters: itemDetails)
Advertisement
Add Comment
Please, Sign In to add comment