SearchWP Metrics with Live Search

SearchWP greatly improves the standard WordPress site search (more information). You can also install the SearchWP Metrics extension for analytics on popular search terms, overall search traffic, search queries with no results, and more.

SWP Metrics collects this data by adding URL variables to the permalinks on the search results page. As an example, click the search icon in my site’s menu and do a search, then look at the URLs returned.

If you are using the Live Ajax Search plugin to display search results as the user types a query, these won’t be tracked and could represent a good percentage of your searches.

You can customize the markup for the live search by creating a directory and file in your theme, searchwp-live-ajax-search/search-results.php.

Add the following to the file to track live searches.

<?php if ( have_posts() ) :
	do_action( 'searchwp_metrics_click_tracking_start' );

	<?php while ( have_posts() ) : the_post(); ?>
		<?php $post_type = get_post_type_object( get_post_type() ); ?>
		<div class="searchwp-live-search-result" role="option" id="" aria-selected="false">
			<p><a href="<?php echo esc_url( get_permalink() ); ?>">
				<?php the_title(); ?> (<?php echo esc_html( $post_type->labels->singular_name ); ?>) »
	<?php endwhile; ?>
<?php else : ?>
	<p class="searchwp-live-search-no-results" role="option">
		<em><?php esc_html_x( 'No results found.', 'swplas' ); ?></em>
	do_action( 'searchwp_metrics_click_tracking_stop' );
endif; ?>

The original template was copied from /wp-content/plugins/searchwp-live-ajax-search/templates/search-results.php.

I added do_action( 'searchwp_metrics_click_tracking_start' ); to the start, and do_action( 'searchwp_metrics_click_tracking_stop' ); to the end. This tells SWP Metrics to add the URL variables to the links found in here.

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Comments are closed. Continue the conversation with me on Twitter: @billerickson