First Cat: SEO

Cat String: SEO|Shortcodes|Wordpress

Published Timestamp: 1576729137

Current Timestamp: 1606474222

Seconds Since Publish: 29745085

Days Since Publish: 344

Days Since Updated: 228

Tracking Your Inline Links in WordPress – Quick and Easy

WordPress is the straight dope of content management systems (CMSs) for those efficient (lazy) SEOs and web developers.  However it’s got a few quirks here and there that can be super annoying.  One of these quirks is the fact that it will strip out an JavaScript code from the text of your posts/pages.

If you wanted to track your internal or outbound links for whatever reason (i.e.: affiliate links, etc.) one of the easiest ways to do this is through a simple onClick event in your HTML links.  However due to WordPress’ built in security, it will strip out any Javascript from its text editor.

There are a couple of ways around this, including:

  • Disabling the WYSIWYG Editor in WordPress
    • This makes it so you can only use the source code editor.  This makes formatting pages like using Notepad. Not fun.
  • Disabling the Javascript stripping security feature within WordPress’ settings
    • I’m actually unclear how exactly to do this, but I’m very clear on the fact that it’s strongly discouraged as a big security vulnerability.
  • Creating a Shortcode which includes your link and appropriate tracking code.
    • Ding ding ding! Easy to do and completely follows the security guidelines (i.e. Javascript should be done at the ShortCode level).

Ok, so how do we do it?  Let’s see:

Creating Your Link Tracking Shortcode

This might be a little intimidating if you’ve never done it before, but it’s quite easy.  It’s comes down to:

  1. Define a function in your functions.php file.
  2. Declare that function as a shortcode.

Done!

So for our purposes we are going to:

  1. Define the “link_function()” function.  This takes a URL and a bit of text and spits out a full HTML link with whatever additional code we want, in this case a Google Analytics tracking code.
  2. Declare the “link_function()” function to power the [[link]] shortcode.
  3. Wherever we want a tracked link within our posts, we just use the [[link url=”https://www.example.com”]]Link Text[[/link]] code instead of a regular link.

Add Code to Your functions.php

Let’s look at the code!  See below:

 

/* BEGIN CODE */
//Reminder: Place this in your functions.php file.  I put it all the way at the bottom of the file.
function link_function($atts, $content = null){
$code_return = '';

$a = shortcode_atts( array(
'url' => '',
), $atts );

$url = $a['url'];
$tracking = " onclick=\"ga('send', 'event', 'Link', 'Click', 'Purchase Details');\" "; 
//This is using the ga.js standard, make sure it conforms with your tracking code event tracking.
//Also, obviously, swap out the event categories with appropriate values. 
//More info here: https://developers.google.com/analytics/devguides/collection/analyticsjs/events
$code_return .= '' . $content .'';

return $code_return;
}
add_shortcode( 'link', 'link_function' );
/* END CODE */

Putting the Links in Posts

After this code is implemented, it’s as simple as using the shortcode for any links you want to track.  I.e.:

This is wonderfully optimized bit of text meant to lead you to click on [[link url=”https://www.example.com/product-page?affiliate-code=michael-hayes”]]this link[[/link]].

Simple right?

 

Leave a Reply

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