Skip to content

Commit

Permalink
Ensure the consent mode defaults are always output regardless of whet…
Browse files Browse the repository at this point in the history
…her there are tags.
  • Loading branch information
techanvil committed Feb 20, 2024
1 parent 9b49510 commit f7b11c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
30 changes: 17 additions & 13 deletions includes/Core/Consent_Mode/Consent_Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ class Consent_Mode {
use Method_Proxy_Trait;

public function register() {
add_action( 'googlesitekit_setup_gtag', $this->get_method_proxy( 'setup_gtag' ) );
}
$consent_defaults = array(
'ad_storage' => 'denied',
'ad_personalization' => 'denied',
);

protected function setup_gtag( $gtag ) {
$gtag->add_command(
'consent',
array(
'default',
array(
'ad_storage' => 'denied',
'ad_personalization' => 'denied',
),
),
'before' // Ensure the consent command is inlined before the gtag.js script.
add_action(
'wp_head',
function() use ( $consent_defaults ) {
?>
<!-- <?php echo esc_html__( 'Google tag (gtag.js) snippet added by Site Kit', 'google-site-kit' ); ?> -->
<script id='google_gtagjs-js-consent-default'>
window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}
gtag("consent","default", <?php echo wp_json_encode( $consent_defaults ); ?>);
</script>
<!-- <?php echo esc_html__( 'End Google tag (gtag.js) snippet added by Site Kit', 'google-site-kit' ); ?> -->
<?php
},
1
);
}
}
15 changes: 7 additions & 8 deletions includes/Core/Tags/GTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public function add_tag( $tag_id, $config = array() ) {
);
}

public function add_command( $command, $parameters, $position = 'after' ) {
public function add_command( $command, $parameters ) {
$this->commands[] = array(
'command' => $command, // e.g. 'config', 'event', etc.
'parameters' => $parameters, // e.g. array( 'send_to', 'AW-123456789' )
'position' => $position, // e.g. 'after', 'before'. This determines the position of the inline script relative to the gtag.js script.
'command' => $command, // e.g. 'config', 'event', etc.
'parameters' => $parameters, // e.g. array( 'send_to', 'AW-123456789' )
);
}

Expand All @@ -60,8 +59,8 @@ protected function enqueue_gtag_script() {
wp_enqueue_script( self::HANDLE, $gtag_src, false, null, false );
wp_script_add_data( self::HANDLE, 'script_execution', 'async' );

// Pass $position = 'before' to ensure the dataLayer is output before the gtag.js script, primarily to facilitate configuring consent mode defaults.
wp_add_inline_script( self::HANDLE, 'window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}', 'before' );
// Note that `gtag()` may already be defined via the `Consent_Mode` output, but this is safe to call multiple times.
wp_add_inline_script( self::HANDLE, 'window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}' );
wp_add_inline_script( self::HANDLE, 'gtag("js", new Date());' );
wp_add_inline_script( self::HANDLE, 'gtag("set", "developer_id.dZTNiMT", true);' ); // Site Kit developer ID.

Expand All @@ -70,15 +69,15 @@ protected function enqueue_gtag_script() {
}

foreach ( $this->commands as $command ) {
wp_add_inline_script( self::HANDLE, $this->get_gtag_call_for_command( $command ), $command['position'] );
wp_add_inline_script( self::HANDLE, $this->get_gtag_call_for_command( $command ) );
}

$filter_google_gtagjs = function ( $tag, $handle ) {
if ( self::HANDLE !== $handle ) {
return $tag;
}

$snippet_comment_begin = sprintf( "\n<!-- %s -->\n", esc_html__( 'Google tag (gtag.js)snippet added by Site Kit', 'google-site-kit' ) );
$snippet_comment_begin = sprintf( "\n<!-- %s -->\n", esc_html__( 'Google tag (gtag.js) snippet added by Site Kit', 'google-site-kit' ) );
$snippet_comment_end = sprintf( "\n<!-- %s -->\n", esc_html__( 'End Google tag (gtag.js) snippet added by Site Kit', 'google-site-kit' ) );

return $snippet_comment_begin . $tag . $snippet_comment_end;
Expand Down

0 comments on commit f7b11c4

Please sign in to comment.