Tracking Wordpress downloads in Google Analytics

Submitted by hc on Fri, 2015-10-23 11:51
hc's picture

A client needed to track stats on the downloading of various PDF publications from their website. The request to download a file could come from within an email link, rather than a click within the Wordpress interface, so using standard Javascript based solutions wasn't good enough, as they might not capture direct links to the file.

A painless solution involving 2 plugins, an a tiny bit of glue code:

With these two installed, and your GA settings configured, you will now be able to send a tracking stat to Google, every time a download is clicked, even if the download is requested outside of the Wordpress page.

Using the dlm_downloading hook provided by Download Monitor, you can add the following to your theme's functions.php:

add_action('dlm_downloading', 'custom_dl_tracking' );

function custom_dl_tracking($download, $version = "1.0") {
    $PGAMP = null;
    if ( class_exists( 'Pilau_GA_Measurement_Protocol' ) ) {
        $PGAMP = Pilau_GA_Measurement_Protocol::get_instance();

        $method = 'event';
        $info = array('category'=>'general', 'action' => 'download');
        $info['label'] = $download->get_the_filename();
        $PGAMP->build_hit( $method, $info );

Adjust the category and action parameters to suit; likewise, if you want to record something other than the filename of the download, check the other parameters available in the DLM DOwnload class

Drupal theme by Kiwi Themes.