2

I know this a basic question, but I can't work out how to log a custom event with custom parameters using facebook analytics. For example, I would like to log the following:

  • song played
  • title
  • artist

Please see the below code (which does not work):

    let dict = AppEvent.ParametersDictionary(AppEventParameterName.Custom("title), AppEventParameterName.Custom("artist"))

    AppEvent.ViewedContent(contentType: "test", contentId: "test", extraParameters: dict)

I get the error message:

"Cannot invoke value of type 'ParametersDictionary.Type' (aka 'Dictionary.Type') with argument list '(AppEventParameterName, AppEventParameterName)"

I have also tried:

let dict = AppEvent.ParametersDictionary(AppEventParameterName.Custom("title"), "test") 

Any help would be greatly appreciated.

Mike Bailey
  • 29
  • 1
  • 8
  • Is XCode showing you an error message? If so what is it? – Chris Barker Nov 22 '16 at 02:02
  • Yep "Cannot invoke value of type 'ParametersDictionary.Type' (aka 'Dictionary.Type') with argument list '(AppEventParameterName, AppEventParameterName)'" – Mike Bailey Nov 22 '16 at 02:19
  • A dictionary is a set of (key, value) pairs. For ParametersDictionary, the key type is AppEventParameterName and the value type is AppEventParameterValueType. You're providing keys, but you're not providing values. I'm not too familiar with Swift but I think you need to do something like `AppEvent.ParametersDictionary([AppEventParameterName.Custom("title"): "test_title"])`, where `"test_title"` is the value. – Chris Barker Nov 22 '16 at 17:03
  • Unfortunately, that doesn't work. – Mike Bailey Nov 23 '16 at 00:53

2 Answers2

6

As mentioned in the documentation, the dictionary needs to be of type [AppEventParameterName : AppEventParameterValueType].

You can construct the dictionary like:

[.Custom["Song Played]: "Sunday Morning", .Custom["Artist"]: "Maroon 5"]

Then just input it into the AppEventsLogger log call like this:

AppEventsLogger.log("trackedNewSong", parameters: dictionary, valueToSum: nil, accessToken: nil), where "trackedNewSong" would be the event you are logging and the parameters you are attaching to it are embedded in the dictionary.

Hope this helps!

Daniel Hsu
  • 96
  • 4
1

Addition to Daniel's answer

For Swift 4:

let fbParams: [AppEventParameterName : AppEventParameterValueType] = [AppEventParameterName.init("id") : id]