How to track file downloads

I have a website that plays mp3 in a flash player. If the user clicks the play button, the flash player automatically downloads mp3 and starts playing it.

Is there an easy way to track how many times a particular clip (or any binary file) has been downloaded?




Is the playback link a link to a real mp3 file or to some JavaScript code that appears in the player?

If the latter, you can easily add your own registration code there to track the number of hits to it.

If first, you need something that can track the web server log itself and make that distinction. My hosting plan comes with a Webalizer that does it nicely.

This is the javascript code that answers this question.

However, it would be nice to learn how to track downloads using a different method (without switching hosts).

+74
php logging apache download analytics
Aug 01 '08 at 17:14
source share
8 answers

The funny thing is that I wrote a php media gallery for all my music 2 days ago. I had a similar problem. I am using http://musicplayer.sourceforge.net/ for the player. And the playlist is built through php. Are all music requests sent there with a script called xfer.php? File = WHATEVER

$filename = base64_url_decode($_REQUEST['file']); header("Cache-Control: public"); header('Content-disposition: attachment; filename='.basename($filename)); header("Content-Transfer-Encoding: binary"); header('Content-Length: '. filesize($filename)); // Put either file counting code here. either a db or static files // readfile($filename); //and spit the user the file function base64_url_decode($input) { return base64_decode(strtr($input, '-_,', '+/=')); } 

And when you call the files, use something like:

 function base64_url_encode($input) { return strtr(base64_encode($input), '+/=', '-_,'); } 

http://us.php.net/manual/en/function.base64-encode.php

If you are using some kind of javascript or flash player (such as a JW player) that requires the actual link to be an mp3 file or something similar, you can add the text "& type = .mp3" so that the final the link became something like this: "www.example.com/xfer.php? file = 34842ffjfjxfh and type = .mp3." So it looks like it ends with the mp3 extension without affecting the file link.

+38
Aug 01 '08 at 17:33
source share

Use httpd log files. Install http://awstats.sourceforge.net/

+28
Aug 01 '08 at 17:51
source share

Use bash:

 grep mp3 /var/log/httpd/access_log | wc 
+25
Oct 02 '09 at 2:12
source share

If your song / binary was submitted by apache, you can easily grep access_log to find out the number of downloads. A simple post-logrotate script can grep to log and maintain count statistics in db. This has a performance advantage because you are not in your request code in real time. Doing non-critical things, such as offline statistics, is a good idea to scale your site to a large number of users.

+13
Oct 12 '08 at 9:10
source share

You can even configure the Apache.htaccess directive, which converts * .mp3 requests to querystring that dubayou works with. This might be an elegant way to keep a direct request and still be able to perform the function of the slipstream log in response.

+12
Aug 01 '08 at 17:42
source share

Is the link to play the link to the actual mp3 file or to some javascript code that the player issues?

If the latter, you can easily add your own logging code to track the number of hits to it.

If the first, you will need something that can track the web server log itself and make that distinction. My hosting plan comes with a webalizer that makes it beautiful.

+6
Aug 01 '08 at 17:24
source share

Is there a database for your music library? If there is any server code that starts when mp3 is downloaded, you can add additional code to increase the amount of playback. You may also have javascript make a second request to increase the number of plays, but this can lead to a false increase in the number of people / robots.

I worked on an internet radio site and we used separate tables to track the playing time of each song. Our streams were involved in a perl script with the launch of icecast, so we ran a database query every time a new track started. Then, to calculate the play counter, we ran a query to count how many times the song ID was recorded in the playback log.

+3
04 Sep '08 at 18:44
source share

The problem with things like AWStats / reading through web server logs is that large downloads can often be divided into pieces of data in the logs. This makes it difficult to agree on the exact number of downloads.

I offer Google Analytics event tracking , as this will be logged once per click on the download link.

+3
Jun 09 '16 at 9:38 on
source share



All Articles