I’ve managed to get some pretty solid tracking using some filters and Google analytics events. I’m also using cloudflare so I’ve added some extra IP address stuff.
I’m using this php library to help with this: https://github.com/ins0/google-measurement-php-client
/*-----------------------------------------------------------------------------------*/
/* Add analytics tracking to our podcast downloads */
/*-----------------------------------------------------------------------------------*/
function podcast_analytics($file){
require_once(dirname(__FILE__).'/src/Racecore/GATracking/GATracking.php');
$client_ip = $_SERVER["HTTP_CF_CONNECTING_IP"] ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER['REMOTE_ADDR'];
$options = array(
// use proxy
'proxy' => array(
'ip' => $client_ip
)
);
$gatracking = new \Racecore\GATracking\GATracking('your-analytics-id-here', $options);
$f = str_replace('https://example.com/','',$file);
$page = $gatracking->createTracking('Factory', array(
'dh' => 'example.com',
'dp' => $f,
'dt' => $file
));
$event = $gatracking->createTracking('Event');
$event->setAsNonInteractionHit(false);
$event->setEventCategory('Audio');
$event->setEventAction('Plays');
$event->setEventLabel($f);
$response = $gatracking->sendMultipleTracking(array(
$page, $event
));
}
add_action('ssp_file_download', 'podcast_analytics');
/*-----------------------------------------------------------------------------------*/
/* Add some Analytics tracking to our podcast feed */
/*-----------------------------------------------------------------------------------*/
function track_podcast_xml(){
require_once(dirname(__FILE__).'/src/Racecore/GATracking/GATracking.php');
$client_ip = $_SERVER["HTTP_CF_CONNECTING_IP"] ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER['REMOTE_ADDR'];
$options = array(
// use proxy
'proxy' => array(
'ip' => $client_ip
)
);
$gatracking = new \Racecore\GATracking\GATracking('your-analytics-id-here', $options);
$page = $gatracking->createTracking('Factory', array(
'dh' => 'example.com',
'dp' => 'feed/podcast',
'dt' => 'feed/podcast'
));
$event = $gatracking->createTracking('Event');
$event->setAsNonInteractionHit(false);
$event->setEventCategory('XML');
$event->setEventAction('FeedHit');
$event->setEventLabel('feed/podcast');
$response = $gatracking->sendMultipleTracking(array(
$page, $event
));
}
add_action('ssp_before_feed', 'track_podcast_xml');