Total Complexity | 6 |
Complexity/F | 2 |
Lines of Code | 38 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | /* global wpData */ |
||
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 |