Skip to content

Commit a724efd

Browse files
authored
Merge pull request #257 from plausible/track_logged_in_users
Added: Logged-in user status option
2 parents 2438940 + 5c0855c commit a724efd

File tree

5 files changed

+100
-36
lines changed

5 files changed

+100
-36
lines changed

src/Admin/Provisioning.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Provisioning {
5757
private $custom_pageview_properties = [
5858
'author',
5959
'category',
60+
'user_logged_in',
6061
];
6162

6263
/**

src/Admin/Settings/Page.php

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,6 @@ public function __construct() {
151151
'value' => '404',
152152
'caps' => [ self::CAP_GOALS ],
153153
],
154-
'outbound-links' => [
155-
'label' => esc_html__( 'Outbound links', 'plausible-analytics' ),
156-
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-external-link-clicks',
157-
'slug' => 'enhanced_measurements',
158-
'type' => 'checkbox',
159-
'value' => 'outbound-links',
160-
'caps' => [ self::CAP_GOALS ],
161-
],
162154
'file-downloads' => [
163155
'label' => esc_html__( 'File downloads', 'plausible-analytics' ),
164156
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-file-downloads',
@@ -167,21 +159,21 @@ public function __construct() {
167159
'value' => 'file-downloads',
168160
'caps' => [ self::CAP_GOALS ],
169161
],
170-
'search' => [
171-
'label' => esc_html__( 'Search queries', 'plausible-analytics' ),
172-
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-site-search-tracking',
162+
'outbound-links' => [
163+
'label' => esc_html__( 'Outbound links', 'plausible-analytics' ),
164+
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-external-link-clicks',
173165
'slug' => 'enhanced_measurements',
174166
'type' => 'checkbox',
175-
'value' => 'search',
167+
'value' => 'outbound-links',
176168
'caps' => [ self::CAP_GOALS ],
177169
],
178-
'tagged-events' => [
179-
'label' => esc_html__( 'Custom events', 'plausible-analytics' ),
180-
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-setup-custom-events-to-track-goal-conversions',
170+
'pageview-props' => [
171+
'label' => esc_html__( 'Authors and categories', 'plausible-analytics' ),
172+
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-send-custom-properties',
181173
'slug' => 'enhanced_measurements',
182174
'type' => 'checkbox',
183-
'value' => 'tagged-events',
184-
'caps' => [ self::CAP_GOALS ],
175+
'value' => 'pageview-props',
176+
'caps' => [ self::CAP_PROPS ],
185177
],
186178
'revenue' => [
187179
'label' => esc_html__( 'Ecommerce revenue', 'plausible-analytics' ),
@@ -191,14 +183,6 @@ public function __construct() {
191183
'value' => 'revenue',
192184
'caps' => [ self::CAP_GOALS, self::CAP_FUNNELS, self::CAP_PROPS, self::CAP_REVENUE ],
193185
],
194-
'pageview-props' => [
195-
'label' => esc_html__( 'Authors and categories', 'plausible-analytics' ),
196-
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-send-custom-properties',
197-
'slug' => 'enhanced_measurements',
198-
'type' => 'checkbox',
199-
'value' => 'pageview-props',
200-
'caps' => [ self::CAP_PROPS ],
201-
],
202186
'form-completions' => [
203187
'label' => esc_html__( 'Form completions', 'plausible-analytics' ),
204188
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-form-completions',
@@ -207,6 +191,30 @@ public function __construct() {
207191
'value' => 'form-completions',
208192
'caps' => [ self::CAP_GOALS ],
209193
],
194+
'user-logged-in' => [
195+
'label' => esc_html__( 'Logged-in user status', 'plausible-analytics' ),
196+
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-logged-in-user-status',
197+
'slug' => 'enhanced_measurements',
198+
'type' => 'checkbox',
199+
'value' => 'user-logged-in',
200+
'caps' => [ self::CAP_PROPS ],
201+
],
202+
'search' => [
203+
'label' => esc_html__( 'Search queries', 'plausible-analytics' ),
204+
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-site-search-tracking',
205+
'slug' => 'enhanced_measurements',
206+
'type' => 'checkbox',
207+
'value' => 'search',
208+
'caps' => [ self::CAP_GOALS ],
209+
],
210+
'tagged-events' => [
211+
'label' => esc_html__( 'Custom events', 'plausible-analytics' ),
212+
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-setup-custom-events-to-track-goal-conversions',
213+
'slug' => 'enhanced_measurements',
214+
'type' => 'checkbox',
215+
'value' => 'tagged-events',
216+
'caps' => [ self::CAP_GOALS ],
217+
],
210218
'hash' => [
211219
'label' => esc_html__( 'Hash-based routing', 'plausible-analytics' ),
212220
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-hash-based-url-tracking',
@@ -434,7 +442,7 @@ public function __construct() {
434442
if ( ! empty( $settings[ 'self_hosted_domain' ] ) ) {
435443
$fields = $this->fields[ 'general' ][ 1 ][ 'fields' ];
436444

437-
array_splice( $fields, 6, 0, self::OPTION_NOT_AVAILABLE_IN_CE_HOOK );
445+
array_splice( $fields, 5, 0, self::OPTION_NOT_AVAILABLE_IN_CE_HOOK );
438446

439447
$this->fields[ 'general' ][ 1 ][ 'fields' ] = $fields;
440448
}

src/Filters.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
namespace Plausible\Analytics\WP;
1111

1212
use WP_Term;
13-
use Exception;
14-
15-
// Bailout, if accessed directly.
16-
if ( ! defined( 'ABSPATH' ) ) {
17-
exit;
18-
}
1913

2014
class Filters {
2115
/**
@@ -27,7 +21,8 @@ class Filters {
2721
*/
2822
public function __construct() {
2923
add_filter( 'script_loader_tag', [ $this, 'add_plausible_attributes' ], 10, 2 );
30-
add_filter( 'plausible_analytics_script_params', [ $this, 'maybe_add_custom_params' ] );
24+
add_filter( 'plausible_analytics_script_params', [ $this, 'maybe_add_pageview_props' ] );
25+
add_filter( 'plausible_analytics_script_params', [ $this, 'maybe_track_logged_in_users' ] );
3126
}
3227

3328
/**
@@ -79,7 +74,7 @@ public function add_plausible_attributes( $tag, $handle ) {
7974
*
8075
* @return mixed|void
8176
*/
82-
public function maybe_add_custom_params( $params ) {
77+
public function maybe_add_pageview_props( $params ) {
8378
$settings = Helpers::get_settings();
8479

8580
if ( ! is_array( $settings[ 'enhanced_measurements' ] ) || ! in_array( 'pageview-props', $settings[ 'enhanced_measurements' ] ) ) {
@@ -120,6 +115,38 @@ public function maybe_add_custom_params( $params ) {
120115
}
121116
}
122117

123-
return apply_filters( 'plausible_analytics_pageview_properties', $params );
118+
return $params;
119+
}
120+
121+
/**
122+
* Adds custom parameter User Logged In if Custom Properties is enabled.
123+
*
124+
* @since v2.4.0
125+
*
126+
* @param $params
127+
*
128+
* @return mixed|string
129+
*/
130+
public function maybe_track_logged_in_users( $params ) {
131+
$settings = Helpers::get_settings();
132+
133+
if ( ! is_array( $settings[ 'enhanced_measurements' ] ) || ! in_array( 'pageview-props', $settings[ 'enhanced_measurements' ] ) ) {
134+
return $params; // @codeCoverageIgnore
135+
}
136+
137+
$logged_in = _x( 'no', __( 'Value when user is not logged in.', 'plausible-analytics' ), 'plausible-analytics' );
138+
139+
if ( is_user_logged_in() ) {
140+
$user = wp_get_current_user();
141+
$roles = (array) $user->roles;
142+
143+
if ( ! empty( $roles ) ) {
144+
$logged_in = $roles[ 0 ];
145+
}
146+
}
147+
148+
$params .= " event-user_logged_in='$logged_in'";
149+
150+
return $params;
124151
}
125152
}

tests/integration/ActionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ActionsTest extends TestCase {
1515
/**
1616
* @see Actions::maybe_register_assets()
1717
* @see Filters::add_plausible_attributes()
18-
* @see Filters::maybe_add_custom_params()
18+
* @see Filters::maybe_add_pageview_props()
1919
* @return void
2020
* @throws \Exception
2121
*/

tests/integration/FiltersTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,32 @@ public function testAddPlausibleAttributes() {
2929

3030
$this->assertStringNotContainsString( 'plausible-analytics-js', $tag );
3131
}
32+
33+
/**
34+
* @see Filters::maybe_track_logged_in_users()
35+
*
36+
* @return void
37+
*/
38+
public function testTrackLoggedInUsers() {
39+
$class = new Filters();
40+
41+
add_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] );
42+
43+
$params = $class->maybe_track_logged_in_users( '' );
44+
45+
$this->assertStringContainsString( 'no', $params );
46+
47+
global $current_user;
48+
49+
$user = new \WP_User();
50+
$user->ID = 1;
51+
$user->roles = [ 'test' ];
52+
$current_user = $user;
53+
54+
$params = $class->maybe_track_logged_in_users( '' );
55+
56+
remove_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] );
57+
58+
$this->assertStringContainsString( 'test', $params );
59+
}
3260
}

0 commit comments

Comments
 (0)