イベントは、ユーザーの操作、システム イベント、エラーなど、アプリで起こっていることを把握するのに役立ちます。
Firebase 向け Google アナリティクスは、一部のイベントに関するログを自動で記録します。ログを受信するためのコードを追加する必要はありません。それ以外のデータをアプリで収集する場合は、最大 500 種類までのアナリティクス イベントをログに記録できます。アプリで記録できるログの量に制限はありません。イベント名では大文字と小文字が区別されます。そのため、大文字と小文字の使い方だけが異なっている同じ名前の 2 つのイベントは、2 つの別個のイベントとしてログに記録されます。
始める前に
プロジェクトを設定し、C++ 用 Firebase 向け Google アナリティクスの説明に従ってアナリティクスにアクセスできることを確認してください。
イベントをログに記録する
firebase::analytics モジュールを初期化すると、LogEvent() メソッドを使ってイベントをログに記録できるようになります。
アナリティクス SDK には、小売、e コマース、旅行、ゲームなどといったさまざまな種類のアプリでよく使われるイベントが推奨イベントとして多数定義されており、すぐに使うことができます。こうしたイベントの詳細と使い方については、Firebase ヘルプセンターのイベントとプロパティをご覧ください。
推奨イベントの実装方法の詳細については、次の記事をご覧ください。
- 推奨イベント:
Event定数のリストをご覧ください。 - 既定のパラメータ:
Parameters定数のリストをご覧ください。
次に示す例では、推奨イベントである SELECT_CONTENT イベントをログに記録する方法を説明します。
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]));
既定のパラメータのほかにも、あらゆるイベントに次のパラメータを追加できます。
カスタム パラメータ: カスタム パラメータはアナリティクス レポートには記載されませんが、ユーザーリストの定義の際にフィルタとして使用できます。定義したユーザーリストはあらゆるレポートに適用できます。アプリが BigQuery プロジェクトとリンクされている場合、カスタム パラメータは BigQuery にエクスポートされるデータにも含まれます。
VALUEパラメータ:VALUEはアナリティクス イベントに関連する主要な指標を蓄積するのに役立つ汎用のパラメータです。そのようなイベントにはたとえば収益、距離、時間、得点などがあります。
推奨されるアナリティクス イベントの種類では収集できないデータをアプリで収集したい場合は、次の例で示すように独自のカスタム アナリティクス イベントを記録することができます。
// 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 10s before logging a valid session.
analytics::SetMinimumSessionDuration(1000 * 10);
// App session times out after 30 minutes.
// If the app is placed in the background and returns to the foreground after
// the timeout is expired analytics will log a new session.
analytics::SetSessionTimeoutDuration(1000 * 60 * 30);
LogMessage("Get App Instance ID...");
auto future_result = analytics::GetAnalyticsInstanceId();
while (future_result.status() == firebase::kFutureStatusPending) {
if (ProcessEvents(1000)) break;
}
if (future_result.status() == firebase::kFutureStatusComplete) {
LogMessage("Analytics Instance ID %s", future_result.result()->c_str());
} else {
LogMessage("ERROR: Failed to fetch Analytics Instance ID %s (%d)",
future_result.error_message(), future_result.error());
}
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;
}
Android Studio のデバッグログでイベントを表示する
詳細ログを有効にして SDK によるイベントのログ記録を監視し、イベントが正しく記録されているかどうかを確認することができます。対象となるのは、自動で記録されるイベントと手動で記録されるイベントの両方です。
詳細ログを有効にするには、次の一連の adb コマンドを入力します。
adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC
このコマンドを入力すると Android Studio の logcat にイベントが表示され、イベントが送信されているかどうかを即座に確認することができます。
ダッシュボードでアナリティクス イベントを表示する
Firebase コンソールのダッシュボードでは、アナリティクス イベントに関する集約された統計情報を表示することができます。これらのダッシュボードは、一日を通して定期的に更新されます。即座にテストを行いたい場合は、前のセクションで説明した logcat の出力を使用してください。
Firebase コンソールでこのデータにアクセスするには、次のように操作します。
- Firebase コンソールでプロジェクトを開きます。
- メニューで [アナリティクス] を選択し、アナリティクスのレポート ダッシュボードを表示します。
[イベント] タブには、アプリによってログに記録されたアナリティクス イベントの種類ごとに自動的に作成されたイベント レポートが表示されます。アナリティクスのレポート ダッシュボードの詳細については、Firebase ヘルプセンターをご覧ください。

