From 9b278bdbe188e23edbfc6c785ec627509f3f0ec0 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:16:41 +0200 Subject: [PATCH 1/4] Added: user logged in/out status. --- src/Admin/Provisioning.php | 1 + src/Filters.php | 40 +++++++++++++++++++++++++++---- tests/integration/ActionsTest.php | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 787741f5..ff4a9fde 100644 --- a/src/Admin/Provisioning.php +++ b/src/Admin/Provisioning.php @@ -57,6 +57,7 @@ class Provisioning { private $custom_pageview_properties = [ 'author', 'category', + 'user_logged_in', ]; /** diff --git a/src/Filters.php b/src/Filters.php index ac79eb29..3d0b3a79 100644 --- a/src/Filters.php +++ b/src/Filters.php @@ -10,7 +10,6 @@ namespace Plausible\Analytics\WP; use WP_Term; -use Exception; // Bailout, if accessed directly. if ( ! defined( 'ABSPATH' ) ) { @@ -27,7 +26,8 @@ class Filters { */ public function __construct() { add_filter( 'script_loader_tag', [ $this, 'add_plausible_attributes' ], 10, 2 ); - add_filter( 'plausible_analytics_script_params', [ $this, 'maybe_add_custom_params' ] ); + add_filter( 'plausible_analytics_script_params', [ $this, 'maybe_add_pageview_props' ] ); + add_filter( 'plausible_analytics_script_params', [ $this, 'maybe_track_logged_in_users' ] ); } /** @@ -79,7 +79,7 @@ public function add_plausible_attributes( $tag, $handle ) { * * @return mixed|void */ - public function maybe_add_custom_params( $params ) { + public function maybe_add_pageview_props( $params ) { $settings = Helpers::get_settings(); if ( ! is_array( $settings[ 'enhanced_measurements' ] ) || ! in_array( 'pageview-props', $settings[ 'enhanced_measurements' ] ) ) { @@ -120,6 +120,38 @@ public function maybe_add_custom_params( $params ) { } } - return apply_filters( 'plausible_analytics_pageview_properties', $params ); + return $params; + } + + /** + * Adds custom parameter User Logged In if Custom Properties is enabled. + * + * @since v2.4.0 + * + * @param $params + * + * @return mixed|string + */ + public function maybe_track_logged_in_users( $params ) { + $settings = Helpers::get_settings(); + + if ( ! is_array( $settings[ 'enhanced_measurements' ] ) || ! in_array( 'pageview-props', $settings[ 'enhanced_measurements' ] ) ) { + return $params; // @codeCoverageIgnore + } + + $logged_in = _x( 'no', __( 'Value when user is not logged in.', 'plausible-analytics' ), 'plausible-analytics' ); + + if ( is_user_logged_in() ) { + $user = wp_get_current_user(); + $roles = (array) $user->roles; + + if ( ! empty( $roles ) ) { + $logged_in = $roles[ 0 ]; + } + } + + $params .= " event-user_logged_in='$logged_in'"; + + return $params; } } diff --git a/tests/integration/ActionsTest.php b/tests/integration/ActionsTest.php index 484bf8cd..95fe7dbf 100644 --- a/tests/integration/ActionsTest.php +++ b/tests/integration/ActionsTest.php @@ -15,7 +15,7 @@ class ActionsTest extends TestCase { /** * @see Actions::maybe_register_assets() * @see Filters::add_plausible_attributes() - * @see Filters::maybe_add_custom_params() + * @see Filters::maybe_add_pageview_props() * @return void * @throws \Exception */ From da9e4f0e9bade17d6c7ec85c594539026b67596e Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 25 Apr 2025 12:39:20 +0200 Subject: [PATCH 2/4] Sorted options (sort of alphabetically) in preparation for "Advanced" section. --- src/Admin/Settings/Page.php | 60 +++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/src/Admin/Settings/Page.php b/src/Admin/Settings/Page.php index 416d3367..0b645067 100644 --- a/src/Admin/Settings/Page.php +++ b/src/Admin/Settings/Page.php @@ -151,14 +151,6 @@ public function __construct() { 'value' => '404', 'caps' => [ self::CAP_GOALS ], ], - 'outbound-links' => [ - 'label' => esc_html__( 'Outbound links', 'plausible-analytics' ), - 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-external-link-clicks', - 'slug' => 'enhanced_measurements', - 'type' => 'checkbox', - 'value' => 'outbound-links', - 'caps' => [ self::CAP_GOALS ], - ], 'file-downloads' => [ 'label' => esc_html__( 'File downloads', 'plausible-analytics' ), 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-file-downloads', @@ -167,21 +159,21 @@ public function __construct() { 'value' => 'file-downloads', 'caps' => [ self::CAP_GOALS ], ], - 'search' => [ - 'label' => esc_html__( 'Search queries', 'plausible-analytics' ), - 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-site-search-tracking', + 'outbound-links' => [ + 'label' => esc_html__( 'Outbound links', 'plausible-analytics' ), + 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-external-link-clicks', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', - 'value' => 'search', + 'value' => 'outbound-links', 'caps' => [ self::CAP_GOALS ], ], - 'tagged-events' => [ - 'label' => esc_html__( 'Custom events', 'plausible-analytics' ), - 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-setup-custom-events-to-track-goal-conversions', + 'pageview-props' => [ + 'label' => esc_html__( 'Authors and categories', 'plausible-analytics' ), + 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-send-custom-properties', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', - 'value' => 'tagged-events', - 'caps' => [ self::CAP_GOALS ], + 'value' => 'pageview-props', + 'caps' => [ self::CAP_PROPS ], ], 'revenue' => [ 'label' => esc_html__( 'Ecommerce revenue', 'plausible-analytics' ), @@ -191,14 +183,6 @@ public function __construct() { 'value' => 'revenue', 'caps' => [ self::CAP_GOALS, self::CAP_FUNNELS, self::CAP_PROPS, self::CAP_REVENUE ], ], - 'pageview-props' => [ - 'label' => esc_html__( 'Authors and categories', 'plausible-analytics' ), - 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-send-custom-properties', - 'slug' => 'enhanced_measurements', - 'type' => 'checkbox', - 'value' => 'pageview-props', - 'caps' => [ self::CAP_PROPS ], - ], 'form-completions' => [ 'label' => esc_html__( 'Form completions', 'plausible-analytics' ), 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-form-completions', @@ -207,6 +191,30 @@ public function __construct() { 'value' => 'form-completions', 'caps' => [ self::CAP_GOALS ], ], + 'user-logged-in' => [ + 'label' => esc_html__( 'Logged-in user status', 'plausible-analytics' ), + 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-logged-in-user-status', + 'slug' => 'enhanced_measurements', + 'type' => 'checkbox', + 'value' => 'user-logged-in', + 'caps' => [ self::CAP_PROPS ], + ], + 'search' => [ + 'label' => esc_html__( 'Search queries', 'plausible-analytics' ), + 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-site-search-tracking', + 'slug' => 'enhanced_measurements', + 'type' => 'checkbox', + 'value' => 'search', + 'caps' => [ self::CAP_GOALS ], + ], + 'tagged-events' => [ + 'label' => esc_html__( 'Custom events', 'plausible-analytics' ), + 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-setup-custom-events-to-track-goal-conversions', + 'slug' => 'enhanced_measurements', + 'type' => 'checkbox', + 'value' => 'tagged-events', + 'caps' => [ self::CAP_GOALS ], + ], 'hash' => [ 'label' => esc_html__( 'Hash-based routing', 'plausible-analytics' ), 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-hash-based-url-tracking', @@ -434,7 +442,7 @@ public function __construct() { if ( ! empty( $settings[ 'self_hosted_domain' ] ) ) { $fields = $this->fields[ 'general' ][ 1 ][ 'fields' ]; - array_splice( $fields, 6, 0, self::OPTION_NOT_AVAILABLE_IN_CE_HOOK ); + array_splice( $fields, 5, 0, self::OPTION_NOT_AVAILABLE_IN_CE_HOOK ); $this->fields[ 'general' ][ 1 ][ 'fields' ] = $fields; } From 46f2f2e990564b49ea4a2c293dcfad3316003055 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Fri, 25 Apr 2025 13:23:13 +0200 Subject: [PATCH 3/4] Removed unnecessary ABSPATH check --- src/Filters.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Filters.php b/src/Filters.php index 3d0b3a79..57d60166 100644 --- a/src/Filters.php +++ b/src/Filters.php @@ -11,11 +11,6 @@ use WP_Term; -// Bailout, if accessed directly. -if ( ! defined( 'ABSPATH' ) ) { - exit; -} - class Filters { /** * Constructor. From 5c0855c61e2a0acd30672544a7d72b2e2ea69f2a Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Mon, 5 May 2025 13:19:39 +0200 Subject: [PATCH 4/4] Added testTrackLoggedInUsers. --- tests/integration/FiltersTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/integration/FiltersTest.php b/tests/integration/FiltersTest.php index e448126b..28cb87c9 100644 --- a/tests/integration/FiltersTest.php +++ b/tests/integration/FiltersTest.php @@ -29,4 +29,32 @@ public function testAddPlausibleAttributes() { $this->assertStringNotContainsString( 'plausible-analytics-js', $tag ); } + + /** + * @see Filters::maybe_track_logged_in_users() + * + * @return void + */ + public function testTrackLoggedInUsers() { + $class = new Filters(); + + add_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] ); + + $params = $class->maybe_track_logged_in_users( '' ); + + $this->assertStringContainsString( 'no', $params ); + + global $current_user; + + $user = new \WP_User(); + $user->ID = 1; + $user->roles = [ 'test' ]; + $current_user = $user; + + $params = $class->maybe_track_logged_in_users( '' ); + + remove_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] ); + + $this->assertStringContainsString( 'test', $params ); + } }