'Firebase Analytics for iOS — custom parameters with predefined events (Swift)

I'm trying to implement Firebase Analytics for Swift iOS app. Could you please explain, if it's possible to pass custom parameters with predefined AnalyticsEvent like AnalyticsEventEcommercePurchase https://firebase.google.com/docs/reference/swift/firebaseanalytics/api/reference/Constants#analyticseventecommercepurchase?

For instanse, I'd love to add 'dicount_amount', 'delivery_type' to that event. Is it possible for AnalyticsEventEcommercePurchase?

Also, is it possible to add 'items' array with the parameters of each item in purchase? Is it possible to pass predefined parameters with custom events? For example, pass AnalyticsParameterItemId with my own event view_product?

Thank you so much. I'd appreciate any recommendations and examples.



Solution 1:[1]

You can. Simply pass them along with the predefined parameters, like this:

Analytics.logEvent(AnalyticsEventEcommercePurchase, parameters: [
  AnalyticsParameterCurrency: "aud",
  AnalyticsParameterValue: "999",
  "whatever_you_want": "foo"
])

You can also used predefined parameters in custom events as these are just strings.

Solution 2:[2]

Try this:

let parameter =["discount_amount": 5, "delivery_type" : COD] // put all your parameter here 

Analytics.logEvent(EventName, parameters: parameter) //replace EventName with your event name you want to log

is it possible to add 'items' array with the parameters of each item in purchase? Currently it's not possible as firebase only support Text or Number(Double, Int etc.) , further number can classified in different types

enter image description here

Solution 3:[3]

i create a new class to put all the Firebase Analytics functions

this is example

class ClientAnalytics
{

    static func purchaseEvent(itemAdded : String , value : Double , currency : String)
{
    Analytics.logEvent(AnalyticsEventEcommercePurchase, parameters:
    [
        AnalyticsParameterItemID   : itemAdded as NSObject,
        AnalyticsParameterValue    : value,
        AnalyticsParameterCurrency : currency
    ])
}

how to call

ClientAnalytics.purchaseEvent(itemAdded: ToolName, value: toolValue, currency: "USD")

the purpose of this functions is to log any purchase Event the user

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 William Taylor
Solution 2
Solution 3 Saad