diff --git a/readme.txt b/readme.txt index 3b7cfc49..7a0490c3 100644 --- a/readme.txt +++ b/readme.txt @@ -278,6 +278,10 @@ Please make sure you make a backup of your database before updating any version == Changelog == += 2.6.5 = +* Fixed: the user_logged_in property followed the "Authors and categories" option instead of "Logged-in user status". +* Fixed: the user_logged_in value for logged-out visitors is no longer translated, so it's always "no". + = 2.6.4 = * Improved: the proxy now also forwards the visitor's Accept-Language and User-Agent Client Hints (Sec-CH-UA*) headers to Plausible Analytics, so its bot detection works for proxied requests, too. * Fixed: on sites using WPML (or another multilingual plugin) with a directory per language, the Domain Name could be pre-filled with the language directory included (e.g. example.com/fr), which made the Plugin Token show as invalid. diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 72afb5b3..e0ef1df5 100644 --- a/src/Admin/Provisioning.php +++ b/src/Admin/Provisioning.php @@ -68,7 +68,6 @@ class Provisioning { private $custom_pageview_properties = [ 'author', 'category', - 'user_logged_in', ]; /** * @var string[] $custom_search_properties @@ -673,6 +672,7 @@ public function maybe_create_custom_properties( $_, $settings ) { $query_params = $settings['query_params'] ?? []; if ( ! EnhancedMeasurements::is_enabled( EnhancedMeasurements::PAGEVIEW_PROPS, $enhanced_measurements ) && + ! EnhancedMeasurements::is_enabled( EnhancedMeasurements::LOGGED_IN_USER_STATUS, $enhanced_measurements ) && ! EnhancedMeasurements::is_enabled( EnhancedMeasurements::ECOMMERCE_REVENUE, $enhanced_measurements ) && ! EnhancedMeasurements::is_enabled( EnhancedMeasurements::SEARCH_QUERIES, $enhanced_measurements ) && empty( $query_params ) ) { @@ -691,6 +691,13 @@ public function maybe_create_custom_properties( $_, $settings ) { } } + /** + * Enable the Custom Property for the Logged-in user status option. + */ + if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::LOGGED_IN_USER_STATUS, $enhanced_measurements ) ) { + $properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => 'user_logged_in' ] ] ); + } + /** * Create Custom Properties for WooCommerce integration. */ diff --git a/src/Admin/Upgrades.php b/src/Admin/Upgrades.php index dbe6ae07..b7263874 100644 --- a/src/Admin/Upgrades.php +++ b/src/Admin/Upgrades.php @@ -110,6 +110,10 @@ public function run() { $this->upgrade_to_262(); } + if ( version_compare( $plausible_analytics_version, '2.6.5', '<' ) ) { + $this->upgrade_to_265(); + } + // Add required upgrade routines for future versions here. } @@ -493,6 +497,35 @@ public function upgrade_to_262() { update_option( 'plausible_analytics_version', '2.6.2' ); } + /** + * Upgrade routine for 2.6.5 + * + * Enables the user_logged_in Custom Property if Logged-in user status is enabled. + * + * @since v2.6.5 + * + * @return void + * + * @codeCoverageIgnore because all we'd be doing is testing the Plugins API. + */ + public function upgrade_to_265() { + /** + * Skipped on AJAX requests, where a failing API call replaces the response with an error. Runs on the next regular + * admin request instead. + * + * @see Client::send_json_error() + */ + if ( wp_doing_ajax() ) { + return; + } + + if ( EnhancedMeasurements::is_enabled( EnhancedMeasurements::LOGGED_IN_USER_STATUS ) ) { + ( new Provisioning() )->maybe_create_custom_properties( [], Helpers::get_settings() ); + } + + update_option( 'plausible_analytics_version', '2.6.5' ); + } + /** * Display a notice to CE users that haven't entered an API token yet. * diff --git a/src/InitOptions.php b/src/InitOptions.php index e6f0de70..4e3f3e73 100644 --- a/src/InitOptions.php +++ b/src/InitOptions.php @@ -176,11 +176,12 @@ private function url_matches_patterns( $url, $patterns ) { public function maybe_track_logged_in_users( $options = [] ) { $settings = Helpers::get_settings(); - if ( ! is_array( $settings['enhanced_measurements'] ) || ! in_array( EnhancedMeasurements::PAGEVIEW_PROPS, $settings['enhanced_measurements'] ) ) { + if ( ! is_array( $settings['enhanced_measurements'] ) || ! in_array( EnhancedMeasurements::LOGGED_IN_USER_STATUS, $settings['enhanced_measurements'] ) ) { return $options; // @codeCoverageIgnore } - $logged_in = _x( 'no', 'Value when user is not logged in.', 'plausible-analytics' ); + // Not translated, so the value is the same in every language. + $logged_in = 'no'; if ( is_user_logged_in() ) { $user = wp_get_current_user(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 49e2b12a..b6b4e6bf 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -173,6 +173,19 @@ public function enablePageviewProps( $settings ) { return $settings; } + /** + * Enable Logged-in user status. + * + * @param $settings + * + * @return mixed + */ + public function enableLoggedInUserStatus( $settings ) { + $settings['enhanced_measurements'] = [ 'user-logged-in' ]; + + return $settings; + } + /** * Enable the proxy. * diff --git a/tests/integration/InitOptionsTest.php b/tests/integration/InitOptionsTest.php index 6413038b..13b76cd2 100644 --- a/tests/integration/InitOptionsTest.php +++ b/tests/integration/InitOptionsTest.php @@ -147,16 +147,31 @@ public function testExcludePageview() { * @return void */ public function testTrackLoggedInUsers() { + // The logged-out value must not be translated, whatever the site's language. + $translate = function ( $translation, $text ) { + return $text === 'no' ? 'non' : $translation; + }; + try { /** @var InitOptions $class */ $class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [] )->getMock(); + // Authors and categories doesn't control the user_logged_in property. add_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] ); $params = $class->maybe_track_logged_in_users(); + $this->assertArrayNotHasKey( 'customProperties', $params ); + + remove_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] ); + add_filter( 'plausible_analytics_settings', [ $this, 'enableLoggedInUserStatus' ] ); + + add_filter( 'gettext_with_context', $translate, 10, 2 ); + + $params = $class->maybe_track_logged_in_users(); + $this->assertArrayHasKey( 'customProperties', $params ); - $this->assertArrayHasKey( 'user_logged_in', $params['customProperties'] ); + $this->assertEquals( 'no', $params['customProperties']['user_logged_in'] ); global $current_user; @@ -172,6 +187,8 @@ public function testTrackLoggedInUsers() { $this->assertEquals( 'test', $params['customProperties']['user_logged_in'] ); } finally { remove_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] ); + remove_filter( 'plausible_analytics_settings', [ $this, 'enableLoggedInUserStatus' ] ); + remove_filter( 'gettext_with_context', $translate ); // Don't leave the test user logged in for the tests that follow. wp_set_current_user( 0 ); }