Daira Bezzato
Daira Bezzato

Reputation: 143

How to log a custom Event in Facebook Analytics SDK 2020 for swift Documentation is deprecated

UPDATE

The problem is that facebook documentation for swift is outdated so to solve this you will have to log your custom event like this:

func logMyEvent(name : String, value : String) {
    let params : [String: Any] = ["myParamName" : "myParamValue"]
    let eventName: AppEvents.Name = AppEvents.Name(rawValue: "myEventName")
    AppEvents.logEvent(eventName, parameters: params)
}

IMPORTANT!

Take into account that facebook will log your event in its console about 20 minutes after you called it. So do not stress if the data is not there, just wait (I'm talking from experience hahaha). If you have any doubts don't hesitate to contact me, maybe I can help :D


I'm integrating Swift FacebookCore SDK so I can use Facebook Analytics! The problem is that facebooks official documentation DOES NOT WORK! It seems that they haven`t updated the code so I can not get the real code to log my own customized Event!

This is the code that Facebook gives you!

 * For more details, please take a look at:
 * developers.facebook.com/docs/swift/appevents
 */
func logMyEventEvent(name : String, value : String) {
    let params : AppEvent.ParametersDictionary = [
      "name" : name,
      "value" : value
      ]
    let event = AppEvent(name: "myEvent", parameters: params)
    AppEventsLogger.log(event)
}

Got it from here: https://developers.facebook.com/docs/app-events/getting-started-app-events-ios in the Manually Log Events section.

But AppEvent NO LONGER EXISTS.

Searching the web I found out that is because Facebook renamed it to AppEvents

IMAGE WERE I FOUND IT Here is the link to the GitHub poll. https://github.com/facebookarchive/facebook-swift-sdk/issues/433

But this still does not solve my issue, because I can not log a custom event.

Has anyone solved the same problem without going to the previous version?

Thank you very much!

Upvotes: 14

Views: 9298

Answers (5)

Narendra Prajapati
Narendra Prajapati

Reputation: 79

For Custom Event you can use

import FBSDKCoreKit

AppEvents.shared.logEvent(AppEvents.Name(rawValue: "AppEventName"), parameters: [AppEvents.ParameterName.init("Key"):"Value"])

Upvotes: 3

Hafiz Anser
Hafiz Anser

Reputation: 581

If you want by default functions:

import FBSDKCoreKit

  1. AppEvents.shared.logEvent(.subscribe)

Upvotes: 0

Osama Remlawi
Osama Remlawi

Reputation: 2990

Try this:

1- Declare globally an enum with all required events:

enum FACEBOOK_EVENTS: String {
    case appOpened = "MyApp iOS is opened"
    case appClosed = "MyApp iOS is terminated"
    case checkoutClicked = "Checkout in iOS is clicked"
    case placeOrderClicked = "Place order in iOS is clicked"
}

2- Second, register manually the event like this:

AppEvents.logEvent(AppEvents.Name.init(rawValue: GlobalClass.FACEBOOK_EVENTS.checkoutClicked.rawValue))

AppEvents.logEvent(AppEvents.Name.init(rawValue: GlobalClass.FACEBOOK_EVENTS.appOpened.rawValue))

...

Upvotes: 0

Jkrist
Jkrist

Reputation: 817

import FBSDKCoreKit

AppEvents.logEvent(AppEvents.Name.(*FBSDKAppEventName), parameters: ["*FBEvent": <Any>])

*FBSDKAppEventName based on facebook, so choose one out of these: Facebook Standart Events

*FBEvent - Based on chosen FBSDKAppEventName there might be, and might not be parameter. Choose one from Parameters and set as String.

Example:

AppEvents.logEvent(AppEvents.Name.achievedLevel, parameters: [AppEvents.Parameters.achieved: "5"])

Example track without the parameters:

AppEvents.logEvent(AppEvents.Name.submitApplication)

Upvotes: 1

Drew
Drew

Reputation: 976

It's hard to believe, but I couldn't find any documentation on this either.

However, I worked out the following code from looking though the FBSDKCoreKit source. I haven't tested it yet, but I am posting it here just in case I meet with a sudden unexpected death in the next few minutes.

import FBSDKCoreKit

let name = "myEvent"
let parameters: [String: Any] = [
    "myParameter": "myParameterValue"
]

let event = AppEvents.Name(name)
AppEvents.logEvent(event, parameters: parameters)

Upvotes: 11

Related Questions