เหตุการณ์ในบันทึก

เหตุการณ์ช่วยให้คุณมีข้อมูลเชิงลึกเกี่ยวกับสิ่งที่เกิดขึ้นภายในแอป เช่น การกระทำของผู้ใช้ เหตุการณ์ของระบบ หรือข้อผิดพลาด

Google Analytics จะบันทึกเหตุการณ์บางอย่างโดยอัตโนมัติ คุณจึงไม่จำเป็นต้องเพิ่มโค้ดใดๆ เพื่อรับเหตุการณ์เหล่านั้น หากแอปต้องรวบรวม ข้อมูลเพิ่มเติม คุณสามารถบันทึกAnalyticsประเภทเหตุการณ์ ที่แตกต่างกันได้สูงสุด 500 รายการในแอป โดยไม่มีการจำกัดปริมาณรวมของเหตุการณ์ที่แอปบันทึก โปรดทราบว่าชื่อเหตุการณ์จะคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ และการบันทึกเหตุการณ์ 2 รายการที่ชื่อต่างกันตรงตัวพิมพ์เท่านั้นจะทำให้ระบบบันทึกเหตุการณ์ 2 รายการแยกกัน

ก่อนเริ่มต้น

ตรวจสอบว่าคุณได้ตั้งค่าโปรเจ็กต์และเข้าถึง Analytics ได้ตามที่อธิบายไว้ในเริ่มต้นใช้งาน Analytics สำหรับ C++

เหตุการณ์ในบันทึก

หลังจากเริ่มต้นโมดูล firebase::analytics แล้ว คุณจะใช้โมดูลนี้เพื่อบันทึก เหตุการณ์ด้วยเมธอด LogEvent() ได้

Analytics SDK กำหนดเหตุการณ์ที่แนะนำหลายรายการซึ่งใช้กันทั่วไปในแอปประเภทต่างๆ รวมถึงแอปค้าปลีกและอีคอมเมิร์ซ การเดินทาง และเกม เพื่อช่วยให้คุณเริ่มต้นใช้งานได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับเหตุการณ์เหล่านี้ และเวลาที่ควรใช้ได้ในบทความเหตุการณ์และพร็อพเพอร์ตี้ ในศูนย์ช่วยเหลือของ Firebase

คุณดูรายละเอียดการติดตั้งใช้งานสำหรับเหตุการณ์ที่แนะนำได้ในตำแหน่งต่อไปนี้

  • เหตุการณ์ที่แนะนํา: ดูรายการค่าคงที่ Event
  • พารามิเตอร์ที่กำหนด: ดูรายการค่าคงที่ของ Parameters

ตัวอย่างต่อไปนี้แสดงวิธีบันทึกเหตุการณ์ suggested 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 เป็นพารามิเตอร์อเนกประสงค์ ซึ่งมีประโยชน์ในการสะสมเมตริกสําคัญที่เกี่ยวข้องกับ Analytics เหตุการณ์ ตัวอย่างเช่น รายได้ ระยะทาง เวลา และคะแนน

หากแอปพลิเคชันของคุณมีความต้องการเฉพาะที่ไม่ได้ครอบคลุมในAnalyticsประเภทเหตุการณ์Analyticsที่แนะนำ คุณสามารถบันทึกAnalyticsเหตุการณ์Analyticsที่กำหนดเองของคุณเองได้ตามตัวอย่างนี้

// 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

คำสั่งนี้จะแสดงเหตุการณ์ใน Logcat ของ Android Studio ซึ่งจะช่วยให้คุณ ยืนยันได้ทันทีว่าระบบได้ส่งเหตุการณ์แล้ว

ดูเหตุการณ์ Analytics ในแดชบอร์ด

คุณดูสถิติแบบรวมเกี่ยวกับAnalyticsเหตุการณ์ได้ในแดชบอร์ดคอนโซล Firebase แดชบอร์ดเหล่านี้จะอัปเดตเป็นระยะตลอดทั้งวัน หากต้องการทดสอบทันที ให้ใช้เอาต์พุต Logcat ตามที่อธิบายไว้ใน ส่วนก่อนหน้า

วิธีเข้าถึงข้อมูลนี้ในFirebase Console

  1. เปิดโปรเจ็กต์ในคอนโซล Firebase
  2. เลือก Analytics จากเมนูเพื่อดูแดชบอร์ดการรายงาน Analytics

แท็บเหตุการณ์จะแสดงรายงานเหตุการณ์ที่สร้างขึ้นโดยอัตโนมัติสำหรับเหตุการณ์Analyticsแต่ละประเภทที่แตกต่างกันซึ่งแอปของคุณบันทึกไว้ ดูข้อมูลเพิ่มเติมเกี่ยวกับแดชบอร์ด