Skip to content

Commit

Permalink
Fix up top level funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Dec 2, 2024
1 parent b3b6ef6 commit b91dcc0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
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

0 comments on commit b91dcc0

Please sign in to comment.