Stay organized with collections
Save and categorize content based on your preferences.
You can send exception events to measure the number and type of crashes or
errors that occur on a web page. This page describes how to use gtag.js to send
exceptions to Google Analytics.
Implementation
When an error occurs, send an exception event to Google Analytics:
where <exception_parameters> is one or more parameter-value pairs. Separate
each pair by a comma. For example, this command sends a nonfatal error
exception.
gtag('event', 'exception', {
'description': 'error_description',
'fatal': false // set to true if the error is fatal
});
Exception parameters
The following table lists the exception parameters:
Parameter name
Data type
Required
Description
description
string
No
A description of the error.
fatal
boolean
No
true if the error was fatal.
Example
Given the following function:
function divide(x, y) {
if (y === 0) {
throw "Division by zero";
}
return x/y;
}
the following code will send an exception event to Google Analytics if the
divisor y is zero:
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
});
}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-09 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"]]