summaryrefslogtreecommitdiff
path: root/site/js/google-analytics-message.js
blob: c776a8a5ed95c73ecb050ed30e575a24caf295a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
document.addEventListener('DOMContentLoaded', function (event) {

  function determineDataOfExpiration() {
    const date = new Date();
    date.setMonth(date.getMonth() + 1);  // Add one month to the current date
    const expiresString = "expires=" + date.toUTCString()
    return expiresString
  }
	// function to toggle Google Analytics message
  const toggleGoogleAnalyticsMessage = action => {
    const message = document.getElementById('google-analytics-message')
    action === 'show' && message.classList.add('open')
    action === 'hide' && message.classList.remove('open')
  }

  // check if cookies with user choice exist
  const isGoogleAnalyticsAllowedValueExist =
    document.cookie.split(';')
      .find(row => row.includes('isGoogleAnalyticsAllowed'))

  // if the user has already made a choice
  if (isGoogleAnalyticsAllowedValueExist) {

    const isGoogleAnalyticsAllowedValue = isGoogleAnalyticsAllowedValueExist.split('=')[1]

    // and if the user chose "Accept" - connect Google Analytics
    if (isGoogleAnalyticsAllowedValue === 'yes') {
      window.dataLayer = window.dataLayer || [];
      function gtag() { dataLayer.push(arguments); }
      gtag('js', new Date());
      gtag('config', 'G-J3WHFQG00P');
    }

  } else {

    // display a message with a question
    toggleGoogleAnalyticsMessage('show')

    const googleAnalyticsMessageAccept = document.querySelector('#google-analytics-accept')
    const googleAnalyticsMessageDecline = document.querySelectorAll('#google-analytics-decline')

    // register a click on the "Accept" button
    googleAnalyticsMessageAccept.addEventListener('click', function () {
      document.cookie = `isGoogleAnalyticsAllowed=yes;${determineDataOfExpiration()};path=/`
      toggleGoogleAnalyticsMessage('hide')
    })

    // register a click on the "Decline" button
    googleAnalyticsMessageDecline.forEach(el => {
      el.addEventListener('click', function () {
        document.cookie = `isGoogleAnalyticsAllowed=no;${determineDataOfExpiration()};path=/`
        toggleGoogleAnalyticsMessage('hide')
      })
    })

  }
})