Google Analytics icon

A fix for outbound & download links not working in Yoast’s Google Analytics for WordPress

The Problem

I just noticed on one of my sites that some download links to PDF, zip, and other assets had an almost total drop in download events in Google Analytics after a recent Yoast Google Analytics for WordPress plugin update.  So I began to troubleshoot …

Track outbound clicks and downloads is enabled in the Yoast plugin settings
Track outbound clicks and downloads is enabled in the Yoast plugin settings.

The firing of trackEvent() is handled by the popular Yoast Google Analytics plugin, which automatically adds onClick() javascript handlers to fire the correct event for <a /> tags, and uses the GA Event Category of “download”, and the domain or full URL to the file (determined by your plugin settings) as the Event Action.  It had worked for over a year, and is working normally on our other web properties, all of which use the same latest version of the plugin Google Analytics for WordPress plugin.

I enabled Debug Mode with the Yoast Google Analytics plugin and set the option to log all users. I typically prefer not to skew my reporting with administrator traffic, but in order to see the Debug Mode output in your browser’s javascript console, you must select the “Ignore no-one” option on the settings page.

Set ignore users option to "Ignore no-one".
Set ignore users option to “Ignore no-one”.
Enable debug mode for browsers with Javascript consoles, or alternatively, you can use the Firebug Lite feature.
Enable debug mode for browsers with Javascript consoles, or alternatively, you can use the Firebug Lite feature.

I was then able to monitor the console and see the Google Analytics pageview event fire, however, I did not see the “download” event fire.  I was stuck.

The Solution

It was a partner of mine, Colin Alsheimer from Weber Shandwick (@colinize on Twitter) that figured out the root cause:  relative URLs in the download links.

The tracking beacon finally fires successfully when using fully qualified URLs, not relative URLs.
The tracking beacon finally fires successfully when using fully qualified URLs, not relative URLs.

When I viewed the source code of my site, I could see that only the download & outbound links with fully-qualified URLs had onclick handlers attached in order to properly fire _trackEvent() attached, and not the links with relative URLs. After updating my page to use the full URLs, those download links immediately began working again, and I was able to see the event fire in the debug console as well as show up in my GA real-time event tracking.

Success!  The root cause of this seems to be a bug in the Yoast plugin as GA should allow any text string – relative URL or not – as an event action, and I’ve reported the issue to them in the WordPress forums.  It seems that the “link sanitization” feature that rewrites relative URLs with full URLs was added to the plugin in v4.0.2, however since then it has stopped adding the click event handlers to links with relative URLs.

The moral of the story is that relative links rarely come back and bite you, and they are so convenient when moving content and code between environments.  But the cost of that convenience is a very small chance that not using a full URL will break something, and it may or may not fit your individual tolerance for risk.

Have a comment about using relative links in Google Analytics event tracking?  Help others out by posting below!