index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. } else {
  22. document.cookie = 'oa=0' + cookieAttributes
  23. }
  24. }
  25. if (
  26. getMeta('ol-ExposedSettings').gaToken ||
  27. getMeta('ol-ExposedSettings').gaTokenV4
  28. ) {
  29. document
  30. .querySelectorAll('[data-ol-cookie-banner-set-consent]')
  31. .forEach(el => {
  32. el.addEventListener('click', function (e) {
  33. e.preventDefault()
  34. const consentType = el.getAttribute('data-ol-cookie-banner-set-consent')
  35. setConsent(consentType)
  36. })
  37. })
  38. const oaCookie = document.cookie.split('; ').find(c => c.startsWith('oa='))
  39. if (!oaCookie) {
  40. const cookieBannerEl = document.querySelector('.cookie-banner')
  41. if (cookieBannerEl) {
  42. cookieBannerEl.classList.remove('hidden')
  43. }
  44. }
  45. }