|
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), |
|
|
|
|
|
|
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
|
|
|
|