utils.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import getMeta from '@/utils/meta'
  2. export type CookieConsentValue = 'all' | 'essential'
  3. function loadGA() {
  4. if (window.olLoadGA) {
  5. window.olLoadGA()
  6. }
  7. }
  8. export function setConsent(value: CookieConsentValue | null) {
  9. const cookieDomain = getMeta('ol-ExposedSettings').cookieDomain
  10. const oneYearInSeconds = 60 * 60 * 24 * 365
  11. const cookieAttributes =
  12. '; path=/' +
  13. '; domain=' +
  14. cookieDomain +
  15. '; max-age=' +
  16. oneYearInSeconds +
  17. '; SameSite=Lax; Secure'
  18. if (value === 'all') {
  19. document.cookie = 'oa=1' + cookieAttributes
  20. loadGA()
  21. window.dispatchEvent(new CustomEvent('cookie-consent', { detail: true }))
  22. } else {
  23. document.cookie = 'oa=0' + cookieAttributes
  24. window.dispatchEvent(new CustomEvent('cookie-consent', { detail: false }))
  25. }
  26. }
  27. export function cookieBannerRequired() {
  28. const exposedSettings = getMeta('ol-ExposedSettings')
  29. return Boolean(
  30. exposedSettings.gaToken ||
  31. exposedSettings.gaTokenV4 ||
  32. exposedSettings.propensityId ||
  33. exposedSettings.hotjarId ||
  34. (exposedSettings.mixpanelLabsToken && exposedSettings.labsEnabled)
  35. )
  36. }
  37. export function hasMadeCookieChoice() {
  38. return document.cookie.split('; ').some(c => c.startsWith('oa='))
  39. }