Skip to content

Commit

Permalink
Add feature for GTM script
Browse files Browse the repository at this point in the history
  • Loading branch information
dlh01 committed May 28, 2024
1 parent a67070f commit 30fbcd5
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C

## Unreleased

Nothing yet.
### Added

- `GTM_Script` feature.

## 2.1.0

Expand Down
1 change: 1 addition & 0 deletions docs/feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Feature {

- [Conditional_Feature](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-conditional-feature.php): Boot a feature only when a condition is met.
- [Group](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-group.php): Group related features.
- [GTM_Script](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-gtm-script.php): Add the standard Google Tag Manager script and data layer.
- [Lazy_Feature](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-lazy-feature.php): Instantiate a feature only when called upon.
- [Quick_Feature](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-quick-feature.php): Make a callable a feature.
- [Template_Feature](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-template-feature.php): Boot a feature only when templates load.
Expand Down
79 changes: 79 additions & 0 deletions src/alley/wp/features/class-gtm-script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* GTM_Script class file
*
* @package wp-type-extensions
*/

namespace Alley\WP\Features;

use Alley\WP\Types\Feature;
use JsonSerializable;
use stdClass;

/**
* Google Tag Manager script placement.
*/
final class GTM_Script implements Feature {
/**
* Constructor.
*
* @param string $tag_id GTM tag ID.
* @param array|stdClass|JsonSerializable $data_layer Initial data layer data.
*/
public function __construct(

Check failure on line 24 in src/alley/wp/features/class-gtm-script.php

View workflow job for this annotation

GitHub Actions / code-quality / phpstan PHP 8.1

Method Alley\WP\Features\GTM_Script::__construct() has parameter $data_layer with no value type specified in iterable type array.
private readonly string $tag_id,
private readonly array|stdClass|JsonSerializable $data_layer,
) {}

/**
* Boot the feature.
*/
public function boot(): void {
add_action( 'wp_head', [ $this, 'render_head' ] );
add_action( 'wp_body_open', [ $this, 'render_body' ] );
}

/**
* Render the GTM tag in the document body.
*/
public function render_head(): void {
$data = $this->data_layer instanceof JsonSerializable ? $this->data_layer : (object) $this->data_layer;
$flags = WP_DEBUG ? JSON_PRETTY_PRINT : 0;

printf(
<<<'HTML'
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push(%s);
</script>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer',%s);</script>
<!-- End Google Tag Manager -->
HTML,
wp_json_encode( $data, $flags ),
wp_json_encode( $this->tag_id, $flags ),
);
}

/**
* Render the GTM tag in the document body.
*/
public function render_body(): void {
printf(
<<<'HTML'
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="%s" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
HTML,
esc_url( "https://www.googletagmanager.com/ns.html?id={$this->tag_id}" ),
);
}
}

0 comments on commit 30fbcd5

Please sign in to comment.