Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Modify logic around top level opt in checks #1576

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/__tests__/consent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
})

Expand Down
4 changes: 3 additions & 1 deletion src/consent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ 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 ||
(this.consent === ConsentStatus.PENDING && this.config.opt_out_capturing_by_default)
)
}

// 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading