How to Add Google Analytics to WP Related Posts


by in Tutorials, WordPress


WP Related Posts - Google AnalyticsWe all know that having a “related posts” plugin helps with many technical SEO aspects.

This includes decreased bounce rate and overall user experience (UX) with increased visitor engagement.

But, how do we measure these statistics? Let’s rewind and discuss the benefits first.

If you go to any major website, such as Forbes or Mashable, you will see some type of content discovery section. The idea behind this is that your website visitor is in “action” mode and looking for more content to feed their appetite.

The content discovery section might be in the form of promoted content through channels like Outbrain or Taboola, or self-contained content that further explores the author’s posts or content related to that specific post.

If your site has a ton of traffic, the promoted route would be the way to go so you can monetize your site’s exceptional content.

If on the other hand, you’re still building traffic, or simply want to keep your visitors engaged on your own site, stick with a related posts plugin for your WordPress site.

When nRelate shut down, I started exploring new options. And, I specifically wanted an option that will help with the SEO advantages mentioned above.

Decreased Bounce Rate

This is a simple metric where if a visitor only views one page on your site, that equals a bounce rate of 100%. At two pages, the bounce rate is 50% – like I said, simple.

A bounce rate below 40% is considered excellent. So, if a visitor reads your posts, then clicks on an additional recommended post at the end, we’re golden.

Better UX

Providing a better user experience for visitors is something we should all move towards more and more. There’s nothing better when coming to a site, and it almost reads my mind with what I want next.

Having “related posts” at the end is a great step in this direction. We keep the visitor happy by providing more information that matches what they came for.

Of course, this also equals more page views for our site = SEO win.

WordPress Related Posts

The option I settled on was WP Related Posts by Zemanta. I love that the links and images have direct SEO value with internal linking.

They also have an interesting way to promote other posts when you’re writing your content, and we all know that adding authority links within our posts has SEO value. Make sure you’re linking out from your posts to quality references!

But, this plugin has one drawback – I couldn’t tell if it truly was driving increased engagement.

Adding a WordPress Filter

WP is a very powerful platform. After reaching out to Zemanta support and learning they didn’t have a filter to measure Google Analytics, I decided to write my own.

// UTM tags or Event Tracking on Zemanta Related Posts Plugin
function add_utm_tags_wp_rp($content) {
 
   // Google Analytics Variables
   $utm_source = 'zadroweb'; // your blog name
   $utm_medium = 'blog'; // medium is blog but change if needed
   $utm_campaign_links = 'related_posts_link'; // what is the campaign name for links?
   $utm_campaign_images = 'related_posts_image'; // what is the campaign name for images?
 
   $event_category = 'Blog'; // event category
   $event_action_links = 'Related Posts Link'; // event action for links
   $event_action_images = 'Related Posts Image'; // event action for images
 
   // Add additional special characters to remove from post title
   $string_array = array('!','@','#','$','%','^','&','?',',','-',':');
 
   // Should not have to touch below this
   global $post;
   $string_replace = str_replace(' ','_',str_replace($string_array,'',$post->post_title));
 
   // Pattern matching for links
   $pattern_links = '/<a href="(.*?)" class="wp_rp_title(.*?)"(.*?)>/i';
   //$replacement_links = '<a href="$1?utm_source='.$utm_source.'&utm_medium='.$utm_medium.'&utm_campaign='.$utm_campaign_links.'&utm_term='.$string_replace.'" class="wp_rp_title$2"$3>';
   $replacement_links = '<a href="$1" onClick="dataLayer.push({\'event\': \'trackEvent\', \'eventCategory\': \'' .  $event_category . '\', \'eventAction\': \'' .  $event_action_links . '\', \'eventLabel\': \'' .  $post->post_name . '\'});" class="wp_rp_title$2"$3>';
   $content = preg_replace($pattern_links, $replacement_links, $content);
 
   // Pattern matchign for images
   $pattern_images = '/<a href="(.*?)" class="wp_rp_thumbnail(.*?)"(.*?)>/i';
   //$replacement_images = '<a href="$1?utm_source='.$utm_source.'&utm_medium='.$utm_medium.'&utm_campaign='.$utm_campaign_images.'&utm_term='.$string_replace.'" class="wp_rp_title$2"$3>';
   $replacement_images = '<a href="$1" onClick="dataLayer.push({\'event\': \'trackEvent\', \'eventCategory\': \'' .  $event_category . '\', \'eventAction\': \'' .  $event_action_images . '\', \'eventLabel\': \'' .  $post->post_name . '\'});" class="wp_rp_title$2"$3>';
   $content = preg_replace($pattern_images, $replacement_images, $content);
 
   return $content;
}
add_filter('the_content', 'add_utm_tags_wp_rp');

This might look complicated, but it’s pretty straightforward. All you have to do is copy this code and add it to your functions.php file to get started.

Then modify the Google Analytics variables to suit your needs. That’s all there is to it. Give it some time, then you can check your GA under “campaigns” to see if the images or links are providing increased SEO value.

What’s nice is that I’ve also included the optional “utm_term” variable that will show you the posts that are driving the most engagement.

Update June 6, 2016

When I originally wrote this post, it was to track clicks using Google UTM parameters. However, this was making the URLs way too long, and I don’t recommend very long URLs as that can have a negative impact on your SEO.

I’ve updated the code to include a way to track clicks using “event” behavior. You can un-comment your preferred method in the code above.

But first, you will want to follow this event tracking guide to setup Google Tag Manager. If you’re still using Google Analytics via the standard tracking code, you should be able to modify the code pretty easily.

In Summary

We will continue to see Google make a stronger shift into measuring UX into their algorithms. Bounce rate, time on site, and all these metrics are being calculated towards your overall SEO value and, well…rankings.

Make sure you have a related posts plugin to provide a better user experience for your visitors, and always track everything!

Please, rate this post:

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00)
Loading...
Tags: ,

Discussion (3)


  • Paolo Gabrielli

    Many thanks for your post! I think I spent something like 20 minutes searching for someone that used Analytics to analyze Recommended/Related posts. I rarely spend so much time searching but it sounded incredible that nobody had that necessity.
    Thanks again,
    Paolo

    Reply

  • James

    Does this code, as it is written above, trigger an ‘event’ when someone clicks on a related post? Thanks

    Reply

    • Dario Zadro

      Currently, it adds UTM parameters to the URL and can be tracked in the “campaigns” section of GA. Adding an “events” based version to trim the URL is a great idea and something I’m working on to post in the near future.

      Reply