dusanmiha

GA4 - iOS - Swift - purchase

Oct 11th, 2021 (edited)
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.31 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 purchase by logging a purchase event with one or more items defined with the relevant fields.
  4.  
  5. // A pair of jeggings (product1)
  6. var jeggings: [String: Any] = [
  7.   AnalyticsParameterItemID: "SKU_123",                      //Item ID (context-specific) (NSString).
  8.   AnalyticsParameterItemName: "jeggings",                   //Item Name (context-specific) (NSString).
  9.   AnalyticsParameterItemCategory: "pants",                  //Item category (context-specific) (NSString).
  10.   AnalyticsParameterItemCategory2: "sub-cat1",              //Item sub-category (context-specific) (NSString).
  11.   AnalyticsParameterItemCategory3: "sub-cat2",              //Item sub-category (context-specific) (NSString).
  12.   AnalyticsParameterItemCategory4: "sub-cat3",              //Item sub-category (context-specific) (NSString).
  13.   AnalyticsParameterItemCategory5: "sub-cat4",              //Item sub-category (context-specific) (NSString).
  14.   AnalyticsParameterItemVariant: "black",                   //Item variant (NSString).
  15.   AnalyticsParameterItemBrand: "Google",                    //Item brand(NSString).
  16.   AnalyticsParameterPrice: 9.99,                            //Product price (double as NSNumber).
  17. ]
  18. // Prepare purchase params
  19. var purchaseParams: [String: Any] = [
  20.   AnalyticsParameterTransactionID: "T12345",                //The unique identifier of a transaction (NSString).
  21.   AnalyticsParameterAffiliation: "Google Store",            //Optional. A product affiliation to designate a supplying company or brick and mortar store location (NSString).                      
  22.   AnalyticsParameterCurrency: "USD",                        //Currency of the purchase or items associated with the event, in 3-letter ISO_4217 format (NSString).
  23.   AnalyticsParameterValue: 14.98,                           //Value should be specified as signed 64-bit integer or double as NSNumber.
  24.   AnalyticsParameterTax: 2.58,                              //Tax cost associated with a transaction (double as NSNumber).
  25.  
  26.  
  27.   AnalyticsParameterShipping: 5.34,                         //Shipping cost associated with a transaction (double as NSNumber).
  28.  
  29.  
  30.   AnalyticsParameterCoupon: "SUMMER_FUN"
  31. ]
  32.  
  33. // Add items
  34. purchaseParams[AnalyticsParameterItems] = [jeggings]
  35.  
  36. // Log purchase event
  37. Analytics.logEvent(AnalyticsEventPurchase, parameters: purchaseParams)
Advertisement
Add Comment
Please, Sign In to add comment