diff --git a/src/__tests__/consent.test.ts b/src/__tests__/consent.test.ts index b6f520ee5..89a892aa9 100644 --- a/src/__tests__/consent.test.ts +++ b/src/__tests__/consent.test.ts @@ -40,8 +40,8 @@ describe('consentManager', () => { deleteAllCookies() }) - it('should start default opted in', () => { - expect(posthog.has_opted_in_capturing()).toBe(true) + it('should start neither opted in or out', () => { + expect(posthog.has_opted_in_capturing()).toBe(false) expect(posthog.has_opted_out_capturing()).toBe(false) expect(posthog.persistence?.disabled).toBe(false) @@ -188,10 +188,12 @@ describe('consentManager', () => { it('should respect it if explicitly set', () => { posthog = createPostHog({ respect_dnt: true }) expect(posthog.has_opted_in_capturing()).toBe(false) + expect(posthog.has_opted_out_capturing()).toBe(true) }) it('should not respect it if not explicitly set', () => { - expect(posthog.has_opted_in_capturing()).toBe(true) + expect(posthog.has_opted_in_capturing()).toBe(false) + expect(posthog.has_opted_out_capturing()).toBe(false) }) }) diff --git a/src/consent.ts b/src/consent.ts index 0b6f4b830..a4010fbcc 100644 --- a/src/consent.ts +++ b/src/consent.ts @@ -32,6 +32,7 @@ export class ConsentManager { return this.storedConsent } + // NOTE: This is the method that should generally be used internally to check for whether we can track public isOptedOut() { return ( this.consent === ConsentStatus.DENIED || @@ -39,8 +40,9 @@ export class ConsentManager { ) } + // Only returns true if _explicitly_ opted in. If the user is pending, they are not opted in. public isOptedIn() { - return !this.isOptedOut() + return this.consent === ConsentStatus.GRANTED } public optInOut(isOptedIn: boolean) { diff --git a/src/posthog-core.ts b/src/posthog-core.ts index c7594073e..4de82a417 100644 --- a/src/posthog-core.ts +++ b/src/posthog-core.ts @@ -599,7 +599,7 @@ export class PostHog { // NOTE: We want to fire this on the next tick as the previous implementation had this side effect // and some clients may rely on it setTimeout(() => { - if (this.consent.isOptedIn()) { + if (!this.consent.isOptedOut()) { this._captureInitialPageview() } }, 1)