-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Reacting to config changes (#1138)
- Loading branch information
1 parent
e3ca684
commit 4998648
Showing
19 changed files
with
254 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import posthog, { PostHogConfig } from 'posthog-js' | ||
|
||
/** | ||
* Below is an example of a consent-driven config for PostHog | ||
* Lots of things start in a disabled state and posthog will not use cookies without consent | ||
* | ||
* Once given, we enable autocapture, session recording, and use localStorage+cookie for persistence via set_config | ||
* This is only an example - data privacy requirements are different for every project | ||
*/ | ||
export function cookieConsentGiven(): null | boolean { | ||
if (typeof window === 'undefined') return null | ||
return localStorage.getItem('cookie_consent') === 'true' | ||
} | ||
|
||
export const configForConsent = (): Partial<PostHogConfig> => { | ||
const consentGiven = localStorage.getItem('cookie_consent') === 'true' | ||
|
||
return { | ||
persistence: consentGiven ? 'localStorage+cookie' : 'memory', | ||
disable_surveys: !consentGiven, | ||
autocapture: consentGiven, | ||
disable_session_recording: !consentGiven, | ||
} | ||
} | ||
|
||
export const updatePostHogConsent = (consentGiven: boolean) => { | ||
if (consentGiven) { | ||
localStorage.setItem('cookie_consent', 'true') | ||
} else { | ||
localStorage.removeItem('cookie_consent') | ||
} | ||
|
||
posthog.set_config(configForConsent()) | ||
} | ||
|
||
if (typeof window !== 'undefined') { | ||
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY || '', { | ||
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com', | ||
session_recording: { | ||
recordCrossOriginIframes: true, | ||
}, | ||
debug: true, | ||
scroll_root_selector: ['#scroll_element', 'html'], | ||
// persistence: cookieConsentGiven() ? 'localStorage+cookie' : 'memory', | ||
__preview_process_person: 'identified_only', | ||
...configForConsent(), | ||
}) | ||
;(window as any).posthog = posthog | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.