Skip to content

Commit

Permalink
Add option to delay JavaScript execution
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Feb 6, 2024
1 parent eafe08e commit 2b02d24
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion assets/admin/settings.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => 'a1f5a87986f782e4c463');
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '0333fbdc4036c3034f0e');
2 changes: 1 addition & 1 deletion assets/admin/settings.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Release date: 2024-MM-DD
Find out about what's new in our [our release post](https://gtmkit.com/gtm-kit-1-19/).

#### Enhancements:
* Add option to fire a 'load_delayed_js' event, which can be used to delay JavaScript execution in Google Tag Manager.
* This release comes with many behind-the-scenes improvements and general enhancements.
* The script settings are the same on all pages and are now cached in the object cache for better performance.
* The function to share system data with the support team has been improved with more data.
Expand Down
21 changes: 21 additions & 0 deletions src/Frontend/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public static function register( Options $options ): void {
add_action( 'wp_head', [ $page, 'container_disabled' ] );
}

if ( $options->get( 'general', 'load_js_event' ) ) {
add_action( 'wp_enqueue_scripts', [ $page, 'enqueue_delay_js_script' ] );
}

if ( $noscript_implementation === '0' && $container_active ) {
add_action( 'wp_body_open', [ $page, 'get_body_script' ] );
} elseif ( $noscript_implementation === '1' && $container_active ) {
Expand Down Expand Up @@ -176,6 +180,18 @@ function (cb) {
wp_add_inline_script( 'gtmkit-container', $script );
}

/**
* This script fires the 'delay_js' event in Google Tag Manager
*/
public function enqueue_delay_js_script(): void {

$script = esc_attr( $this->datalayer_name ) . '.push({"event" : "load_delayed_js"});' . "\n";

wp_register_script( 'gtmkit-delay', '', [ 'gtmkit-container' ], GTMKIT_VERSION, [ 'strategy' => 'defer' ] );
wp_enqueue_script( 'gtmkit-delay' );
wp_add_inline_script( 'gtmkit-delay', $script, 'before' );
}

/**
* Get GTM script
*
Expand Down Expand Up @@ -212,6 +228,11 @@ public function get_gtm_script( string $gtm_id ): void {
*/
public function set_inline_script_attributes( array $attributes, string $script ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
if ( isset( $attributes['id'] ) && strpos( $attributes['id'], 'gtmkit-' ) === 0 ) {

if ( strpos( $attributes['id'], 'gtmkit-delay' ) === 0 ) {
return $attributes;
}

$script_attributes = apply_filters(
'gtmkit_header_script_attributes',
[
Expand Down

0 comments on commit 2b02d24

Please sign in to comment.