diff --git a/src/Actions.php b/src/Actions.php index 36d8648..2e9dbf4 100644 --- a/src/Actions.php +++ b/src/Actions.php @@ -130,6 +130,33 @@ public function maybe_register_assets() { ); } + // Track query parameters (if enabled and set) + if ( Helpers::is_enhanced_measurement_enabled( 'query-params' ) ) { + $query_params = Helpers::get_settings()['query_params'] ?? []; + $props = []; + + foreach ( $query_params as $query_param ) { + if ( isset( $_REQUEST[ $query_param ] ) ) { + $props[ $query_param ] = $_REQUEST[ $query_param ]; + } + } + + if ( ! empty( $props ) ) { + $data = wp_json_encode( + [ + 'props' => $props, + ] + ); + + $script = "plausible('WP Query Parameters', $data );"; + + wp_add_inline_script( + 'plausible-analytics', + "document.addEventListener('DOMContentLoaded', function () {\n$script\n});" + ); + } + } + // 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. if ( Helpers::is_enhanced_measurement_enabled( 'search' ) && is_search() ) { global $wp_query; @@ -138,7 +165,7 @@ public function maybe_register_assets() { $data = wp_json_encode( [ 'props' => [ - // convert queries to lowercase and remove trailing whitespace to ensure same terms are grouped together + // convert queries to lowercase and remove trailing whitespace to ensure the same terms are grouped together 'search_query' => strtolower( trim( get_search_query() ) ), 'result_count' => $wp_query->found_posts, 'search_source' => $search_source, @@ -149,7 +176,7 @@ public function maybe_register_assets() { wp_add_inline_script( 'plausible-analytics', - "document.addEventListener('DOMContentLoaded', function() {\n$script\n});" + "document.addEventListener('DOMContentLoaded', function () {\n$script\n});" ); } diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 112889b..367f5be 100644 --- a/src/Admin/Provisioning.php +++ b/src/Admin/Provisioning.php @@ -91,6 +91,7 @@ public function __construct( $client = null ) { 'file-downloads' => __( 'File Download', 'plausible-analytics' ), 'form-completions' => __( 'WP Form Completions', 'plausible-analytics' ), 'outbound-links' => __( 'Outbound Link: Click', 'plausible-analytics' ), + 'query-params' => __( 'WP Query Parameters', 'plausible-analytics' ), 'search' => __( 'WP Search Queries', 'plausible-analytics' ), ]; @@ -334,7 +335,8 @@ public function maybe_create_custom_properties( $old_settings, $settings ) { if ( ! Helpers::is_enhanced_measurement_enabled( 'pageview-props', $enhanced_measurements ) && ! Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) && - ! Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) ) { + ! Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) && + ! Helpers::is_enhanced_measurement_enabled( 'query-params', $enhanced_measurements ) ) { return; // @codeCoverageIgnore } @@ -359,6 +361,15 @@ public function maybe_create_custom_properties( $old_settings, $settings ) { } } + /** + * Create Custom Properties for Query Parameters option. + */ + if ( Helpers::is_enhanced_measurement_enabled( 'query-params', $enhanced_measurements ) ) { + foreach ( Helpers::get_settings()['query_params'] ?? [] as $query_param ) { + $properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $query_param ] ] ); + } + } + /** * Create Custom Properties for Search Queries option. */ diff --git a/src/Admin/Settings/Page.php b/src/Admin/Settings/Page.php index 8515f9b..6a612ee 100644 --- a/src/Admin/Settings/Page.php +++ b/src/Admin/Settings/Page.php @@ -221,6 +221,28 @@ public function __construct() { 'value' => 'user-logged-in', 'caps' => [ self::CAP_PROPS ], ], + 'query-params' => [ + 'label' => esc_html__( 'Query parameters', 'plausible-analytics' ), + 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-track-custom-query-parameters', + 'slug' => 'enhanced_measurements', + 'type' => 'checkbox', + 'value' => 'query-params', + 'addtl_opts' => true, + 'caps' => [ self::CAP_PROPS ], + ], + 'query-params-patterns' => [ + 'slug' => 'query_params', + 'description' => sprintf( + __( + 'Enter the query parameters you\'d like to track. E.g. enter lang if you want to track %s.', + 'plausible-analytics' + ), + get_home_url() . '?lang=en' + ), + 'type' => 'clonable_text', + 'value' => Helpers::get_settings()['query_params'] ?? [], + 'hidden' => ! Helpers::is_enhanced_measurement_enabled( 'query-params' ), + ], 'search' => [ 'label' => esc_html__( 'Search queries', 'plausible-analytics' ), 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-site-search-tracking',