Completed
Pull Request — master (#230)
by
unknown
04:14 queued 01:51
created

Features/GoogleAnalytics/script.js   A

Complexity

Total Complexity 6
Complexity/F 2

Size

Lines of Code 38
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 2
dl 0
loc 38
rs 10
c 1
b 0
f 0
wmc 6
mnd 2
bc 8
fnc 3
bpm 2.6666
cpm 2
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A script.js ➔ setOptoutCookie 0 4 1
A $gaOptoutLinks.click 0 11 2
A script.js ➔ getOptoutCookie 0 9 2
1
/* global wpData */
2
3
import $ from 'jquery'
4
5
const $gaOptoutLinks = $('.globalAction-optoutGa')
6
const data = wpData
7
8
const alreadyOptedOut = getOptoutCookie()
9
10
if (alreadyOptedOut) {
11
  $gaOptoutLinks.remove()
12
  window['ga-disable-' + data.gaId] = true
13
} else {
14
  $gaOptoutLinks.on('click', function (e) {
15
    e.preventDefault()
16
17
    const confirmOptout = window.confirm(data.confirm)
18
19
    if (confirmOptout) {
20
      window['ga-disable-' + data.gaId] = true
21
      window.alert(data.success)
22
      setOptoutCookie()
23
    }
24
  })
25
}
26
27
function setOptoutCookie () {
28
  document.cookie = 'disableGa=true'
29
  $gaOptoutLinks.remove()
30
}
31
32
function getOptoutCookie () {
33
  const value = document.cookie
34
  const parts = value.split('; disableGa=')
35
  if (parts.length === 2) {
36
    return parts.pop().split(';').shift()
37
  } else {
38
    return false
39
  }
40
}
41