Skip to content

Commit

Permalink
Fix console pollution in onetrust (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Mar 26, 2024
1 parent 4eaabad commit 632b87f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-stingrays-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-consent-wrapper-onetrust': patch
---

Fix log pollution for OneTrust users
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})

Expand Down
18 changes: 4 additions & 14 deletions packages/consent/consent-wrapper-onetrust/src/lib/onetrust-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 => {
Expand Down

0 comments on commit 632b87f

Please sign in to comment.