Posted on

Click tracking

Relevanssi Premium 2.16 introduced a new feature of click tracking. It allows you to see the effectiveness of the search, which links the users click from the search results, and which search terms are most effective for each post.

The tracking allows you to find which posts receive clicks from low rankings. You can then boost these posts higher up in the rankings to make them easier to find. You can also see where keywords result in clicks on too many posts — more focus may be necessary there.

How to use

To use click tracking, enable it on the Relevanssi Logging settings page. You also need to enable regular search logging. With the click tracking active, the User searches page will show you clicks to the posts. The Relevanssi sidebar on post edit pages will also show you more information.

You can see “Click tracking insights” on the User searches page. This view will show you the posts with the most clicks, the posts with low-ranking clicks and the queries with the most posts. You can click each post and query to see more information.

How it works

Click tracking works by adding extra parameters to the search result links. When the feature is active, you’ll see two new parameters added to the links: _rt and _rt_nonce. The _rt_nonce is a safeguard to avoid logging clicks many times. The actual data is in the _rt parameter. It contains all the necessary information in an encoded format.

When a link with these parameters gets clicked, Relevanssi saves the click. The click data is in the wp_relevanssi_tracking table. Relevanssi stores each click’s post ID, search query, ranking, and timestamp. The log has no information about the user.

When the user sees the page, Relevanssi removes the click-tracking parameters from the URL. This helps prevent users from distributing the URLs with the click tracking parameters.

Showing the most clicked pages

I’m using this function to create a shortcode that will display the most popular articles:

add_shortcode( 'most_clicked_results', 'most_clicked_results_shortcode' );
function most_clicked_results_shortcode() {
	global $relevanssi_variables, $wpdb;

	$output = get_transient( 'most_clicked_results_shortcode' );
	if ( $output ) {
		return $output;
	}

	$popular_posts = $wpdb->get_results( "SELECT post_id, count(*) AS hits FROM {$relevanssi_variables['tracking_table']} GROUP BY post_id ORDER BY hits DESC LIMIT 6" );
	$post_ids      = wp_list_pluck( $popular_posts, 'post_id' );
	$links         = array();
	foreach ( $post_ids as $id ) {
		$links[] = '<a href="' . get_the_permalink( $id ) . '">' . get_the_title( $id ) . '</a>';
	}

	$links = implode( ' | ', $links );
	$output = '<p class="most_clicked_results_shortcode">Most popular search results: ' . $links . '</p>';

	set_transient( 'most_clicked_results_shortcode', $output, WEEK_IN_SECONDS );
	return $output;
}

The [most_clicked_results] shortcode fetches the six most clicked posts and creates a simple element. The results are cached for a week to avoid excessive database calls.

Permalink Manager Pro

The Permalink Manager Pro plugin removes the Relevanssi click tracking parameters from the permalinks. Here’s how you can restore the tracking parameters:

add_filter( 'permalink_manager_filter_final_post_permalink', 'pm_relevanssi_add_tracking', 999, 3 );

function pm_relevanssi_add_tracking( $permalink, $post, $old_permalink ) {
	global $relevanssi_tracking_permalink;
  
	if ( ! function_exists( 'relevanssi_add_tracking' ) ) {
		return $permalink;
    }

  	if ( isset( $relevanssi_tracking_permalink[ $post->ID ] ) ) {
		unset( $relevanssi_tracking_permalink[ $post->ID ] );
	}

	$permalink = relevanssi_add_tracking( $permalink, $post );

	return $permalink;
}

Problems with permalinks

Sometimes, click tracking may cause problems with permalinks. If you notice broken permalinks on search results pages and disabling the click tracking fixes the problem, you can disable the click tracking permalink cache with this snippet.

add_filter( 'relevanssi_add_highlight_and_tracking', function( $value ) {
  global $relevanssi_tracking_permalink;
  $relevanssi_tracking_permalink = array();
  return $value;
} );

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *