Issues (281)

Branch: master

src/Frontend/Core/Header/GoogleAnalytics.php (1 issue)

1
<?php
2
3
namespace Frontend\Core\Header;
4
5
use Common\Core\Cookie;
6
use Common\ModulesSettings;
7
use ForkCMS\Privacy\ConsentDialog;
8
9
final class GoogleAnalytics
10
{
11
    /** @var ModulesSettings */
12
    private $modulesSettings;
13
14
    /** @var Cookie */
15
    private $cookie;
16
17
    /** @var ConsentDialog */
18
    private $consentDialog;
19
20
    public function __construct(ModulesSettings $modulesSettings, ConsentDialog $consentDialog, Cookie $cookie)
21
    {
22
        $this->modulesSettings = $modulesSettings;
23
        $this->consentDialog = $consentDialog;
24
        $this->cookie = $cookie;
25
    }
26
27
    private function shouldAddGoogleAnalyticsHtml(): bool
28
    {
29
        $googleAnalyticsTrackingId = $this->modulesSettings->get(
30
            'Core',
31
            'google_tracking_google_analytics_tracking_id',
32
            ''
33
        );
34
35
        return ($googleAnalyticsTrackingId !== '');
36
    }
37
38
    private function shouldAnonymize(): bool
39
    {
40
        // @deprecated remove this in Fork 6, the privacy consent dialog should be used
41
        if ($this->modulesSettings->get('Core', 'show_cookie_bar', false) && !$this->cookie->hasAllowedCookies()) {
0 ignored issues
show
Deprecated Code introduced by
The function Common\Core\Cookie::hasAllowedCookies() has been deprecated: remove this in Fork 6, the privacy consent dialog should be used ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
        if ($this->modulesSettings->get('Core', 'show_cookie_bar', false) && !/** @scrutinizer ignore-deprecated */ $this->cookie->hasAllowedCookies()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
42
            return true;
43
        }
44
45
        // if the consent dialog is disabled we will anonymize by default
46
        if (!$this->modulesSettings->get('Core', 'show_consent_dialog', false)) {
47
            return true;
48
        }
49
50
        // the visitor has agreed to be tracked
51
        if ($this->consentDialog->hasAgreedTo('statistics')) {
52
            return false;
53
        }
54
55
        // fallback
56
        return true;
57
    }
58
59
    public function __toString(): string
60
    {
61
        if (!$this->shouldAddGoogleAnalyticsHtml()) {
62
            return '';
63
        }
64
65
        $code = [
66
            '<!-- Global site tag (gtag.js) - Google Analytics -->',
67
            '<script async src="https://www.googletagmanager.com/gtag/js?id=%1$s"></script>',
68
            '<script>',
69
            '  window.dataLayer = window.dataLayer || [];',
70
            '  function gtag(){dataLayer.push(arguments);}',
71
            '  gtag(\'js\', new Date());',
72
        ];
73
74
        if ($this->shouldAnonymize()) {
75
            $code[] = '  gtag(\'config\', \'%1$s\', { \'anonymize_ip\': true });';
76
        } else {
77
            $code[] = '  gtag(\'config\', \'%1$s\');';
78
        }
79
80
        $code[] = '</script>';
81
82
        return sprintf(
83
            implode("\n", $code) . "\n",
84
            $this->modulesSettings->get('Core', 'google_tracking_google_analytics_tracking_id', null)
85
        );
86
    }
87
}
88