Skip to content

Commit

Permalink
Merge pull request #8310 from google/bug/8309-fix-incorrect-opt-out-s…
Browse files Browse the repository at this point in the history
…nippet-rendering

Bug/8309 fix incorrect opt out snippet rendering
  • Loading branch information
tofumatt authored Feb 23, 2024
2 parents 1a5240e + 583ad68 commit 3b92666
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 5 additions & 5 deletions includes/Modules/Analytics_4.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,16 +636,16 @@ private function print_tracking_opt_out() {
* @return bool
*/
protected function is_tracking_disabled() {
$settings = $this->get_settings()->get();
// @TODO Revert this when we use the new GA4 SettingsEdit form once #7932 is merged and we save all settings to the Analytics-4 module.
$settings = ( new Analytics( $this->context ) )->get_settings()->get();

// This filter is documented in Tag_Manager::filter_analytics_allow_tracking_disabled.
if ( ! apply_filters( 'googlesitekit_allow_tracking_disabled', $settings['useSnippet'] ) ) {
return false;
}

$option = $this->get_settings()->get();

$disable_logged_in_users = in_array( 'loggedinUsers', $option['trackingDisabled'], true ) && is_user_logged_in();
$disable_content_creators = in_array( 'contentCreators', $option['trackingDisabled'], true ) && current_user_can( 'edit_posts' );
$disable_logged_in_users = in_array( 'loggedinUsers', $settings['trackingDisabled'], true ) && is_user_logged_in();
$disable_content_creators = in_array( 'contentCreators', $settings['trackingDisabled'], true ) && current_user_can( 'edit_posts' );

$disabled = $disable_logged_in_users || $disable_content_creators;

Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/integration/Modules/Analytics_4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,10 @@ public function test_tracking_opt_out_snippet( $settings, $logged_in, $is_tracki
$analytics = new Analytics_4( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) );
$analytics->get_settings()->set( $settings );

// TODO Remove this when #7932 and #8082 are merged which save and migrate the new GA4 settings.
// This saves the trackingDisabled setting to the Analytics module which is being used temporarily.
( new Analytics( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) )->get_settings()->merge( array( 'trackingDisabled' => $settings['trackingDisabled'] ) );

remove_all_actions( 'template_redirect' );
$analytics->register();
do_action( 'template_redirect' );
Expand Down

0 comments on commit 3b92666

Please sign in to comment.