site-kit-wp: The plugin should not send any data if the wp_get_environment_type() is detected as anything other than production

https://make.wordpress.org/core/2020/07/24/new-wp_get_environment_type-function-in-wordpress-5-5/ https://developer.wordpress.org/reference/functions/wp_get_environment_type/

No data should be sent to Google if the WP_ENVIRONMENT_TYPE system variable is not production.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • If the function wp_get_environment_type() exists (we should not polyfill this one) and it it returns anything other than “production”, all Site Kit tags (Analytics, AdSense, Tag Manager) that collect data should not be rendered.

Implementation Brief

  • create a new Tag_Production_Guard common tag guard class similar to Tag_Verify_Guard in the same Core/Tags/Guards location
  • in the can_activate method of the new class use the function wp_get_environment_type() to return TRUE if the environment is production and the function exists
  • also return TRUE if the function does not exist
  • update the register_tag methods used by Adsense, Analytics, Analytics 4 and Tag Manager to include the new Tag_Production_Guard. For example the Adsense code is in Modules/Adsense.php. The additional code will be something like $tag->use_guard( new Tag_Production_Guard() );

Test Coverage

Add tests to a new Core/Tags/Guards/Tag_Product_GuardTest file.

  • if the wp_get_environment_type() function does not exist test that TRUE is returned
  • if the functions does exist test what the tag guard returns based on whether WP_ENVIRONMENT_TYPE is set to production or not. The environment variable can be set using putenv:
    • putenv( 'WP_ENVIRONMENT_TYPE=development' ) sets it to development
    • putenv( 'WP_ENVIRONMENT_TYPE' ) unsets the variable

Visual Regression Changes

  • n/a

QA Brief

  • Take a look at Setting the environment type https://make.wordpress.org/core/2020/07/24/new-wp_get_environment_type-function-in-wordpress-5-5/
  • Enable any of the modules that enable tag guard (e.g. Adsense, Analytics, Analytics 4 and Tag Manager )
  • Set your environment type to local and save the page source for the home page of your site that end users see (e.g. http://sitekit.test) to local.txt
  • Change it back to production and save the page source for the home page to production.txt
  • Take a diff between the local.txt and production.txt, ensure that the local doesn’t contain the tags in the page source, example screenshot:
Screen Shot 2021-07-27 at 10 32 06 AM

Changelog entry

  • Add a new tag guard that prevents rendering tags for non-production environments.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 19 (3 by maintainers)

Most upvoted comments

Hi @andyblackwell 👋 just wanted to follow-up with you to let you know that the upcoming release (1.78.0) will include a new filter to control which environment types tags will be output for. See #4474 for details.

We do have environment specific configurations and safeguards as you eluded to that stem from the env that is set, so that isn’t an option.

@ivankruchkoff could you please add QAB?

@Hazlitte would it be possible to add an estimate here now the IB is completed please?

@felixarntz this sounds like a good idea and something we could probably implement as a common tag guard. What do you think?