Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 8 additions & 1 deletion src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class Provisioning {
private $custom_pageview_properties = [
'author',
'category',
'user_logged_in',
];
/**
* @var string[] $custom_search_properties
Expand Down Expand Up @@ -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 ) ) {
Expand All @@ -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.
*/
Expand Down
33 changes: 33 additions & 0 deletions src/Admin/Upgrades.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down Expand Up @@ -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' );
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
Dan0sz marked this conversation as resolved.
Resolved
}

/**
* Display a notice to CE users that haven't entered an API token yet.
*
Expand Down
5 changes: 3 additions & 2 deletions src/InitOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
19 changes: 18 additions & 1 deletion tests/integration/InitOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 );
}
Expand Down
Loading