helick /
gtm
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Helick\GTM; |
||||||
| 4 | |||||||
| 5 | use Helick\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, 'dataLayer']); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 19 | add_action('wp_head', [$self, 'head']); |
||||||
| 20 | add_action('wp_body_open', [$self, 'body']); |
||||||
| 21 | } |
||||||
| 22 | |||||||
| 23 | /** |
||||||
| 24 | * Display the data layer snippet. |
||||||
| 25 | * |
||||||
| 26 | * @return void |
||||||
| 27 | */ |
||||||
| 28 | public function dataLayer(): void |
||||||
| 29 | { |
||||||
| 30 | $snippet = '<script>%s=[%s]</script>'; |
||||||
| 31 | |||||||
| 32 | $dataLayerVariable = data_layer_variable(); |
||||||
| 33 | $dataLayer = data_layer(); |
||||||
| 34 | |||||||
| 35 | if (empty($dataLayer)) { |
||||||
| 36 | return; |
||||||
| 37 | } |
||||||
| 38 | |||||||
| 39 | echo sprintf( |
||||||
| 40 | $snippet, |
||||||
| 41 | esc_js($dataLayerVariable), |
||||||
|
0 ignored issues
–
show
The function
esc_js 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
Loading history...
|
|||||||
| 42 | wp_json_encode($dataLayer) |
||||||
|
0 ignored issues
–
show
The function
wp_json_encode 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
Loading history...
|
|||||||
| 43 | ), "\n"; |
||||||
| 44 | } |
||||||
| 45 | |||||||
| 46 | /** |
||||||
| 47 | * Display the head snippet. |
||||||
| 48 | * |
||||||
| 49 | * @return void |
||||||
| 50 | */ |
||||||
| 51 | public function head(): void |
||||||
| 52 | { |
||||||
| 53 | $snippet = <<<HTML |
||||||
| 54 | <!-- Google Tag Manager --> |
||||||
| 55 | <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
||||||
| 56 | new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
||||||
| 57 | j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
||||||
| 58 | 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
||||||
| 59 | })(window,document,'script','%s','%s');</script> |
||||||
| 60 | <!-- End Google Tag Manager --> |
||||||
| 61 | HTML; |
||||||
| 62 | |||||||
| 63 | $dataLayerVariable = data_layer_variable(); |
||||||
| 64 | $containerId = container_id(); |
||||||
| 65 | |||||||
| 66 | echo sprintf( |
||||||
| 67 | $snippet, |
||||||
| 68 | esc_js($dataLayerVariable), |
||||||
|
0 ignored issues
–
show
The function
esc_js 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
Loading history...
|
|||||||
| 69 | esc_js($containerId) |
||||||
| 70 | ), "\n"; |
||||||
| 71 | } |
||||||
| 72 | |||||||
| 73 | /** |
||||||
| 74 | * Display the body snippet. |
||||||
| 75 | * |
||||||
| 76 | * @return void |
||||||
| 77 | */ |
||||||
| 78 | public function body(): void |
||||||
| 79 | { |
||||||
| 80 | $snippet = <<<HTML |
||||||
| 81 | <!-- Google Tag Manager (noscript) --> |
||||||
| 82 | <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=%s" |
||||||
| 83 | height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> |
||||||
| 84 | <!-- End Google Tag Manager (noscript) --> |
||||||
| 85 | HTML; |
||||||
| 86 | |||||||
| 87 | $containerId = container_id(); |
||||||
| 88 | |||||||
| 89 | echo sprintf($snippet, esc_js($containerId)), "\n"; |
||||||
|
0 ignored issues
–
show
The function
esc_js 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
Loading history...
|
|||||||
| 90 | } |
||||||
| 91 | } |
||||||
| 92 |