From bc62103c16e287460d79b1e67d4aa25134f1c2e1 Mon Sep 17 00:00:00 2001
From: Dan0sz <18595395+Dan0sz@users.noreply.github.com>
Date: Tue, 2 Dec 2025 15:35:29 +0100
Subject: [PATCH 1/6] Added: Show what page the search was made on.
---
src/Actions.php | 5 +++--
src/Admin/Provisioning.php | 3 ++-
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/Actions.php b/src/Actions.php
index 74deef3..561996c 100644
--- a/src/Actions.php
+++ b/src/Actions.php
@@ -126,8 +126,9 @@ public function maybe_register_assets() {
[
'props' => [
// convert queries to lowercase and remove trailing whitespace to ensure same terms are grouped together
- 'search_query' => strtolower( trim( get_search_query() ) ),
- 'result_count' => $wp_query->found_posts,
+ 'search_query' => strtolower( trim( get_search_query() ) ),
+ 'result_count' => $wp_query->found_posts,
+ 'search_source' => wp_get_referer(),
],
]
);
diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php
index 0569ec6..112889b 100644
--- a/src/Admin/Provisioning.php
+++ b/src/Admin/Provisioning.php
@@ -66,6 +66,7 @@ class Provisioning {
private $custom_search_properties = [
'search_query',
'result_count',
+ 'search_source',
];
/**
@@ -365,7 +366,7 @@ public function maybe_create_custom_properties( $old_settings, $settings ) {
$caps = get_option( 'plausible_analytics_api_token_caps', [] );
foreach ( $this->custom_search_properties as $property ) {
- if ( empty( $caps[ 'props' ] ) && $property === 'result_count' ) {
+ if ( empty( $caps[ 'props' ] ) && ( $property === 'result_count' || $property == 'search_source' ) ) {
continue;
}
From dade96147214ca6956df277b79826548f72af1a2 Mon Sep 17 00:00:00 2001
From: Dan0sz <18595395+Dan0sz@users.noreply.github.com>
Date: Tue, 2 Dec 2025 15:50:28 +0100
Subject: [PATCH 2/6] Improved: added migration script for when Search Queries
is already enabled.
---
src/Admin/Upgrades.php | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/src/Admin/Upgrades.php b/src/Admin/Upgrades.php
index c882211..5ea6749 100644
--- a/src/Admin/Upgrades.php
+++ b/src/Admin/Upgrades.php
@@ -84,6 +84,10 @@ public function run() {
$this->upgrade_to_231();
}
+ if ( version_compare( $plausible_analytics_version, '2.4.2', '<' ) ) {
+ $this->upgrade_to_242();
+ }
+
// Add required upgrade routines for future versions here.
}
@@ -296,4 +300,26 @@ public function upgrade_to_231() {
update_option( 'plausible_analytics_version', '2.3.1' );
}
+
+ /**
+ * If Search Queries is enabled, make sure the custom properties are created after updating.
+ *
+ * @return void
+ */
+ public function upgrade_to_242() {
+ $settings = Helpers::get_settings();
+
+ if ( Helpers::is_enhanced_measurement_enabled( 'search' ) ) {
+ $provisioning = new Provisioning();
+
+ // No token entered.
+ if ( ! $provisioning->client instanceof Client ) {
+ return;
+ }
+
+ $provisioning->maybe_create_custom_properties( [], $settings );
+ }
+
+ update_option( 'plausible_analytics_version', '2.4.2' );
+ }
}
From 1303a7a6d9abddc74b88f38e52da7d83d26d2795 Mon Sep 17 00:00:00 2001
From: Dan0sz <18595395+Dan0sz@users.noreply.github.com>
Date: Tue, 2 Dec 2025 17:24:51 +0100
Subject: [PATCH 3/6] Improved: add hidden input field to search block to make
sure we always have the referer.
---
src/Actions.php | 77 +++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 74 insertions(+), 3 deletions(-)
diff --git a/src/Actions.php b/src/Actions.php
index 561996c..dbf98e8 100644
--- a/src/Actions.php
+++ b/src/Actions.php
@@ -19,6 +19,8 @@ public function __construct() {
add_action( 'wp_head', [ $this, 'maybe_insert_version_meta_tag' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'maybe_register_assets' ] );
add_action( 'admin_bar_menu', [ $this, 'admin_bar_node' ], 100 );
+ add_filter( 'get_search_form', [ $this, 'maybe_add_hidden_input_to_search_form' ], 10, 1 );
+ add_filter( 'render_block', [ $this, 'maybe_add_hidden_input_to_search_block' ], 10, 2 );
}
/**
@@ -122,17 +124,18 @@ public function maybe_register_assets() {
if ( Helpers::is_enhanced_measurement_enabled( 'search' ) && is_search() ) {
global $wp_query;
- $data = wp_json_encode(
+ $search_source = isset( $_REQUEST[ 'search_source' ] ) ? sanitize_text_field( $_REQUEST[ 'search_source' ] ) : wp_get_referer();
+ $data = wp_json_encode(
[
'props' => [
// convert queries to lowercase and remove trailing whitespace to ensure same terms are grouped together
'search_query' => strtolower( trim( get_search_query() ) ),
'result_count' => $wp_query->found_posts,
- 'search_source' => wp_get_referer(),
+ 'search_source' => $search_source,
],
]
);
- $script = "plausible('WP Search Queries', $data );";
+ $script = "plausible('WP Search Queries', $data );";
wp_add_inline_script(
'plausible-analytics',
@@ -144,6 +147,74 @@ public function maybe_register_assets() {
do_action( 'plausible_analytics_after_register_assets', $settings );
}
+ /**
+ * Adds a hidden input field to the search form for enhanced measurement of search referrer.
+ *
+ * This method checks if enhanced measurement is enabled for search, and if so, it appends
+ * a hidden input field containing a reference to the search's referrer URL.
+ *
+ * @param string $form The HTML markup of the search form.
+ *
+ * @return string The modified HTML markup of the search form with the hidden input added,
+ * or the original form if enhanced measurement is not enabled.
+ */
+ public function maybe_add_hidden_input_to_search_form( $form ) {
+ if ( ! Helpers::is_enhanced_measurement_enabled( 'search' ) ) {
+ return $form;
+ }
+
+ $referrer = $this->get_referrer();
+ $hidden_input = '';
+
+ return str_replace( '', $hidden_input . '', $form );
+ }
+
+ /**
+ * Retrieves the current page URL to be used as a referrer.
+ *
+ * This method constructs the referrer by obtaining the current page URL and ensures
+ * it is sanitized. If the referrer cannot be determined, an empty string is returned.
+ *
+ * @return string The sanitized referrer URL or an empty string if unavailable.
+ */
+ private function get_referrer() {
+ $referrer = esc_url( home_url( add_query_arg( null, null ) ) );
+
+ if ( ! $referrer ) {
+ $referrer = '';
+ }
+
+ return esc_attr( $referrer );
+ }
+
+ /**
+ * Adds a hidden input field to the content of a search block for enhanced measurement of search referrer.
+ *
+ * This method checks if the given block is a WordPress core search block. If so, it appends
+ * a hidden input field containing a reference to the current page's URL as the search's referrer.
+ * The hidden input is inserted before the button element if present or at the end of the block content otherwise.
+ *
+ * @param string $block_content The current content of the block.
+ * @param array $block The block attributes and settings.
+ *
+ * @return string The modified content of the block with the hidden input added if it is a core search block,
+ * or the original block content if the block is not a search block.
+ */
+ public function maybe_add_hidden_input_to_search_block( $block_content, $block ) {
+ if ( $block[ 'blockName' ] === 'core/search' ) {
+ $referrer = $this->get_referrer();
+ $hidden_input = '';
+
+ if ( str_contains( $block_content, '