Skip to content

Commit 09aec3d

Browse files
authored
[fix] store PostHog opt-in in cookie_consent as well (#233)
1 parent 16f4c82 commit 09aec3d

File tree

6 files changed

+24
-26
lines changed

6 files changed

+24
-26
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"js-md5": "^0.8.3",
6161
"lowlight": "^3.1.0",
6262
"mustache": "^4.2.0",
63-
"posthog-js": "^1.133.0",
63+
"posthog-js": "^1.135.2",
6464
"qrcode": "^1.5.3",
6565
"react": "^18.3.1",
6666
"react-dom": "^18.3.1",

src/locales/de-DE/messages.po

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ msgstr "In wenigen Minuten bis zum ersten Ergebnis"
179179
msgid "Abandoned"
180180
msgstr "Verlassen"
181181

182-
#: src/shared/cookie-consent/CookieConsent.tsx:70
182+
#: src/shared/cookie-consent/CookieConsent.tsx:81
183183
msgid "Accept"
184184
msgstr ""
185185

@@ -1455,7 +1455,7 @@ msgstr "Regionen"
14551455
msgid "Register"
14561456
msgstr "Registrieren"
14571457

1458-
#: src/shared/cookie-consent/CookieConsent.tsx:84
1458+
#: src/shared/cookie-consent/CookieConsent.tsx:95
14591459
msgid "Reject"
14601460
msgstr ""
14611461

@@ -1801,7 +1801,7 @@ msgstr "Warnung"
18011801
msgid "We have sent an email with a confirmation link to your email address. Please follow the link to activate your account."
18021802
msgstr "Wir haben eine E-Mail mit einem Bestätigungslink an Ihre E-Mail-Adresse gesendet. Bitte folgen Sie dem Link, um Ihr Konto zu aktivieren."
18031803

1804-
#: src/shared/cookie-consent/CookieConsent.tsx:46
1804+
#: src/shared/cookie-consent/CookieConsent.tsx:52
18051805
msgid "We use cookies and other tracking technologies to analyze site usage and assist in marketing efforts. For details, see our <0>cookie policy<1/></0>."
18061806
msgstr ""
18071807

src/locales/en-US/messages.po

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ msgstr "A few minutes to first results"
179179
msgid "Abandoned"
180180
msgstr "Abandoned"
181181

182-
#: src/shared/cookie-consent/CookieConsent.tsx:70
182+
#: src/shared/cookie-consent/CookieConsent.tsx:81
183183
msgid "Accept"
184184
msgstr "Accept"
185185

@@ -1455,7 +1455,7 @@ msgstr "Regions"
14551455
msgid "Register"
14561456
msgstr "Register"
14571457

1458-
#: src/shared/cookie-consent/CookieConsent.tsx:84
1458+
#: src/shared/cookie-consent/CookieConsent.tsx:95
14591459
msgid "Reject"
14601460
msgstr "Reject"
14611461

@@ -1801,7 +1801,7 @@ msgstr "Warning"
18011801
msgid "We have sent an email with a confirmation link to your email address. Please follow the link to activate your account."
18021802
msgstr "We have sent an email with a confirmation link to your email address. Please follow the link to activate your account."
18031803

1804-
#: src/shared/cookie-consent/CookieConsent.tsx:46
1804+
#: src/shared/cookie-consent/CookieConsent.tsx:52
18051805
msgid "We use cookies and other tracking technologies to analyze site usage and assist in marketing efforts. For details, see our <0>cookie policy<1/></0>."
18061806
msgstr "We use cookies and other tracking technologies to analyze site usage and assist in marketing efforts. For details, see our <0>cookie policy<1/></0>."
18071807

src/shared/cookie-consent/CookieConsent.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ import { PostHogPageView } from 'src/shared/posthog'
1010
const CookieConsentComp = () => {
1111
const postHog = usePostHog()
1212
const containerRef = useRef<HTMLDivElement>(null)
13-
const [showConsent, setShowConsent] = useState(postHog.has_opted_in_capturing() ? false : Cookies.get('cookie_consent') !== 'false')
13+
const [showConsent, setShowConsent] = useState(false)
1414

1515
useEffect(() => {
16-
if (postHog.has_opted_in_capturing() || Cookies.get('cookie_consent') !== 'false') {
16+
if (Cookies.get('cookie_consent') === 'true') {
17+
postHog.opt_in_capturing({ enable_persistence: true })
18+
} else if (!postHog.has_opted_in_capturing()) {
19+
setShowConsent(Cookies.get('cookie_consent') !== 'false')
20+
}
21+
22+
if (Cookies.get('cookie_consent') !== 'true' && Cookies.get('cookie_consent') !== 'false') {
1723
Cookies.remove('cookie_consent', {
1824
domain: env.isProd ? '.fix.security' : undefined,
1925
secure: !env.isLocal,
@@ -64,6 +70,11 @@ const CookieConsentComp = () => {
6470
variant="contained"
6571
onClick={() => {
6672
setShowConsent(false)
73+
Cookies.set('cookie_consent', 'true', {
74+
domain: env.isProd ? '.fix.security' : undefined,
75+
secure: !env.isLocal,
76+
expires: 365,
77+
})
6778
postHog.opt_in_capturing({ enable_persistence: true })
6879
}}
6980
>

src/shared/posthog/PostHogProvider.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useSuspenseQuery } from '@tanstack/react-query'
2-
import Cookies from 'js-cookie'
32
import postHog from 'posthog-js'
43
import { PostHogProvider as Provider } from 'posthog-js/react'
54
import { useEffect, useState } from 'react'
@@ -24,15 +23,6 @@ export const PostHogProvider = ({ children }: { children: React.ReactNode }) =>
2423
environment === 'prd' ? import.meta.env.VITE_POSTHOG_PROD_PROJECT_API_KEY : import.meta.env.VITE_POSTHOG_DEV_PROJECT_API_KEY
2524
env.isProd = environment === 'prd'
2625
if (projectApiKey && !postHog.__loaded) {
27-
const postHogCookie = Cookies.get(`ph_${projectApiKey}_posthog`)
28-
let parsedPostHogCookie: { distinct_id?: string } | undefined
29-
if (postHogCookie) {
30-
try {
31-
parsedPostHogCookie = JSON.parse(postHogCookie) as { distinct_id?: string } | undefined
32-
} catch {
33-
parsedPostHogCookie = undefined
34-
}
35-
}
3626
postHog.init(projectApiKey, {
3727
api_host: env.postHogApiHost,
3828
ui_host: env.postHogUiHost,
@@ -49,9 +39,6 @@ export const PostHogProvider = ({ children }: { children: React.ReactNode }) =>
4939
disable_surveys: true,
5040
enable_recording_console_log: false,
5141
})
52-
if (!window.localStorage.getItem(`ph_${projectApiKey}_posthog`) && parsedPostHogCookie?.distinct_id) {
53-
postHog.opt_in_capturing({ enable_persistence: true })
54-
}
5542
}
5643
setInitialized(true)
5744
}, [environment])

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9055,10 +9055,10 @@ postcss@^8.4.38:
90559055
picocolors "^1.0.0"
90569056
source-map-js "^1.2.0"
90579057

9058-
posthog-js@^1.133.0:
9059-
version "1.133.0"
9060-
resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.133.0.tgz#fbba377d8e11dd5833685111ac219049771736ea"
9061-
integrity sha512-d+TfOqWTPRGoFuxaxRaGhh/XCg1tR5TyjdxCIW1Qp1XQE22zqite2/vg5l+mE6VdZfjMqeBStx0wjmxOj3uUDA==
9058+
posthog-js@^1.135.2:
9059+
version "1.135.2"
9060+
resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.135.2.tgz#1da1508760521e6f0fe1ab908bc4ffbe04c2952c"
9061+
integrity sha512-kqix067CyrlcNKUhVxrys8Qp0O/8FUtlkp7lfM+tkJFJAMZsKjIDVslz2AjI9y79CvyyZX+pddfA7F3YFYlS0Q==
90629062
dependencies:
90639063
fflate "^0.4.8"
90649064
preact "^10.19.3"

0 commit comments

Comments
 (0)