@@ -35,6 +35,11 @@ class Provisioning {
35
35
'category ' ,
36
36
];
37
37
38
+ private $ custom_search_properties = [
39
+ 'search_query ' ,
40
+ 'result_count ' ,
41
+ ];
42
+
38
43
/**
39
44
* Build class.
40
45
*
@@ -63,6 +68,7 @@ public function __construct( $client = null ) {
63
68
'404 ' => __ ( '404 ' , 'plausible-analytics ' ),
64
69
'outbound-links ' => __ ( 'Outbound Link: Click ' , 'plausible-analytics ' ),
65
70
'file-downloads ' => __ ( 'File Download ' , 'plausible-analytics ' ),
71
+ 'search ' => __ ( 'Search ' , 'plausible-analytics ' ),
66
72
];
67
73
68
74
$ this ->init ();
@@ -140,6 +146,13 @@ public function maybe_create_goals( $old_settings, $settings ) {
140
146
}
141
147
142
148
$ goals [] = $ this ->create_request_custom_event ( $ this ->custom_event_goals [ $ measurement ] );
149
+
150
+ if ( $ measurement === 'search ' ) {
151
+ global $ wp_rewrite ;
152
+
153
+ $ search_url = str_replace ( '%search% ' , '' , $ wp_rewrite ->get_search_permastruct () );
154
+ $ goals [] = $ this ->create_request_custom_event ( null , 'Pageview ' , '' , $ search_url );
155
+ }
143
156
}
144
157
145
158
$ this ->create_goals ( $ goals );
@@ -152,7 +165,7 @@ public function maybe_create_goals( $old_settings, $settings ) {
152
165
*
153
166
* @return GoalCreateRequestCustomEvent
154
167
*/
155
- private function create_request_custom_event ( $ name , $ type = 'CustomEvent ' , $ currency = '' ) {
168
+ private function create_request_custom_event ( $ name , $ type = 'CustomEvent ' , $ currency = '' , $ path = '' ) {
156
169
$ props = [
157
170
'goal ' => [
158
171
'event_name ' => $ name ,
@@ -164,6 +177,12 @@ private function create_request_custom_event( $name, $type = 'CustomEvent', $cur
164
177
$ props [ 'goal ' ][ 'currency ' ] = $ currency ;
165
178
}
166
179
180
+ if ( $ type === 'Pageview ' ) {
181
+ unset( $ props [ 'goal ' ][ 'event_name ' ] );
182
+
183
+ $ props [ 'goal ' ][ 'path ' ] = $ path ;
184
+ }
185
+
167
186
return new Client \Model \GoalCreateRequestCustomEvent ( $ props );
168
187
}
169
188
@@ -267,7 +286,8 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
267
286
$ enhanced_measurements = $ settings [ 'enhanced_measurements ' ];
268
287
269
288
if ( ! Helpers::is_enhanced_measurement_enabled ( 'pageview-props ' , $ enhanced_measurements ) &&
270
- ! Helpers::is_enhanced_measurement_enabled ( 'revenue ' , $ enhanced_measurements ) ) {
289
+ ! Helpers::is_enhanced_measurement_enabled ( 'revenue ' , $ enhanced_measurements ) &&
290
+ ! Helpers::is_enhanced_measurement_enabled ( 'search ' , $ enhanced_measurements ) ) {
271
291
return ; // @codeCoverageIgnore
272
292
}
273
293
@@ -292,6 +312,15 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
292
312
}
293
313
}
294
314
315
+ /**
316
+ * Create Custom Properties for Search Queries option.
317
+ */
318
+ if ( Helpers::is_enhanced_measurement_enabled ( 'search ' , $ enhanced_measurements ) ) {
319
+ foreach ( $ this ->custom_search_properties as $ property ) {
320
+ $ properties [] = new Client \Model \CustomProp ( [ 'custom_prop ' => [ 'key ' => $ property ] ] );
321
+ }
322
+ }
323
+
295
324
$ create_request ->setCustomProps ( $ properties );
296
325
297
326
$ this ->client ->enable_custom_property ( $ create_request );
0 commit comments