<p>I have a website that plays mp3s in a flash player. If a user clicks 'play' the flash player automatically downloads an mp3 and starts playing it. </p>

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

<hr>

<blockquote>
  <p>Is the play link a link to the actual
  mp3 file or to some javascript code
  that pops up a player?</p>
  
  <p>If the latter, you can easily add your
  own logging code in there to track the
  number of hits to it.</p>
  
  <p>If the former, you'll need something
  that can track the web server log
  itself and make that distinction. My
  hosting plan comes with Webalizer,
  which does this nicely.</p>
</blockquote>

<p>It's a javascript code so that answers that. </p>

<p>However, it would be nice to know how to track downloads using the other method (without switching hosts).</p>

[{'answer_id': 148, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 3, 'selected': False, 'text': "<p>Is the play link a link to the actual mp3 file or to some javascript code that pops up a player? </p>\n\n<p>If the latter, you can easily add your own logging code in there to track the number of hits to it.</p>\n\n<p>If the former, you'll need something that can track the web server log itself and make that distinction. My hosting plan comes with webalizer, which does this nicely.</p>\n"}, {'answer_id': 152, 'author': 'w-ll', 'author_id': 146637, 'author_profile': 'https://Stackoverflow.com/users/146637', 'pm_score': 6, 'selected': True, 'text': '<p>The funny thing is I wrote a php media gallery for all my musics 2 days ago. I had a similar problem.  I\'m using <a href="http://musicplayer.sourceforge.net/" rel="nofollow noreferrer">http://musicplayer.sourceforge.net/</a> for the player. And the playlist is built via php. All music requests go to a script called xfer.php?file=WHATEVER</p>\n<pre><code>$filename = base64_url_decode($_REQUEST[\'file\']);\nheader(&quot;Cache-Control: public&quot;);\nheader(\'Content-disposition: attachment; filename=\'.basename($filename));\nheader(&quot;Content-Transfer-Encoding: binary&quot;);\nheader(\'Content-Length: \'. filesize($filename));\n\n//  Put either file counting code here, either a db or static files\n//\nreadfile($filename);  //and spit the user the file\n\nfunction base64_url_decode($input) {\n    return base64_decode(strtr($input, \'-_,\', \'+/=\'));\n}\n</code></pre>\n<p>And when you call files use something like:</p>\n<pre><code>function base64_url_encode($input) {\n     return strtr(base64_encode($input), \'+/=\', \'-_,\');\n}\n</code></pre>\n<p><a href="http://us.php.net/manual/en/function.base64-encode.php" rel="nofollow noreferrer">http://us.php.net/manual/en/function.base64-encode.php</a></p>\n<p>If you are using some JavaScript or a flash player (JW player for example) that requires the actual link of an mp3 file or whatever, you can append the text &quot;&amp;type=.mp3&quot; so the final link becomes something like:\n&quot;www.example.com/xfer.php?file=34842ffjfjxfh&amp;type=.mp3&quot;. That way it looks like it ends with an mp3 extension without affecting the file link.</p>\n'}, {'answer_id': 153, 'author': 'saint_groceon', 'author_id': 59, 'author_profile': 'https://Stackoverflow.com/users/59', 'pm_score': 4, 'selected': False, 'text': '<p>You could even set up an Apache .htaccess directive that converts *.mp3 requests into the querystring dubayou is working with.  It might be an elegant way to keep the direct request and still be able to slipstream log function into the response.</p>\n'}, {'answer_id': 158, 'author': 'Tim Boland', 'author_id': 70, 'author_profile': 'https://Stackoverflow.com/users/70', 'pm_score': 5, 'selected': False, 'text': '<p>Use your httpd log files. Install <a href="http://awstats.sourceforge.net/" rel="noreferrer">http://awstats.sourceforge.net/</a></p>\n'}, {'answer_id': 44446, 'author': 'Mike H', 'author_id': 4563, 'author_profile': 'https://Stackoverflow.com/users/4563', 'pm_score': 2, 'selected': False, 'text': "<p>Is there a database for your music library? If there is any server code that runs when downloading the mp3 then you can add extra code there to increment the play count. You could also have javascript make a second request to increment the play count, but this could lead to people/robots falsely incrementing counts.</p>\n\n<p>I used to work for an internet-radio site and we used separate tables to track the time every song was played. Our streams were powered by a perl script running icecast, so we triggered a database request every time a new track started playing. Then to compute the play count we would run a query to count how many times a song's id was in the play log.</p>\n"}, {'answer_id': 195266, 'author': 'Vinay Y S', 'author_id': 27183, 'author_profile': 'https://Stackoverflow.com/users/27183', 'pm_score': 4, 'selected': False, 'text': '<p>If your song / binary file was served by apache, you can easily grep the access_log to find out the number of downloads. A simple post-logrotate script can grep the logs and maintain your count statistics in a db.\nThis has the performance advantage by not being in your live request code path. Doing non-critical things like stats offline is a good idea to scale your website to large number of users.</p>\n'}, {'answer_id': 1507462, 'author': 'randomx', 'author_id': 172896, 'author_profile': 'https://Stackoverflow.com/users/172896', 'pm_score': 5, 'selected': False, 'text': '<p>Use bash:</p>\n\n<pre><code>grep mp3 /var/log/httpd/access_log | wc\n</code></pre>\n'}, {'answer_id': 37722221, 'author': 'icc97', 'author_id': 327074, 'author_profile': 'https://Stackoverflow.com/users/327074', 'pm_score': 2, 'selected': False, 'text': '<p>The problem I had with things like AWStats / reading through web server logs  is that large downloads can often be split in data chunks within the logs. This makes reconciling the exact number of downloads quite hard.</p>\n\n<p>I\'d suggest the Google Analytics <a href="https://developers.google.com/analytics/devguides/collection/analyticsjs/events" rel="nofollow">Event Tracking</a>, as this will register once per click on a download link.</p>\n'}]
['https://Stackoverflow.com/questions/146', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/30/']