13

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!

Daira Bezzato
  • 133
  • 1
  • 6
  • 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? – Mukul Kumar Mar 23 '22 at 10:15

5 Answers5

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)
Drew
  • 926
  • 1
  • 9
  • 13
  • 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)` – Daira Bezzato May 20 '20 at 00:03
  • 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? – Sagar Mar 25 '22 at 14:59
3

For Custom Event you can use

import FBSDKCoreKit

AppEvents.shared.logEvent(AppEvents.Name(rawValue: "AppEventName"), parameters: [AppEvents.ParameterName.init("Key"):"Value"])
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
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)
Jkrist
  • 748
  • 1
  • 6
  • 24
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))

...
Osama Remlawi
  • 2,356
  • 20
  • 21
0

If you want by default functions:

import FBSDKCoreKit

  1. AppEvents.shared.logEvent(.subscribe)
HafizAnser
  • 553
  • 6
  • 10