utils.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. )
  35. }
  36. export function hasMadeCookieChoice() {
  37. return document.cookie.split('; ').some(c => c.startsWith('oa='))
  38. }