評估例外狀況
您可以傳送例外狀況事件,評估當機次數和類型。
發生的錯誤。本頁說明如何使用 gtag.js 傳送
例外狀況。
導入作業
發生錯誤時,請將例外狀況事件傳送至 Google Analytics:
gtag('event', 'exception', {<exception_parameters>});
其中 <exception_parameters>
是一或多個參數/值組合。分用
來逐一列出各個字詞組合舉例來說,這個指令會傳送一般錯誤
例外狀況。
gtag('event', 'exception', {
'description': 'error_description',
'fatal': false // set to true if the error is fatal
});
例外狀況參數
下表列出例外狀況參數:
參數名稱 |
資料類型 |
必填 |
說明 |
description |
字串 |
否 |
錯誤說明。 |
fatal |
布林值 |
否 |
如果錯誤為嚴重錯誤,則為 true 。 |
範例
以下方函式為例:
function divide(x, y) {
if (y === 0) {
throw "Division by zero";
}
return x/y;
}
如果
除數 y 為零:
var x = document.getElementById('x').value;
var y = document.getElementById('y').value;
try {
var r = divide(x, y);
} catch(err) {
gtag('event', 'exception', {
'description': err,
'fatal': false
});
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-09-13 (世界標準時間)。
[null,null,["上次更新時間:2024-09-13 (世界標準時間)。"],[[["Google Analytics can track website errors and crashes using exception events sent via gtag.js."],["`gtag('event', 'exception', {\u003cexception_parameters\u003e})` is the core function to send exception data, including an optional description and fatality status."],["An example demonstrates how to capture and send exceptions occurring within a JavaScript `try...catch` block."]]],["Exception events, used to track web page crashes and errors, are sent to Google Analytics via the `gtag('event', 'exception', {\u003cexception_parameters\u003e});` command. `\u003cexception_parameters\u003e` include 'description' (error details) and 'fatal' (boolean indicating if the error is fatal). When an error is detected, a `gtag` event can be sent. An example uses a `try...catch` block to intercept division-by-zero errors and trigger the `gtag` event.\n"]]