1

The question is "Best way to count number of downloads of several files on website"

What I am trying to do:

  1. Track and tally the number of downloads of Several files
  2. For files which have different extensions. (foo.zip, bar.tar.gz, foo2.zip)
  3. Avoid relying on server side code.

I've seen multiple answers for counting the file's downloads with the Apache access.log cat /path/to/access.log | grep foo.zip | grep 200 | wc -l from Best way to count file downloads on a website , which is an eloquent solution however requires access to the backend log file. I was hoping to use an mostly-javascript solution, Which could also make a call to a php portion of code.

Community
  • 1
  • 1
Beau Bouchard
  • 835
  • 11
  • 28
  • 6
    You have to rely on server-side code. – Pointy Oct 20 '14 at 14:48
  • You won't be able to get away with not touching the server at all. The JS code is only executed on the clients computer and can not be trusted to perform persistent tasks. In any case, your JS would have to report the data to somewhere and that would most likely be a server. – Lix Oct 20 '14 at 14:50
  • (all-clientside-)javascript cannot store anything, so you could not count per website, but only let each user count on his own – Bergi Oct 20 '14 at 14:57
  • I am not looking for a all-client-side solution, I understand a call to the backend would be required at some point to store the count. adjusted the question accordingly. – Beau Bouchard Oct 20 '14 at 15:04

2 Answers2

3

Why not use Google Analytics?

https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide?hl=es

If your website already has Google Analytics enabled, you can simply add an onclick event to the links, allowing it to create a count of when the links are clicked.

For your example the links would look like:

<a onclick="var that=this;_gaq.push(['_trackEvent','Download','foo',this.href]);setTimeout(function(){location.href=that.href;},400);return false;" href="downloads/foo.zip">Download foo.zip</a>

<a onclick="var that=this;_gaq.push(['_trackEvent','Download','bar',this.href]);setTimeout(function(){location.href=that.href;},400);return false;" href="downloads/bar.tar.gz">Download foo.zip</a>

<a onclick="var that=this;_gaq.push(['_trackEvent','Download','foo2',this.href]);setTimeout(function(){location.href=that.href;},400);return false;" href="downloads/foo2.zip">Download foo.zip</a>

In Google Analytics the "download" event would be the total number of downloads for all three files, and each file will have its own event "foo, bar, foo2" which will be each file's individual download count.

If you want to do this from the server side, you need to include some code in the client. It is more efficient use tools already exist than to brew your own solution using php and javascript.

Dave Everitt
  • 17,193
  • 6
  • 67
  • 97
manuerumx
  • 1,230
  • 14
  • 28
0

You could use goo.gl or any other url shortener service, create links to your downloads, and get the stats of the links

kvdl
  • 39
  • 4