Send events

This article describes how to send events from an application to Google Analytics using the Google Analytics for Firebase SDK. To learn how to send events from your websites, see Send events. To learn more about events, see About events.

Before you begin

Make sure that you've set up your project and can access Analytics as described in Get Started with Google Analytics.

Send events

After you configure the FirebaseApp instance, you can begin to send events with the logEvent() method. This method is equivalent to using the event command in gtag.js.

You can find implementation details for suggested event types in the following locations:

The following example demonstrates how to send a recommended event called select_content:

Swift

Analytics.logEvent(AnalyticsEventSelectContent, parameters: [
  AnalyticsParameterItemID: "id-\(title!)",
  AnalyticsParameterItemName: title!,
  AnalyticsParameterContentType: "cont",
])

Objective-C

[FIRAnalytics logEventWithName:kFIREventSelectContent
                    parameters:@{
                                 kFIRParameterItemID:[NSString stringWithFormat:@"id-%@", self.title],
                                 kFIRParameterItemName:self.title,
                                 kFIRParameterContentType:@"image"
                                 }];

In addition to the prescribed parameters, you can add the following parameters to any event:

  • Custom parameters: Custom parameters can be used as dimensions and metrics in Analytics reports. You can use custom dimensions for non-numerical event parameter data and custom metrics for any parameter data better represented numerically. Once you've logged a custom parameter using the SDK, register the dimension or metric to ensure those custom parameters appear in Analytics reports. Do this via: Analytics > Events > Manage Custom Definitions > Create Custom Dimensions.
  • kFIRParameterValue parameter: kFIRParameterValue is a general purpose parameter that is useful for accumulating a key metric that pertains to an event. Examples include revenue, distance, time, and points.

If your application has specific needs not covered by a suggested event type, you can log your own custom events as shown in this example:

Swift

Analytics.logEvent("share_image", parameters: [
  "name": name as NSObject,
  "full_text": text as NSObject,
])

Objective-C

 [FIRAnalytics logEventWithName:@"share_image"
                    parameters:@{
                                 @"name": name,
                                 @"full_text": text
                                 }];

Set default event parameters

You can log parameters across events using setDefaultEventParameters. Default parameters are associated with all future events that are logged.

As with custom parameters, register the default event parameters to ensure those custom parameters appear in Analytics reports.

Swift

Analytics.setDefaultEventParameters([ "level_name": "Caverns01", "level_difficulty": 4 ])

Objective-C

[FIRAnalytics setDefaultEventParameters:
  @{
  @"level_name": "Caverns01",
  @"level_difficulty": @(4)
}];

If a parameter is specified in the logEvent() method, that value is used instead of the default.

To clear a default parameter, call the setDefaultEventParameters method with the parameter set to nil.

View events in the Xcode debug console

You can enable verbose logging to monitor logging of events by the SDK to help verify that events are being logged properly. This includes both automatically and manually logged events.

You can enable verbose logging as follows:

  1. In Xcode, select Product > Scheme > Edit scheme...
  2. Select Run from the left menu.
  3. Select the Arguments tab.
  4. In the Arguments Passed On Launch section, add -FIRAnalyticsDebugEnabled.

The next time you run your app, your events will display in the Xcode debug console, helping you immediately verify that events are being sent.

View events in the dashboard

You can view aggregated statistics about your events in the Firebase console dashboards. These dashboards update periodically throughout the day. For immediate testing, use the logcat output as described in the previous section.

You can access this data from the Events dashboard in the Firebase console. This dashboard shows the event reports that are automatically created for each distinct type of event logged by your app.