1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helick\GTM; |
4
|
|
|
|
5
|
|
|
use Helick\GTM\Contracts\Bootable; |
6
|
|
|
|
7
|
|
|
final class Snippet implements Bootable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Boot the service. |
11
|
|
|
* |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
|
|
public static function boot(): void |
15
|
|
|
{ |
16
|
|
|
$self = new static; |
17
|
|
|
|
18
|
|
|
add_action('wp_head', [$self, 'head'], 1, 0); |
|
|
|
|
19
|
|
|
add_action('wp_body_open', [$self, 'body'], 1, 0); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Display the head snippet. |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function head(): void |
28
|
|
|
{ |
29
|
|
|
$this->displayDataLayer(); |
30
|
|
|
|
31
|
|
|
$this->displayHeadSnippet(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Display the body snippet. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function body(): void |
40
|
|
|
{ |
41
|
|
|
$snippet = <<<HTML |
42
|
|
|
<!-- Google Tag Manager (noscript) --> |
43
|
|
|
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=%s" |
44
|
|
|
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> |
45
|
|
|
<!-- End Google Tag Manager (noscript) --> |
46
|
|
|
HTML; |
47
|
|
|
|
48
|
|
|
$containerId = containerId(); |
49
|
|
|
|
50
|
|
|
echo sprintf($snippet, esc_js($containerId)), "\n"; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Display the data layer. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
private function displayDataLayer(): void |
59
|
|
|
{ |
60
|
|
|
$snippet = '<script>%s=[%s]</script>'; |
61
|
|
|
|
62
|
|
|
$dataLayerVariable = dataLayerVariable(); |
63
|
|
|
$dataLayer = dataLayer(); |
64
|
|
|
|
65
|
|
|
if (empty($dataLayer)) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
echo sprintf( |
70
|
|
|
$snippet, |
71
|
|
|
esc_js($dataLayerVariable), |
|
|
|
|
72
|
|
|
wp_json_encode($dataLayer) |
|
|
|
|
73
|
|
|
), "\n"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Display the head snippet. |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
private function displayHeadSnippet(): void |
82
|
|
|
{ |
83
|
|
|
$snippet = <<<HTML |
84
|
|
|
<!-- Google Tag Manager --> |
85
|
|
|
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
86
|
|
|
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
87
|
|
|
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
88
|
|
|
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
89
|
|
|
})(window,document,'script','%s','%s');</script> |
90
|
|
|
<!-- End Google Tag Manager --> |
91
|
|
|
HTML; |
92
|
|
|
|
93
|
|
|
$dataLayerVariable = dataLayerVariable(); |
94
|
|
|
$containerId = containerId(); |
95
|
|
|
|
96
|
|
|
echo sprintf( |
97
|
|
|
$snippet, |
98
|
|
|
esc_js($dataLayerVariable), |
|
|
|
|
99
|
|
|
esc_js($containerId) |
100
|
|
|
), "\n"; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|