Track Individual Downloads with Google Analytics?

7 views
Skip to first unread message

td...@apple2.org

unread,
Jan 18, 2019, 4:34:14 AM1/18/19
to Directory Lister Mailing List
Including the gtag.js script in a file called analytics.inc lets you see what directories have been visited.

Is it possible to get the clicked links tracked the same way?

So one can see how many times a particular file was accessed, not just the directory that contains it?

eugenen...@gmail.com

unread,
Feb 3, 2019, 11:07:23 AM2/3/19
to Directory Lister Mailing List

Yes this is possible as I wanted to do the same thing. You have to do it by firing an event from the link using javascript. I did this a while ago so I may not have copied all the code I had to make but here is what I could find.

First I added a little tweak to the main DirectoryLister.php file, the comments explain:
----------------------------------------------
// Determine file type by extension
if (is_dir($realPath)) {

// I want to trigger Google Analytic events on file downloads but
// putting it on the a tag applies events even to the folders.
// So I am using the iconClass to identify if it's a folder or not
// however, putting the current iconClass on an href invokes bootstraps css and iconography
// so I am having to rename the file extensions to decouple it from bootstrap.

//$iconClass = 'fa-folder';
$iconClass = 'folder';
$sort = 1;
----------------------------------------------

I then did this javascript but your particular needs may be different, for me I bundled a bunch of file types and grouped them so in GA it made some sense to me what types of files users were clicking on:
----------------------------------------------
$(document).ready(function() {
// Work out what type of file was clicked and apply GA event types
$("#directory-listing li a").not(".folder").on( "click", function(e) {
e.preventDefault();
var filetitle = $(this).attr('data-name');
var filetype = filetitle.split('.').pop().toLowerCase();
var href = $(this).attr('href');
var filename = filetitle.replace(/\.[^/.]+$/, "");

//ga send data = hitType, eventCategory, eventAction, eventLabel -file extension
switch(filetype) {
case 'mp3':
case 'aif':
case 'aiff':
case 'wma':
case 'm4a':
case 'flac':
case 'ogg':
case 'wav':
ga('send', 'event', 'Audio', 'listen', filename, {
hitCallback: function() {document.location = href;}
});
break;
case 'mp4':
case 'wmv':
case 'mp4':
case 'mpg':
case 'mpeg':
case 'avi':
case 'webm':
ga('send', 'event', 'Video', 'watch', filename, {
hitCallback: function() {document.location = href;}
});
break;
case 'png':
case 'jpg':
case 'jpeg':
case 'gif':
case 'webp':
ga('send', 'event', 'Image', 'view', filename, {
hitCallback: function() {document.location = href;}
});
break;
case 'pdf':
case 'pptx':
case 'ppt':
case 'doc':
case 'docx':
case 'txt':
ga('send', 'event', 'Document', 'read', filename, {
hitCallback: function() {document.location = href;}
});
break;
case 'zip':
ga('send', 'event', 'Package', 'download', filename, {
hitCallback: function() {document.location = href;}
});
break;
default:
ga('send', 'event', 'Unknown', filetype, filename, {
hitCallback: function() {document.location = href;}
});
}
});
});
----------------------------------------------

Reply all
Reply to author
Forward
0 new messages