diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 787741f..ff4a9fd 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/Admin/Settings/Page.php b/src/Admin/Settings/Page.php index 416d336..0b64506 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; } diff --git a/src/Filters.php b/src/Filters.php index ac79eb2..57d6016 100644 --- a/src/Filters.php +++ b/src/Filters.php @@ -10,12 +10,6 @@ namespace Plausible\Analytics\WP; use WP_Term; -use Exception; - -// Bailout, if accessed directly. -if ( ! defined( 'ABSPATH' ) ) { - exit; -} class Filters { /** @@ -27,7 +21,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 +74,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 +115,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 484bf8c..95fe7db 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 */ diff --git a/tests/integration/FiltersTest.php b/tests/integration/FiltersTest.php index e448126..28cb87c 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 ); + } }