事件可以讓您深入了解您的應用中正在發生的事情,例如用戶操作、系統事件或錯誤。
Google Analytics 會自動為您記錄一些事件;您無需添加任何代碼即可接收它們。如果您的應用需要收集額外數據,您可以在您的應用中記錄多達 500 種不同的 Analytics 事件類型。您的應用記錄的事件總量沒有限制。請注意,事件名稱區分大小寫,記錄兩個名稱僅大小寫不同的事件將導致兩個不同的事件。
在你開始之前
確保您已設置項目並且可以訪問 Analytics,如開始使用 Analytics for C++中所述。
記錄事件
初始化firebase::analytics
模塊後,您可以使用它通過LogEvent()
方法記錄事件。
為幫助您入門,Analytics SDK 定義了一些建議事件,這些事件在不同類型的應用程序中很常見,包括零售和電子商務、旅遊和遊戲應用程序。要詳細了解這些事件以及何時使用它們,請瀏覽 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]));
除了規定的參數外,您還可以將以下參數添加到任何事件中:
自定義參數:自定義參數不會直接顯示在您的 Analytics 報告中,但它們可以用作可應用於每個報告的受眾定義中的過濾器。如果您的應用鏈接到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 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("Log current screen."); // Log the user's current screen. analytics::LogEvent(analytics::kEventScreenView, "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 控制台儀表板中查看有關您的 Analytics 事件的匯總統計信息。這些儀表板全天定期更新。對於即時測試,請使用上一節中所述的 logcat 輸出。
要在 Firebase 控制台中訪問此數據:
- 在Firebase 控制台中,打開您的項目。
- 從菜單中選擇Analytics以查看 Analytics 報告儀表板。
事件選項卡顯示為您的應用程序記錄的每種不同類型的分析事件自動創建的事件報告。在 Firebase 幫助中心閱讀有關Analytics 報告儀表板的更多信息。