Skip to content

Commit

Permalink
Added: Search Queries option to Enhanced Measurements and included au…
Browse files Browse the repository at this point in the history
…to provisioning of custom properties and (pageview) event goals.
  • Loading branch information
Dan0sz committed Jun 6, 2024
1 parent 8e2743c commit ab66877
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
24 changes: 16 additions & 8 deletions src/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +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 ( apply_filters( 'plausible_analytics_track_search', true ) && is_search() ) {
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' => [
'keyword' => get_search_query(),
'resultCount' => intval( $wp_query->found_posts ),
'search_query' => get_search_query(),
'result_count' => intval( $wp_query->found_posts ),
],
]
);
$script = 'plausible( "pageview", { u: "' . esc_attr( $search_url ) . '" } );';
$script .= "\n" . "plausible( 'Search', $data );";
$script = 'plausible("pageview", {u:"' . esc_attr( $search_url ) . '"});';
$script .= "\nplausible('Search', $data );";

wp_add_inline_script( 'plausible-analytics', $script );
wp_add_inline_script( 'plausible-analytics', "document.addEventListener('DOMContentLoaded', function() {\n$script\n});" );
}

// This action allows you to add your own custom scripts!
Expand Down
33 changes: 31 additions & 2 deletions src/Admin/Provisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Provisioning {
'category',
];

private $custom_search_properties = [
'search_query',
'result_count',
];

/**
* Build class.
*
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
Expand All @@ -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,
Expand All @@ -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 );
}

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

Expand All @@ -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 );
Expand Down
9 changes: 8 additions & 1 deletion src/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ public function __construct() {
'type' => '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',
Expand Down Expand Up @@ -649,7 +656,7 @@ public function render_analytics_dashboard() {
<div id="plausible-analytics-stats">
<iframe plausible-embed=""
src="<?php echo "{$shared_link}&embed=true&theme=light&background=transparent"; ?>"
scrolling="no" loading="lazy" style="border: 0; width: 100%; height: 1750px; "></iframe>
loading="lazy" style="border: 0; width: 100%; height: 1750px; "></iframe>
<script async src="<?php echo $hosted_domain; ?>/js/embed.host.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,16 @@ public static function get_filename( $local = false ) {
if ( ! self::is_enhanced_measurement_enabled( 'tagged-events' ) &&
self::is_enhanced_measurement_enabled( 'revenue' ) &&
( Integrations::is_wc_active() || Integrations::is_edd_active() ) ) {
$file_name .= '.' . 'tagged-events';
$file_name .= '.tagged-events';
}

if ( ! self::is_enhanced_measurement_enabled( 'pageview-props' ) && self::is_enhanced_measurement_enabled( 'search' ) ) {
$file_name .= '.pageview-props';
}

// Load exclusions.js if any excluded pages are set.
if ( ! empty( $settings[ 'excluded_pages' ] ) ) {
$file_name .= '.' . 'exclusions';
$file_name .= '.exclusions';
}

// Add the manual scripts as we need it to track the search parameter.
Expand Down

0 comments on commit ab66877

Please sign in to comment.