事前準備
如果您尚未將 Firebase 新增至 JavaScript 專案,請參閱該頁面,瞭解如何:
建立 Firebase 專案
向 Firebase 註冊網頁應用程式
請注意,將 Firebase 新增至應用程式時,您可能會完成本頁稍後所述的部分步驟 (例如新增 SDK 和初始化 Firebase)。
步驟 1:新增並初始化 Performance Monitoring
如果尚未 安裝 Firebase JS SDK 並初始化 Firebase,請先完成這些步驟。
新增 Performance Monitoring JS SDK 並初始化 Performance Monitoring:
Web
import { initializeApp } from "firebase/app"; import { getPerformance } from "firebase/performance"; // TODO: Replace the following with your app's Firebase project configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize Firebase const app = initializeApp(firebaseConfig); // Initialize Performance Monitoring and get a reference to the service const perf = getPerformance(app);
Web
import firebase from "firebase/compat/app"; import "firebase/compat/performance"; // TODO: Replace the following with your app's Firebase project configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize Firebase firebase.initializeApp(firebaseConfig); // Initialize Performance Monitoring and get a reference to the service const perf = firebase.performance();
Performance Monitoring SDK 會自動記錄 Core Web Vitals。
步驟 2:產生效能事件,以便顯示初始資料
成功將 SDK 新增至應用程式後,Firebase 就會開始處理事件。如果您仍在本機開發,請與應用程式互動,產生事件,以便進行初始資料收集和處理。
在本機環境中提供及查看網頁應用程式。
載入網站子頁面、與應用程式互動,以及/或觸發網路要求,藉此產生事件。網頁載入後,請務必讓瀏覽器分頁保持開啟至少 10 秒。
前往 Firebase 控制台的「Performance」(效能) 資訊主頁。幾分鐘內,您應該會看到初始資料顯示。
如果沒有看到初始資料,請參閱疑難排解提示。
步驟 3:(選用) 查看效能事件的記錄訊息
開啟瀏覽器的開發人員工具 (例如 Chrome 開發人員工具的「Network」分頁,或 Firefox 的「Network Monitor」)。
在瀏覽器中重新整理網頁應用程式。
查看記錄訊息,瞭解是否有任何錯誤訊息。
幾秒後,在瀏覽器的開發人員工具中,尋找對
firebaselogging.googleapis.com的網路呼叫。如果出現該網路呼叫,表示瀏覽器正在將成效資料傳送至 Firebase。
如果應用程式未記錄效能事件,請參閱疑難排解提示。
步驟 4:(選用) 為特定程式碼新增自訂監控
如要監控與應用程式中特定程式碼相關聯的成效資料,您可以檢測自訂程式碼追蹤記錄。
透過自訂程式碼追蹤,您可以評估應用程式完成特定工作或一組工作所需的時間,例如載入一組圖片或查詢資料庫。自訂程式碼追蹤記錄的預設指標是時間長度,但您也可以新增自訂指標,例如快取命中和記憶體警告。
在程式碼中,您可以使用 Performance Monitoring SDK 提供的 API 定義自訂程式碼追蹤記錄的開頭和結尾 (並新增任何所需的自訂指標)。
請參閱「新增特定程式碼的監控功能」,進一步瞭解這些功能,以及如何將這些功能新增至應用程式。
步驟 5:部署應用程式並查看結果
驗證 Performance Monitoring 後,即可向使用者部署更新版應用程式。
您可以在 Firebase 控制台的「Performance」(成效) 資訊主頁中監控成效資料。
後續步驟
進一步瞭解 Performance Monitoring 自動收集的資料:
- 應用程式網頁載入資料
- 應用程式發出的 HTTP/S 網路要求資料
在 Firebase 控制台中查看、追蹤及篩選成效資料
檢測自訂程式碼追蹤記錄,為應用程式中的特定工作或工作流程新增監控功能