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

5

14

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!

Piercing answered 29/4, 2020 at 17:4 Comment(1)
I tried to implement Facebook SDK to log events in the app. And there are a lot of FBSDKLog: (null) logs and no events in the events manager. Any ideas on how to fix it?Hydrosol
E
11

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)
Eximious answered 11/5, 2020 at 23:56 Comment(2)
Hello, yes, facebook documentation is deprecated, I solved it doing this: let params = [ "myParam" : myParamValue] let eventName: AppEvents.Name = AppEvents.Name(rawValue: "myCustomAnalitics") AppEvents.logEvent(eventName, parameters: params)Piercing
For me, it's working, but the only issue I am facing is I need to log in to my Facebook account on my device. I am not using Facebook login in the app, just using event tracking. but still, it works only when logged in to the Facebook app, is there any way that events can be seen without login into the Facebook app?Nagari
B
3

For Custom Event you can use

import FBSDKCoreKit

AppEvents.shared.logEvent(AppEvents.Name(rawValue: "AppEventName"), parameters: [AppEvents.ParameterName.init("Key"):"Value"])
Beatty answered 24/6, 2022 at 13:27 Comment(0)
C
1
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)
Convex answered 13/6, 2020 at 10:35 Comment(1)
Hi if i am integrate the facebook events all the integration done as per documents but my events not appears in eventsKeg
S
0

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))

...
Sequestered answered 30/11, 2021 at 10:6 Comment(0)
N
0

If you want by default functions:

import FBSDKCoreKit

  1. AppEvents.shared.logEvent(.subscribe)
Newcomen answered 2/6, 2022 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.