From 24027033b292a0ee4096eebd0b0919076d1b8f8b Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 21 May 2025 10:39:50 +0200 Subject: [PATCH] fix(browser): Ensure logs are flushed when sendClientReports=false This was tied together and could theoretically result in logs not being flushed correctly, when users opt-out of client reports. --- packages/browser/src/client.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 73cbd55d42db..c0841d161ed6 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -85,41 +85,41 @@ export class BrowserClient extends Client { super(opts); - // eslint-disable-next-line @typescript-eslint/no-this-alias - const client = this; - const { sendDefaultPii, _experiments } = client._options; + const { sendDefaultPii, sendClientReports, _experiments } = this._options; const enableLogs = _experiments?.enableLogs; - if (opts.sendClientReports && WINDOW.document) { + if (WINDOW.document && (sendClientReports || enableLogs)) { WINDOW.document.addEventListener('visibilitychange', () => { if (WINDOW.document.visibilityState === 'hidden') { - this._flushOutcomes(); + if (sendClientReports) { + this._flushOutcomes(); + } if (enableLogs) { - _INTERNAL_flushLogsBuffer(client); + _INTERNAL_flushLogsBuffer(this); } } }); } if (enableLogs) { - client.on('flush', () => { - _INTERNAL_flushLogsBuffer(client); + this.on('flush', () => { + _INTERNAL_flushLogsBuffer(this); }); - client.on('afterCaptureLog', () => { - if (client._logFlushIdleTimeout) { - clearTimeout(client._logFlushIdleTimeout); + this.on('afterCaptureLog', () => { + if (this._logFlushIdleTimeout) { + clearTimeout(this._logFlushIdleTimeout); } - client._logFlushIdleTimeout = setTimeout(() => { - _INTERNAL_flushLogsBuffer(client); + this._logFlushIdleTimeout = setTimeout(() => { + _INTERNAL_flushLogsBuffer(this); }, DEFAULT_FLUSH_INTERVAL); }); } if (sendDefaultPii) { - client.on('postprocessEvent', addAutoIpAddressToUser); - client.on('beforeSendSession', addAutoIpAddressToSession); + this.on('postprocessEvent', addAutoIpAddressToUser); + this.on('beforeSendSession', addAutoIpAddressToSession); } }