GoogleTagManagerScripts::doWpBodyOpenAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leonidas\Framework\Site\Module;
4
5
use Leonidas\Framework\Module\Abstracts\Module;
6
use Leonidas\Hooks\TargetsWpBodyOpenHook;
7
use Leonidas\Hooks\TargetsWpHeadHook;
8
use WebTheory\Html\Html;
9
10
class GoogleTagManagerScripts extends Module
11
{
12
    use TargetsWpBodyOpenHook;
13
    use TargetsWpHeadHook;
14
15
    protected const NS_URL = 'https://www.googletagmanager.com/ns.html';
16
17
    protected const GTAG_URL = 'https://www.googletagmanager.com/gtag/js';
18
19
    protected const GTM_SCRIPT = <<<JS
20
        (function (w, d, s, l, i) {
21
            w[l] = w[l] || [];
22
            w[l].push({'gtm.start': new Date().getTime(), event: 'gtm.js'});
23
            var f = d.getElementsByTagName(s)[0],
24
                j = d.createElement(s),
25
                dl = l != 'dataLayer' ? '&l=' + l : '';
26
            j.async = true;
27
            j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
28
            f.parentNode.insertBefore(j, f);
29
        })(window, document, 'script', 'dataLayer', '%s');
30
    JS;
31
32
    protected string $tagId;
33
34
    public function hook(): void
35
    {
36
        $this->targetWpBodyOpenHook();
37
        $this->targetWpHeadHook();
38
    }
39
40
    protected function getTagId(): string
41
    {
42
        return $this->tagId ?? $this->tagId();
43
    }
44
45
    protected function getWpHeadPriority(): int
46
    {
47
        return PHP_INT_MIN;
48
    }
49
50
    protected function getWpBodyOpenPriority(): int
51
    {
52
        return PHP_INT_MIN;
53
    }
54
55
    protected function doWpHeadAction(): void
56
    {
57
        $nl = PHP_EOL;
58
        $script = $nl . $this->getGtmScript() . $nl;
59
60
        echo '<!-- Google Tag Manager -->' . $nl;
61
        echo Html::tag('script', [], $script) . $nl;
62
        echo '<!-- End Google Tag Manager -->' . $nl;
63
    }
64
65
    protected function doWpBodyOpenAction(): void
66
    {
67
        $nl = PHP_EOL;
68
        $iframe = $this->getNsIframe();
69
70
        echo '<!-- Google Tag Manager (noscript) -->' . $nl;
71
        echo Html::tag('noscript', [], $iframe) . $nl;
72
        echo '<!-- End Google Tag Manager (noscript) -->' . $nl;
73
    }
74
75
    protected function getGtmScript(): string
76
    {
77
        return sprintf(static::GTM_SCRIPT, $this->getTagId());
78
    }
79
80
    protected function getNsIframe(): string
81
    {
82
        return Html::tag('iframe', [
83
            'src' => add_query_arg($this->getNsQuery(), static::NS_URL),
0 ignored issues
show
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

83
            'src' => /** @scrutinizer ignore-call */ add_query_arg($this->getNsQuery(), static::NS_URL),
Loading history...
84
            'height' => '0',
85
            'width' => '0',
86
            'style' => 'display:none;visibility:hidden',
87
        ]);
88
    }
89
90
    protected function getNsQuery(): array
91
    {
92
        return ['id' => $this->getTagId()];
93
    }
94
95
    protected function tagId(): string
96
    {
97
        return $this->getConfig('services.google.tag_manager.id', '');
98
    }
99
}
100