diff --git a/src/Actions.php b/src/Actions.php index ac9250b9..e4b94569 100644 --- a/src/Actions.php +++ b/src/Actions.php @@ -66,11 +66,38 @@ public function maybe_register_assets() { ); // Track 404 pages (if enabled) - if ( is_array( $settings[ 'enhanced_measurements' ] ) && in_array( '404', $settings[ 'enhanced_measurements' ] ) && is_404() ) { + if ( Helpers::is_enhanced_measurement_enabled( '404' ) && is_404() ) { + $data = wp_json_encode( + [ + 'props' => [ + 'path' => 'documentation.location.pathname', + ], + ] + ); + wp_add_inline_script( 'plausible-analytics', - "document.addEventListener('DOMContentLoaded', function () { plausible('404', { props: { path: document.location.pathname } }); });" + "document.addEventListener('DOMContentLoaded', function () { plausible( '404', $data ); });" + ); + } + + // 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_rewrite, $wp_query; + + $search_url = str_replace( '%search%', '', get_site_url( null, $wp_rewrite->get_search_permastruct() ) ); + $data = wp_json_encode( + [ + 'props' => [ + 'search_query' => get_search_query(), + 'result_count' => $wp_query->found_posts, + ], + ] ); + $script = 'plausible("pageview", {u:"' . esc_attr( $search_url ) . '"});'; + $script .= "\nplausible('Search', $data );"; + + wp_add_inline_script( 'plausible-analytics', "document.addEventListener('DOMContentLoaded', function() {\n$script\n});" ); } // This action allows you to add your own custom scripts! diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 88891d50..8889770f 100644 --- a/src/Admin/Provisioning.php +++ b/src/Admin/Provisioning.php @@ -35,6 +35,11 @@ class Provisioning { 'category', ]; + private $custom_search_properties = [ + 'search_query', + 'result_count', + ]; + /** * Build class. * @@ -63,6 +68,7 @@ public function __construct( $client = null ) { '404' => __( '404', 'plausible-analytics' ), 'outbound-links' => __( 'Outbound Link: Click', 'plausible-analytics' ), 'file-downloads' => __( 'File Download', 'plausible-analytics' ), + 'search' => __( 'Search', 'plausible-analytics' ), ]; $this->init(); @@ -140,6 +146,13 @@ public function maybe_create_goals( $old_settings, $settings ) { } $goals[] = $this->create_request_custom_event( $this->custom_event_goals[ $measurement ] ); + + if ( $measurement === 'search' ) { + global $wp_rewrite; + + $search_url = str_replace( '%search%', '', $wp_rewrite->get_search_permastruct() ); + $goals[] = $this->create_request_custom_event( null, 'Pageview', '', $search_url ); + } } $this->create_goals( $goals ); @@ -152,7 +165,7 @@ public function maybe_create_goals( $old_settings, $settings ) { * * @return GoalCreateRequestCustomEvent */ - private function create_request_custom_event( $name, $type = 'CustomEvent', $currency = '' ) { + private function create_request_custom_event( $name, $type = 'CustomEvent', $currency = '', $path = '' ) { $props = [ 'goal' => [ 'event_name' => $name, @@ -164,6 +177,12 @@ private function create_request_custom_event( $name, $type = 'CustomEvent', $cur $props[ 'goal' ][ 'currency' ] = $currency; } + if ( $type === 'Pageview' ) { + unset( $props[ 'goal' ][ 'event_name' ] ); + + $props[ 'goal' ][ 'path' ] = $path; + } + return new Client\Model\GoalCreateRequestCustomEvent( $props ); } @@ -267,7 +286,8 @@ public function maybe_create_custom_properties( $old_settings, $settings ) { $enhanced_measurements = $settings[ 'enhanced_measurements' ]; if ( ! Helpers::is_enhanced_measurement_enabled( 'pageview-props', $enhanced_measurements ) && - ! Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) ) { + ! Helpers::is_enhanced_measurement_enabled( 'revenue', $enhanced_measurements ) && + ! Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) ) { return; // @codeCoverageIgnore } @@ -292,6 +312,15 @@ public function maybe_create_custom_properties( $old_settings, $settings ) { } } + /** + * Create Custom Properties for Search Queries option. + */ + if ( Helpers::is_enhanced_measurement_enabled( 'search', $enhanced_measurements ) ) { + foreach ( $this->custom_search_properties as $property ) { + $properties[] = new Client\Model\CustomProp( [ 'custom_prop' => [ 'key' => $property ] ] ); + } + } + $create_request->setCustomProps( $properties ); $this->client->enable_custom_property( $create_request ); diff --git a/src/Admin/Settings/Hooks.php b/src/Admin/Settings/Hooks.php index 0234f8f1..4570a5f2 100644 --- a/src/Admin/Settings/Hooks.php +++ b/src/Admin/Settings/Hooks.php @@ -33,13 +33,12 @@ public function __construct( $init = true ) { */ private function init_hooks() { add_filter( 'plausible_analytics_toggle_option_success_message', [ $this, 'maybe_modify_success_message' ], 10, 3 ); - add_action( 'plausible_analytics_settings_api_connect_button', [ $this, 'connect_button' ] ); add_action( 'plausible_analytics_settings_api_token_missing', [ $this, 'missing_api_token_warning' ] ); - add_action( 'plausible_analytics_settings_option_not_available_in_ce', [ $this, 'option_na_in_ce' ] ); - add_action( 'plausible_analytics_settings_proxy_warning', [ $this, 'proxy_warning' ] ); add_action( 'plausible_analytics_settings_enable_analytics_dashboard_notice', [ $this, 'enable_analytics_dashboard_notice' ] ); add_action( 'plausible_analytics_settings_option_disabled_by_missing_api_token', [ $this, 'option_disabled_by_missing_api_token' ] ); add_action( 'plausible_analytics_settings_option_disabled_by_proxy', [ $this, 'option_disabled_by_proxy' ] ); + add_action( 'plausible_analytics_settings_option_not_available_in_ce', [ $this, 'option_na_in_ce' ] ); + add_action( 'plausible_analytics_settings_proxy_warning', [ $this, 'proxy_warning' ] ); } /** @@ -63,27 +62,6 @@ public function maybe_modify_success_message( $message, $option_name, $status ) return __( 'Proxy enabled.', 'plausible-analytics' ); } - /** - * Display connect button. - * - * @output HTML - */ - public function connect_button() { - $settings = Helpers::get_settings(); - - if ( ! empty( $settings[ 'domain_name' ] ) && ! empty( $settings[ 'api_token' ] ) ): ?> - - - - - - - - 'checkbox', 'value' => 'file-downloads', ], + 'search' => [ + 'label' => esc_html__( 'Search queries', 'plausible-analytics' ), + 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-enable-site-search-tracking', + 'slug' => 'enhanced_measurements', + 'type' => 'checkbox', + 'value' => 'search', + ], 'tagged-events' => [ 'label' => esc_html__( 'Custom events', 'plausible-analytics' ), 'docs' => 'https://plausible.io/wordpress-analytics-plugin#how-to-setup-custom-events-to-track-goal-conversions', @@ -649,7 +656,7 @@ public function render_analytics_dashboard() {
+ loading="lazy" style="border: 0; width: 100%; height: 1750px; ">