Skip to content

Commit 60ff0d9

Browse files
authored
Merge pull request #276 from plausible/track_query_params
Added: Track Query Parameters option to Enhanced Measurements
2 parents 5544dab + eb0c6ff commit 60ff0d9

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/Actions.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,33 @@ public function maybe_register_assets() {
130130
);
131131
}
132132

133+
// Track query parameters (if enabled and set)
134+
if ( Helpers::is_enhanced_measurement_enabled( 'query-params' ) ) {
135+
$query_params = Helpers::get_settings()['query_params'] ?? [];
136+
$props = [];
137+
138+
foreach ( $query_params as $query_param ) {
139+
if ( isset( $_REQUEST[ $query_param ] ) ) {
140+
$props[ $query_param ] = $_REQUEST[ $query_param ];
141+
}
142+
}
143+
144+
if ( ! empty( $props ) ) {
145+
$data = wp_json_encode(
146+
[
147+
'props' => $props,
148+
]
149+
);
150+
151+
$script = "plausible('WP Query Parameters', $data );";
152+
153+
wp_add_inline_script(
154+
'plausible-analytics',
155+
"document.addEventListener('DOMContentLoaded', function () {\n$script\n});"
156+
);
157+
}
158+
}
159+
133160
// Track search results. Tracks a search event with the search term and the number of results, and a pageview with the site's search URL.
134161
if ( Helpers::is_enhanced_measurement_enabled( 'search' ) && is_search() ) {
135162
global $wp_query;
@@ -138,7 +165,7 @@ public function maybe_register_assets() {
138165
$data = wp_json_encode(
139166
[
140167
'props' => [
141-
// convert queries to lowercase and remove trailing whitespace to ensure same terms are grouped together
168+
// convert queries to lowercase and remove trailing whitespace to ensure the same terms are grouped together
142169
'search_query' => strtolower( trim( get_search_query() ) ),
143170
'result_count' => $wp_query->found_posts,
144171
'search_source' => $search_source,
@@ -149,7 +176,7 @@ public function maybe_register_assets() {
149176

150177
wp_add_inline_script(
151178
'plausible-analytics',
152-
"document.addEventListener('DOMContentLoaded', function() {\n$script\n});"
179+
"document.addEventListener('DOMContentLoaded', function () {\n$script\n});"
153180
);
154181
}
155182

src/Admin/Provisioning.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function __construct( $client = null ) {
9191
'file-downloads' => __( 'File Download', 'plausible-analytics' ),
9292
'form-completions' => __( 'WP Form Completions', 'plausible-analytics' ),
9393
'outbound-links' => __( 'Outbound Link: Click', 'plausible-analytics' ),
94+
'query-params' => __( 'WP Query Parameters', 'plausible-analytics' ),
9495
'search' => __( 'WP Search Queries', 'plausible-analytics' ),
9596
];
9697

@@ -334,7 +335,8 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
334335

335336
if ( ! Helpers::is_enhanced_measurement_enabled( 'pageview-props', $enhanced_measurements ) &&
336337
! Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) &&
337-
! Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) ) {
338+
! Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) &&
339+
! Helpers::is_enhanced_measurement_enabled( 'query-params', $enhanced_measurements ) ) {
338340
return; // @codeCoverageIgnore
339341
}
340342

@@ -359,6 +361,15 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
359361
}
360362
}
361363

364+
/**
365+
* Create Custom Properties for Query Parameters option.
366+
*/
367+
if ( Helpers::is_enhanced_measurement_enabled( 'query-params', $enhanced_measurements ) ) {
368+
foreach ( Helpers::get_settings()['query_params'] ?? [] as $query_param ) {
369+
$properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $query_param ] ] );
370+
}
371+
}
372+
362373
/**
363374
* Create Custom Properties for Search Queries option.
364375
*/

src/Admin/Settings/Page.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ public function __construct() {
221221
'value' => 'user-logged-in',
222222
'caps' => [ self::CAP_PROPS ],
223223
],
224+
'query-params' => [
225+
'label' => esc_html__( 'Query parameters', 'plausible-analytics' ),
226+
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-custom-query-parameters',
227+
'slug' => 'enhanced_measurements',
228+
'type' => 'checkbox',
229+
'value' => 'query-params',
230+
'addtl_opts' => true,
231+
'caps' => [ self::CAP_PROPS ],
232+
],
233+
'query-params-patterns' => [
234+
'slug' => 'query_params',
235+
'description' => sprintf(
236+
__(
237+
'Enter the query parameters you\'d like to track. E.g. enter <strong>lang</strong> if you want to track <code>%s</code>.',
238+
'plausible-analytics'
239+
),
240+
get_home_url() . '?lang=en'
241+
),
242+
'type' => 'clonable_text',
243+
'value' => Helpers::get_settings()['query_params'] ?? [],
244+
'hidden' => ! Helpers::is_enhanced_measurement_enabled( 'query-params' ),
245+
],
224246
'search' => [
225247
'label' => esc_html__( 'Search queries', 'plausible-analytics' ),
226248
'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-site-search-tracking',

0 commit comments

Comments
 (0)