Log Events

Events provide insight on what is happening in your app, such as user actions, system events, or errors.

Google Analytics for Firebase automatically logs some events for you; you don't need to add any code to receive them. If your app needs to collect additional data, you can log up to 500 different Analytics event types in your app. There is no limit on the total volume of events your app logs. Note that event names are case-sensitive and that logging two events whose names differ only in case will result in two distinct events.

Before you begin

If this is your first time adding Analytics to your app, complete the following steps:

Connect your app in the Firebase console

  1. Install the Firebase SDK for C++.
  2. In the Firebase console, add your app to your Firebase project.

Add Analytics to your app

In order to use the Analytics libraries in your C++ project, you'll need to add it to your build files, so that the appropriate libraries are linked.

Android

  1. Add the Firebase Java Component to the build.gradle file:

    compile 'com.google.firebase:firebase-analytics:10.2.6'

  2. Link the following libraries when you build your C++ component:

    libapp.a
    libanalytics.a
    

iOS

Before you can build for iOS, you'll need to add the Firebase frameworks to your Xcode project:

  1. Add the following dependancy to your Podfile:
    pod 'Firebase/Core'
    pod 'Firebase/Analytics'
  2. Run $pod install
  3. Add firebase.framework and firebase_analytics.framework to your xcode project.

Log events

After you have initialized the firebase::analytics module, you can use it to log events with the LogEvent() method.

To help you get started, the Analytics SDK defines a number of suggested events that are common among different types of apps, including retail and ecommerce, travel, and gaming apps. To learn more about these events and when to use them, browse the Events and properties articles in the Firebase Help Center.

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

  • Suggested events: see the list of Event constants.
  • Prescribed parameters: see the list of Parameters constants.

The following example demonstrates how to log a suggested SELECT_CONTENT Event:

  const analytics::Parameter kSelectContentParameters[] = {
    analytics::Parameter(analytics::kParameterItemID , id),
    analytics::Parameter(analytics::kParameterItemName, "name"),
    analytics::Parameter(analytics::kUserPropertySignUpMethod, "Google"),
    analytics::Parameter("favorite_food", mFavoriteFood),
    analytics::Parameter("user_id", mUserId),
  };
  analytics::LogEvent(
    analytics::kEventSelectContent, kSelectContentParameters,
    sizeof(kSelectContentParameters) / sizeof(kSelectContentParameters[0]));

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

  • Custom parameters: Custom parameters are not represented directly in your Analytics reports, but they can be used as filters in Audience definitions that can be applied to every report. Custom parameters are also included in data exported to BigQuery if your app is linked to a BigQuery project.

  • VALUE Parameter: VALUE is a general purpose parameter that is useful for accumulating a key metric that pertains to an Analytics Event. Examples include revenue, distance, time and points.

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

// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "firebase/analytics.h"
#include "firebase/analytics/event_names.h"
#include "firebase/analytics/parameter_names.h"
#include "firebase/analytics/user_property_names.h"
#include "firebase/app.h"

// Thin OS abstraction layer.
#include "main.h"  // NOLINT

// Execute all methods of the C++ Analytics API.
extern "C" int common_main(int argc, const char* argv[]) {
  namespace analytics = ::firebase::analytics;
  ::firebase::App* app;

  LogMessage("Initialize the Analytics library");
#if defined(__ANDROID__)
  app = ::firebase::App::Create(GetJniEnv(), GetActivity());
#else
  app = ::firebase::App::Create();
#endif  // defined(__ANDROID__)

  LogMessage("Created the firebase app %x",
             static_cast<int>(reinterpret_cast<intptr_t>(app)));
  analytics::Initialize(*app);
  LogMessage("Initialized the firebase analytics API");

  LogMessage("Enabling data collection.");
  analytics::SetAnalyticsCollectionEnabled(true);
  // App needs to be open at least 1s before logging a valid session.
  analytics::SetMinimumSessionDuration(1000);
  // App session times out after 5s.
  analytics::SetSessionTimeoutDuration(5000);

  LogMessage("Set user properties.");
  // Set the user's sign up method.
  analytics::SetUserProperty(analytics::kUserPropertySignUpMethod, "Google");
  // Set the user ID.
  analytics::SetUserId("uber_user_510");

  LogMessage("Set current screen.");
  // Set the user's current screen.
  analytics::SetCurrentScreen("Firebase Analytics C++ testapp", "testapp");

  // Log an event with no parameters.
  LogMessage("Log login event.");
  analytics::LogEvent(analytics::kEventLogin);

  // Log an event with a floating point parameter.
  LogMessage("Log progress event.");
  analytics::LogEvent("progress", "percent", 0.4f);

  // Log an event with an integer parameter.
  LogMessage("Log post score event.");
  analytics::LogEvent(analytics::kEventPostScore, analytics::kParameterScore,
                      42);

  // Log an event with a string parameter.
  LogMessage("Log group join event.");
  analytics::LogEvent(analytics::kEventJoinGroup, analytics::kParameterGroupID,
                      "spoon_welders");

  // Log an event with multiple parameters.
  LogMessage("Log level up event.");
  {
    const analytics::Parameter kLevelUpParameters[] = {
        analytics::Parameter(analytics::kParameterLevel, 5),
        analytics::Parameter(analytics::kParameterCharacter, "mrspoon"),
        analytics::Parameter("hit_accuracy", 3.14f),
    };
    analytics::LogEvent(
        analytics::kEventLevelUp, kLevelUpParameters,
        sizeof(kLevelUpParameters) / sizeof(kLevelUpParameters[0]));
  }

  LogMessage("Complete");

  // Wait until the user wants to quit the app.
  while (!ProcessEvents(1000)) {
  }

  analytics::Terminate();
  delete app;

  LogMessage("Shutdown");

  return 0;
}

View events in the Android Studio debug log

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 with a series of adb commands:

adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC

This command displays your events in the Android Studio logcat, helping you immediately verify that events are being sent.

View analytics events in the dashboard

You can view aggregrated statistics about your Analytics 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.

To access this data in the Firebase console:

  1. In the Firebase console, open your project.
  2. Select Analytics from the menu to view the Analytics reporting dashboard.

The Events tab shows the event reports that are automatically created for each distinct type of Analytics Event logged by your app. Read more about the Analytics reporting dashboard in the Firebase Help Center.

Send feedback about...

Need help? Visit our support page.