Skip to content

Commit 8c36f00

Browse files
authored
Merge pull request #296 from plausible/fix_pageview_props
Fixed: only load author/category pageview props on posts.
2 parents 78cd864 + 09ac7f3 commit 8c36f00

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

src/InitOptions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class InitOptions {
1616
/**
1717
* Constructor.
1818
*
19-
* @return void
2019
* @since 1.0.0
2120
* @access public
21+
* @return void
2222
*/
2323
public function __construct() {
2424
add_filter( 'plausible_analytics_init_options', [ $this, 'maybe_add_pageview_props' ] );
@@ -43,7 +43,7 @@ public function maybe_add_pageview_props( $options = [] ) {
4343

4444
global $post;
4545

46-
if ( ! $post instanceof WP_Post ) {
46+
if ( ! $post instanceof WP_Post || ! is_single() ) {
4747
return $options; // @codeCoverageIgnore
4848
}
4949

@@ -164,11 +164,11 @@ private function url_matches_patterns( $url, $patterns ) {
164164
/**
165165
* Adds a custom parameter User Logged In if Custom Properties is enabled.
166166
*
167+
* @since v2.4.0
168+
*
167169
* @param $options
168170
*
169171
* @return array
170-
* @since v2.4.0
171-
*
172172
*/
173173
public function maybe_track_logged_in_users( $options = [] ) {
174174
$settings = Helpers::get_settings();

tests/integration/InitOptionsTest.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010

1111
class InitOptionsTest extends TestCase {
1212
/**
13-
* @return void
1413
* @see InitOptions::maybe_add_pageview_props()
14+
* @return void
1515
*/
1616
public function testAddPageviewProps() {
1717
try {
18-
global $post;
18+
global $post, $wp_query;
19+
$old_post = $post;
20+
$old_query = $wp_query;
1921

20-
$post_id = wp_insert_post(
22+
$post_id = wp_insert_post(
2123
[
2224
'id' => 1,
2325
'post_author' => 1,
2426
'post_title' => 'Test',
2527
'post_content' => 'Test',
2628
]
2729
);
28-
$test_post = get_post( $post_id );
29-
$post = $test_post;
30+
$test_post = get_post( $post_id );
31+
$post = $test_post;
32+
$wp_query = new \WP_Query();
33+
$wp_query->is_single = true;
3034

3135
add_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] );
3236

@@ -40,15 +44,16 @@ public function testAddPageviewProps() {
4044
$this->assertArrayHasKey( 'category', $options['customProperties'] );
4145
$this->assertEquals( 'Uncategorized', $options['customProperties']['category'] );
4246
} finally {
43-
$post = null;
47+
$post = $old_post;
48+
$wp_query = $old_query;
4449
remove_filter( 'plausible_analytics_settings', [ $this, 'enablePageviewProps' ] );
4550
}
4651
}
4752

4853
/**
54+
* @see InitOptions::maybe_add_proxy_options()
4955
* @return void
5056
* @throws \Exception
51-
* @see InitOptions::maybe_add_proxy_options()
5257
*/
5358
public function testAddProxyOptions() {
5459
try {
@@ -65,8 +70,8 @@ public function testAddProxyOptions() {
6570
}
6671

6772
/**
68-
* @return void
6973
* @see InitOptions::maybe_exclude_pageview()
74+
* @return void
7075
*/
7176
public function testExcludePageview() {
7277
/**
@@ -76,7 +81,7 @@ public function testExcludePageview() {
7681
add_filter( 'plausible_analytics_settings', [ $this, 'setExcludePageview' ] );
7782

7883
$class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [
79-
'get_current_request'
84+
'get_current_request',
8085
] )->getMock();
8186

8287
$class->method( 'get_current_request' )->willReturn( 'http://example.com/category/test' );
@@ -86,7 +91,7 @@ public function testExcludePageview() {
8691
$this->assertArrayNotHasKey( 'transformRequest', $options );
8792

8893
$class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [
89-
'get_current_request'
94+
'get_current_request',
9095
] )->getMock();
9196

9297
$class->method( 'get_current_request' )->willReturn( 'http://test.example.com/test' );
@@ -105,7 +110,7 @@ public function testExcludePageview() {
105110
add_filter( 'plausible_analytics_settings', [ $this, 'setExcludePageviewEdgeCaseAsterisk' ] );
106111

107112
$class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [
108-
'get_current_request'
113+
'get_current_request',
109114
] )->getMock();
110115

111116
$class->method( 'get_current_request' )->willReturn( 'http://example.com/test' );
@@ -124,7 +129,7 @@ public function testExcludePageview() {
124129
add_filter( 'plausible_analytics_settings', [ $this, 'setExcludePageviewEdgeCaseSpace' ] );
125130

126131
$class = $this->getMockBuilder( InitOptions::class )->disableOriginalConstructor()->onlyMethods( [
127-
'get_current_request'
132+
'get_current_request',
128133
] )->getMock();
129134

130135
$class->method( 'get_current_request' )->willReturn( 'http://example.com/test' );
@@ -138,8 +143,8 @@ public function testExcludePageview() {
138143
}
139144

140145
/**
141-
* @return void
142146
* @see InitOptions::maybe_track_logged_in_users()
147+
* @return void
143148
*/
144149
public function testTrackLoggedInUsers() {
145150
try {

0 commit comments

Comments
 (0)