Google Ads Script Errors in Sentry for React Apps
Problem Statement
When integrating Google Ads (gtag.js) into older React applications (5-7+ years), you may encounter recurring Sentry errors like "Can't find variable: gmo" or "invalid or unexpected token". These occur despite the application continuing to function normally. Errors trace back to the Google Tag Manager script snippet in index.html
and are characterized by:
- Non-breaking nature: Users experience no app crashes or visible issues
- External origin: Errors originate from the Google Ads JavaScript
- False positives: Errors clutter your monitoring without indicating app defects
Solutions
Approach 1: Ignore Errors in Sentry Configuration (Recommended)
Configure Sentry to exclude Google Ads-related errors using the ignoreErrors
option:
import * as Sentry from '@sentry/react';
Sentry.init({
dsn: process.env.REACT_APP_SENTRY_DSN,
ignoreErrors: [
"Can't find variable: gmo",
"invalid or unexpected token"
],
// Optional: Also ignore by script origin
denyUrls: [
/googletagmanager\.com/,
/google-analytics\.com/
]
});
Why this works:
- Prevents noise from third-party scripts
- Maintains focus on application-specific errors
- Handles errors Safely without impacting user experience
WARNING
Use message-based filtering only if:
- Errors originate exclusively from external scripts
- You've confirmed error messages won't conflict with app errors
Approach 2: Advanced Filtering with beforeSend
For more control, use Sentry's beforeSend
hook to filter programmatically:
Sentry.init({
dsn: process.env.REACT_APP_SENTRY_DSN,
beforeSend(event) {
const isGoogleScriptError = event.exception?.values?.some(exception =>
// Match error messages
(exception.value?.includes("gmo") ||
exception.value?.includes("invalid token")) &&
// Verify origin in stack trace
exception.stacktrace?.frames?.some(frame =>
frame.filename?.match(/googletagmanager\.com|google-analytics\.com/)
)
);
return isGoogleScriptError ? null : event;
}
});
Key Explanations
Why these errors occur:
gmo
reference likely comes from Google Marketing Objects scriptsinvalid token
often relates to URL parameter handling in ad tracking- Race conditions between script loading and execution
- Google script modifications outside your control
Why not "fix" the scripts:
- Defining
window.gmo = {}
adds unsupported invariants - Google's