index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. getMeta('ol-ExposedSettings').propensityId
  31. ) {
  32. document
  33. .querySelectorAll('[data-ol-cookie-banner-set-consent]')
  34. .forEach(el => {
  35. el.addEventListener('click', function (e) {
  36. e.preventDefault()
  37. const consentType = el.getAttribute('data-ol-cookie-banner-set-consent')
  38. setConsent(consentType)
  39. })
  40. })
  41. const oaCookie = document.cookie.split('; ').find(c => c.startsWith('oa='))
  42. if (!oaCookie) {
  43. const cookieBannerEl = document.querySelector('.cookie-banner')
  44. if (cookieBannerEl) {
  45. cookieBannerEl.classList.remove('hidden')
  46. }
  47. }
  48. }