index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import getMeta from '@/utils/meta'
  2. function loadGA() {
  3. if (window.olLoadGA) {
  4. window.olLoadGA()
  5. }
  6. }
  7. function setConsent(value) {
  8. document.querySelector('.cookie-banner').classList.add('hidden')
  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. if (
  28. getMeta('ol-ExposedSettings').gaToken ||
  29. getMeta('ol-ExposedSettings').gaTokenV4
  30. ) {
  31. document
  32. .querySelectorAll('[data-ol-cookie-banner-set-consent]')
  33. .forEach(el => {
  34. el.addEventListener('click', function (e) {
  35. e.preventDefault()
  36. const consentType = el.getAttribute('data-ol-cookie-banner-set-consent')
  37. setConsent(consentType)
  38. })
  39. })
  40. const oaCookie = document.cookie.split('; ').find(c => c.startsWith('oa='))
  41. if (!oaCookie) {
  42. const cookieBannerEl = document.querySelector('.cookie-banner')
  43. if (cookieBannerEl) {
  44. cookieBannerEl.classList.remove('hidden')
  45. }
  46. }
  47. }