例外を測定する
例外イベントを送信すると、ウェブページで発生するクラッシュやエラーの件数と種類を測定できます。このページでは、gtag.js を使用して Google アナリティクスに例外を送信する方法を説明します。
実装
エラーが発生したときに、Google アナリティクスに例外イベントを送信します。
gtag('event', 'exception', {<exception_parameters>});
<exception_parameters>
には、パラメータと値のペアを 1 つ以上指定し、各ペアはカンマで区切ります。たとえば、このコマンドは致命的でないエラーを
発生します。
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 がゼロの場合に、Google アナリティクスに例外イベントを送信したい場合、コードは次のようになります。
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
});
}
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2024-09-13 UTC。
[null,null,["最終更新日 2024-09-13 UTC。"],[[["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"]]