Campaign Tracking YOURLS in Google Analytics

YOURLS is a URL shortening service you can run on your own server, and it comes with a WordPress plugin called YOURLS WordPress to Twitter. In the past I’ve used WP to Twitter, but since it doesn’t support YOURLS I’ve had to switch.
I’ve asked the author of YOURLS WordPress to Twitter to add Google Analytics Campaign Tracking Code with a custom field for authors to add their own identifier, but he has been too busy updating the YOURLS scripts, so I’ve hacked the plugin code to allow us to use Campaign Tracking now.

Note: Only add this Google Analytics Campaign Targeting code if you are using YOURLS for your own internal web site links only. If you also use the short URLs for offsite links, those links will break because of the tracking code.
In the plugin go to the inc/core.php, and open it in a text editor. I prefer to use Notepad++.
On line 74 change the code from:
$url = get_permalink ($post_id);
$url = get_permalink ($post_id)."?utm_source=your-identifier&utm_medium=your-identifier&utm_campaign=your-identifier";
On line 107 change the code from:
$shorturl = wp_ozh_yourls_api_call( $service, $url);
$shorturl = wp_ozh_yourls_api_call( $service, $url."?utm_source=your-identifier&utm_medium=your-identifier&utm_campaign=your-identifier");
Change “your-identifier” to your custom campaign identifier.
That’s it! Now in your YOURLS reports you will see the Original URL appended with the Google Analytics tracking tag. It will take at least 24 to 48 hours for the Campaign tracking to be posted in Google Analytics. To see the report go to Google Analytics -> View report -> Traffic Sources -> Campaigns, and you’ll see a link with the identifier name you chose.

If you’re not interested in hacking the core.php file yourself, you can (right-click) click here and choose save as to download a copy already modified for tracking, then just unzip the file, and replace the old core.php file with the new one. If you use the already modified core.php file you will see the Google Analytics Campaign tracking labeled as yourls-camp-track.
Note: The core.php file available for download above will work for YOURLS: WordPress to Twitter Version 1.3.2.

Leave a Comment