wp-sentry
wp-sentry copied to clipboard
Browser: How to add `beforeSend` hook?
I tried adding a beforeSend hook. I found #42 but it is not described enough Could you describe exactly how to add beforeSend hook?
Sentry.init({
beforeSend(event, hint) {
if (hint.originalException === "Timeout") return null;
return event;
}
});
Thank you in advance
See: https://github.com/stayallive/wp-sentry#client-side-hook
Something like this might be what you need, not tested though so check it over yourself before using please!
add_action( 'wp_enqueue_scripts', function () {
wp_add_inline_script( 'wp-sentry-browser', 'function wp_sentry_hook(options) { options.beforeSend(event, hint) { return hint.originalException === "Timeout" ? null : event; } }', 'before' );
} );
I'd like to add that maintaining JS snippets within PHP code can quickly become a nightmare (and VS Code will not have any syntax highlighting for the JS inside of the PHP code which makes it even worse), so I'd suggest something like this:
functions.php:
add_action(
'wp_enqueue_scripts',
function () {
// Move your JS to this file
$sentry_config_path = get_template_directory() . '/js/sentry.js';
if ( file_exists( $sentry_config_path ) ) {
// Load the contents of the bundled script
$sentry_config_script = file_get_contents( $sentry_config_path );
// Inline the script before the wp-sentry-browser script
wp_add_inline_script( 'wp-sentry-browser', $sentry_config_script, 'before' );
}
}
);
sentry.js:
function wp_sentry_hook(options) {
options.beforeSend = function (event, hint) {
// Modify the event here
return event;
};
return options;
}