How to Add Google Analytics Campaign tracking to your WordPress RSS Feed

Adding Google Analytics Campaign Tracking to your WordPress RSS feeds is great for measuring how your content is consumed outside of you website.

If a user clicks on your through to your site from their RSS reader,  you need  to be able to track these conversions. This is easily accomplished by placing the text below into your functions.php file in your wordpress theme.

Code Snippet

function rsslinktagger($guid) {
     global $post;
     $get_vars = array(
        'utm_source' => urlencode( get_the_title( $post ) ),
        'utm_medium' => urlencode('RSS'),
        'utm_campaign' => urlencode('RSS Reader')
     );
     return $guid . '?' . implode( '&', $get_vars );
}
add_filter( 'the_permalink_rss', 'rsslinktagger' );

The function above uses the “the_permalink_rss” hook to allow us to filter the the permalink so we may append the analytics query string to the end.

We have 3 variables that we would like to add to the end.
utm_source – The title of our post
utm_medium – Where the post came from
utm_campaign – What campaign to associate the inbound link to

We are running each of the strings through php’s urlencode function to ensure the strings are encoded correctly.

We then return the original URL with the “?” and glue the pieces of the array together using php’s implode so that the returned url should look something like this:
http://joshledford.com?utm_source=TITLE&utm_medium=RSS&utm_campaign=RSS%20Reader

More information on Custom Campigns in Google Analytics can be found here.

One thought on “How to Add Google Analytics Campaign tracking to your WordPress RSS Feed

Leave a Reply

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