Skip to content

Commit

Permalink
Merge pull request #19 from alleyinteractive/feature/gtm
Browse files Browse the repository at this point in the history
Add feature for GTM script
  • Loading branch information
dlh01 authored May 29, 2024
2 parents a67070f + 9772d69 commit 5a225b5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C

Nothing yet.

## 2.2.0

### Added

- `GTM_Script` feature.

## 2.1.0

### Changed
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
81 changes: 81 additions & 0 deletions src/alley/wp/features/class-gtm-script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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.
*
* @phpstan-param array<string, mixed> $data_layer
*
* @param string $tag_id GTM tag ID.
* @param array|stdClass|JsonSerializable $data_layer Initial data layer data.
*/
public function __construct(
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 5a225b5

Please sign in to comment.