From 632b87f376bed558d2d6327d7b130b3bc1d8dffa Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Tue, 26 Mar 2024 15:43:29 -0500 Subject: [PATCH] Fix console pollution in onetrust (#1055) --- .changeset/olive-stingrays-shave.md | 5 +++++ .../src/lib/__tests__/onetrust-api.test.ts | 19 ------------------- .../src/lib/onetrust-api.ts | 18 ++++-------------- 3 files changed, 9 insertions(+), 33 deletions(-) create mode 100644 .changeset/olive-stingrays-shave.md diff --git a/.changeset/olive-stingrays-shave.md b/.changeset/olive-stingrays-shave.md new file mode 100644 index 000000000..769ebf783 --- /dev/null +++ b/.changeset/olive-stingrays-shave.md @@ -0,0 +1,5 @@ +--- +'@segment/analytics-consent-wrapper-onetrust': patch +--- + +Fix log pollution for OneTrust users diff --git a/packages/consent/consent-wrapper-onetrust/src/lib/__tests__/onetrust-api.test.ts b/packages/consent/consent-wrapper-onetrust/src/lib/__tests__/onetrust-api.test.ts index 8cd512d95..25c9909e6 100644 --- a/packages/consent/consent-wrapper-onetrust/src/lib/__tests__/onetrust-api.test.ts +++ b/packages/consent/consent-wrapper-onetrust/src/lib/__tests__/onetrust-api.test.ts @@ -21,39 +21,20 @@ beforeEach(() => { describe(getOneTrustGlobal, () => { it('should get the global', () => { - const consoleErrorSpy = jest - .spyOn(console, 'error') - .mockImplementationOnce(() => {}) ;(window as any).OneTrust = OneTrustMockGlobal expect(getOneTrustGlobal()).toEqual(OneTrustMockGlobal) - expect(consoleErrorSpy).not.toHaveBeenCalled() }) it('should handle null or undefined', () => { - const consoleErrorSpy = jest - .spyOn(console, 'error') - .mockImplementationOnce(() => {}) ;(window as any).OneTrust = undefined expect(getOneTrustGlobal()).toBeUndefined() ;(window as any).OneTrust = null expect(getOneTrustGlobal()).toBeUndefined() - expect(consoleErrorSpy).not.toHaveBeenCalled() }) it('should log an error if the global is an unexpected type', () => { ;(window as any).OneTrust = {} - const consoleErrorSpy = jest - .spyOn(console, 'error') - .mockImplementationOnce(() => {}) expect(getOneTrustGlobal()).toBeUndefined() - expect(consoleErrorSpy.mock.lastCall![0]).toMatch(/window.OneTrust/i) - }) - - it('should not log an error if OneTrust just returns geolocationResponse', () => { - ;(window as any).OneTrust = { geolocationResponse: {} as any } - const consoleErrorSpy = jest.spyOn(console, 'error') - expect(getOneTrustGlobal()).toBeUndefined() - expect(consoleErrorSpy).not.toHaveBeenCalled() }) }) diff --git a/packages/consent/consent-wrapper-onetrust/src/lib/onetrust-api.ts b/packages/consent/consent-wrapper-onetrust/src/lib/onetrust-api.ts index 8791bdf6f..759a0869d 100644 --- a/packages/consent/consent-wrapper-onetrust/src/lib/onetrust-api.ts +++ b/packages/consent/consent-wrapper-onetrust/src/lib/onetrust-api.ts @@ -42,6 +42,10 @@ export interface OneTrustGlobal { } export const getOneTrustGlobal = (): OneTrustGlobal | undefined => { + // window.OneTrust = {...} is used for user configuration e.g. 'Consent Rate Optimization' + // Also, if "show banner" is unchecked, window.OneTrust returns {geolocationResponse: {…}} before it actually returns the OneTrust object + // Thus, we are doing duck typing to see when this object is 'ready' + // function OptAnonWrapper() is the idiomatic way to check if OneTrust is ready, but polling for this works const oneTrust = (window as any).OneTrust if (!oneTrust) return undefined if ( @@ -52,20 +56,6 @@ export const getOneTrustGlobal = (): OneTrustGlobal | undefined => { ) { return oneTrust } - // if "show banner" is unchecked, window.OneTrust returns {geolocationResponse: {…}} before it actually returns the OneTrust object - if ('geolocationResponse' in oneTrust) { - return undefined - } - - console.error( - // OneTrust API has some gotchas -- since this function is often as a polling loop, not - // throwing an error since it's possible that some setup is happening behind the scenes and - // the OneTrust API is not available yet (e.g. see the geolocationResponse edge case). - new OneTrustApiValidationError( - 'window.OneTrust is unexpected type', - oneTrust - ).message - ) } export const getOneTrustActiveGroups = (): string | undefined => {