Recently, I've implemented Firebase Analytics AnalyticsEventBeginCheckout event.
But it seems AnalyticsParameterItems not be sent to Firebase.
My code :
var jeggings: [String: Any] = [
AnalyticsParameterItemID: "SKU_123",
AnalyticsParameterItemName: "jeggings",
AnalyticsParameterItemCategory: "pants",
AnalyticsParameterItemVariant: "black",
AnalyticsParameterItemBrand: "Google",
AnalyticsParameterPrice: 9.99,
]
// A pair of boots
var boots: [String: Any] = [
AnalyticsParameterItemID: "SKU_456",
AnalyticsParameterItemName: "boots",
AnalyticsParameterItemCategory: "shoes",
AnalyticsParameterItemVariant: "brown",
AnalyticsParameterItemBrand: "Google",
AnalyticsParameterPrice: 24.99,
]
var checkoutParams: [String: Any] = [
AnalyticsParameterCurrency: "USD",
AnalyticsParameterValue: 14.98,
AnalyticsParameterCoupon: "SUMMER_FUN"
];
checkoutParams[AnalyticsParameterItems] = [jeggings, boots]
// Log checkout event
Analytics.logEvent(AnalyticsEventBeginCheckout, parameters: checkoutParams)
This code is the tutorial code from Firebase.
It seems AnalyticsParameterItems not been send, but if I send item_id and item_name, these 2 fields appear on my event datas
Any solution ?
Related
I need to log items in carts but I can't figure out how to do that. In Swift it is not allowed to use append() function, so I tried to create a dictionary inside an array but with this method it does not work properly, I got an error in Firebase Debugview. Here is my codes: (Swift, iOS)
func logViewCart(items: [CartItem]){
var itemList : [[String : Any]] = []
for item in items{
var itemParams : [String : Any] = [
"item_id": item.id,
"item_name": item.product?.name,
"item_category": item.product.category,
"price": item.price
]
itemList.append(itemParams)
}
var itemTest : [String : Any] = [:]
for i in itemList {
itemTest[AnalyticsParameterItems] = [i]
}
Analytics.logEvent("view_cart", parameters: [
"items": [itemTest]
])
}
Thanks in advance
There's several ways for the error you are getting, you can start by typing the log message or the behaviour.
I'll go first for the basics on login an event, make sure you have this property set -FIRAnalyticsDebugEnabled on your scheme like so
With that should popUp like in 3~6 seconds on the debug viewer, then make sure you have the parameters right.
I found the solution. Instead of declaring a dictionary inside an array, declaring another array variable with [Any] type solve the problem.
var itemList : [Any] = []
for item in items{
let variantIndex = item.product?.attributes?[1].listValueLabel?.count ?? 1
var itemParams : [String : Any] = [
"item_id": item.itemID,
"item_name": item.product?.name
]
itemList.append(itemParams)
}
Analytics.logEvent("view_cart", parameters: [
AnalyticsParameterItems: itemList
])
I have to post json object.
One important thing, I have to post array of single value.
look at below code.
var pagingOption: [String: Any] = ["page": page,
"rowsPerPage": 20,
"sort": ["id": sortOption.rawValue,
"direction": "DESC"],
"searchStatues": ["accept"]]
searchStatuses is must array. But If I send this code, searchStatuses is not posted array.
If I add more value like this
var pagingOption: [String: Any] = ["page": page,
"rowsPerPage": 20,
"sort": ["id": sortOption.rawValue,
"direction": "DESC"],
"searchStatues": ["accept", "asdf", "asdf"]]
Then delivered array.
If I have only single value, How can I do that??
You can do it this way.
let searchValues: [String] = []
searchValues = ["accept", "asdf", "asdf"]
var pagingOption: [String: Any] = ["page": page,
"rowsPerPage": 20,
"sort": ["id": sortOption.rawValue,
"direction": "DESC"],
"searchStatues": searchValues]
I have to create request body for calling api which have nested dictionary formate to pass parameter. I am unable to create parameter
parameter should be in below format
let parameters: [[String: Any]] = [
"contributionIds":[
"ms.vss-tfs-web.project-members-data-provider-verticals"
],
"dataProviderContext": [
"properties": [
"sourcePage": [
"url":"https://xxx.visualstudio.com/abc%20abc",
"routeId":"ms.vss-tfs-web.project-overview-route",
"routeValues": [
"project":"abc",
"controller":"Apps",
"action":"ContributedHub",
"serviceHost":"2e1bc96a-9bac-4e6e-9e33-eb460123a138 (xxxxx)"
]
]
]
]
]
Above data need to post to api for get response from API
Getting an error
Contextual type '[[String : Any]]' cannot be used with dictionary literal
remove type notation [[String: Any]] because you are specifying wrong notation based on your structure type notation is [String: Any]. if you will not define type notation then that is fine as well.
Try Below Solution,
let parameters: [String: Any] = [
"contributionIds":[
"ms.vss-tfs-web.project-members-data-provider-verticals"
],
"dataProviderContext": [
"properties": [
"sourcePage": [
"url":"https://xxx.visualstudio.com/VSO%20Client",
"routeId":"ms.vss-tfs-web.project-overview-route",
"routeValues": [
"project":"VSO Client",
"controller":"Apps",
"action":"ContributedHub",
"serviceHost":"2e1bc96a-9bac-4e6e-9e33-eb460123a138 (xxxx)"
]
]
]
]
]
OR
let parameters = [
"contributionIds":[
"ms.vss-tfs-web.project-members-data-provider-verticals"
],
"dataProviderContext": [
"properties": [
"sourcePage": [
"url":"https://xxx.visualstudio.com/VSO%20Client",
"routeId":"ms.vss-tfs-web.project-overview-route",
"routeValues": [
"project":"VSO Client",
"controller":"Apps",
"action":"ContributedHub",
"serviceHost":"2e1bc96a-9bac-4e6e-9e33-eb460123a138 (xxxx)"
]
]
]
]
] as [String : Any]
I have a POST API in which I send multiple parameters, one of those parameters has to be an array of dictionaries.
let arr = [
[
"id" : "1",
"price" : "10"
],
[
"id" : "2",
"price" : "20"
]
]
let params : Parameters = [
"param1" : "anyvalue1",
"param2" : "anyvalue2",
"param3" : arr,
]
When I use these parameters in Alamofire Request and hit the API, print(res.result.value) always returns unknown. Can anyone help me with this. Following is the way of requesting the API
Alamofire.request(url, method:.post, parameters: params).responseJSON{(res) in
print(res.result.value) //always shows 'unknown' as output
}
Try to make params a Dic of [String :Any ] like that :
let params : [String:Any] = [
"param1" : "anyvalue1",
"param2" : "anyvalue2",
"param3" : arr,
]
I'm having trouble creating a specific structure in JSON with Swift. I use Swifty JSON for parsing but I can't figure out how to create one.
I have this array which is filled by Id's and quantity Ints of products in a shopping basket . I need to get the array into my JSON but I don't know how.
If you could help me with this I would be very glad :)
var productArray = Array<(id: Int,quantity: Int)>()
let jsonObject: [String: AnyObject] = [
"order": 1,
"client" : 1,
"plats": [
for product in productArray
{
"id": product.id
"quantity": product.quantity
}
]
]
You can't just start looping through stuff while defining your dictionary. Here's another approach.
First, create your array:
var productArray = Array<(id: Int,quantity: Int)>()
Add some products (for testing):
productArray += [(123, 1000)]
productArray += [(456, 50)]
Map this array into a new array of dictionaries:
let productDictArray = productArray.map { (product) -> [String : Int] in
[
"id": product.id,
"quantity": product.quantity
]
}
Use the new mapped array in your JSON object:
let jsonObject: [String: AnyObject] = [
"order": 1,
"client" : 1,
"plats": productDictArray
]
You are not supposed to do any kind of looping/condition making block of codes while creating Array's or Dictionary. For that you need to execute that piece of code outside, create a variable and use it.
Do try this way.
var productArray = Array<(id: Int,quantity: Int)>()
var prods = [[String:Int]]()
for product in productArray
{
var eachDict = [String:Int]()
eachDict["id"] = product.id
eachDict["quantity"] = product.quantity
prods.append(eachDict)
}
let jsonObject: [String: AnyObject] = [
"order": 1,
"client" : 1,
"plats": prods
]