Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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});"
);
}

Expand Down
13 changes: 12 additions & 1 deletion src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
];

Expand Down Expand Up @@ -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
}

Expand All @@ -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.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <strong>lang</strong> if you want to track <code>%s</code>.',
'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',
Expand Down