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. You can see which links the users click from the search results. It also shows you 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 to 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.

On the User searches page, you can see “Click tracking insights”. 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 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 the post ID, search query, ranking, and timestamp for each click. The log has no information about the user.

When the user sees the page, Relevanssi removes the parameters from the URL. This removal helps avoid users 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;
}

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.