Dennis Crane

Tracking your file downloads

Tracking is a king in mISV business.

When somebody downloads a file from your website you must log the event. Therefore, you must invoke a script (e.g. download.php) on your server that will write the info about the event in a file or database and then will redirect the visitor to the actual binary file.

In the same time, using script as a download link is not practical because many software archives do not allow links to php/pl/cgi pages, only to the exe or zip.

To overcome this you may use .htaccess file for redirection.

If you use http://www.site.com/download/abcmaker.zip as a public download link then the sample string for .htaccess can be:

Redirect /download/abcmaker.zip http://www.site.com/download/download.php?product=abcmaker

and the download.php must contain something like:

<?
...
//logging info to file or db
...
header("Location: /download/abcmaker_1421.zip"); //link to real file
?>

As you see this approach allows also to change the actual file name on the server while all incoming links may still point to http://www.site.com/download/abcmaker.zip

del.icio.us:Tracking your file downloads digg:Tracking your file downloads reddit:Tracking your file downloads

6 Responses to “Tracking your file downloads”

  1. Andrea Nagaron 26 Jun 2007 at 9:23 am

    That’s a great tip Dennis. Thanks. It will allow for virtually tracking all downloads.

    Have you got any tips on how to get the real downloads so that you take in account the download managers?

  2. Dennis Craneon 26 Jun 2007 at 9:33 am

    In download.php you may analyze referrer (e.g. getenv(”HTTP_REFERER”), $GLOBALS[”HTTP_REFERER”] or $HTTP_REFERER), user agent ($_SERVER[”HTTP_USER_AGENT”]), IP ($_SERVER[”REMOTE_ADDR”]) and your cookies that were set on other pages.

    Usually download managers and bots may be recognized by user agent strings and by IP (e.g. regular daily downloads from the same IP).

  3. NirDon 26 Jun 2007 at 10:34 am

    Hi Dennis
    Why are you only tracking downloads?
    Why aren’t you using web analytics software (for example, Google Analytics)?
    And if you are interested only in tracking downloads why aren’t you using the web server logs? This is faster and more reliable then any redirector script and you don’t have to write a single line of code (if the answer is that you don’t have access to the server logs, well, if you think tracking is king you should think about switching web hosts).

  4. Dennis Craneon 26 Jun 2007 at 2:06 pm

    Nir,
    I don’t say we track only downloads. We track everything :-) This is just one of the techniques we use.
    Web analytics software is often superfluous and raw log analysis also requires specialized tools or scripts.
    PHP scripts built into the page code allows me easily track the standard ISV conversions: “visit(referrer)-download-order”. Plus, I have more control over my own scripts and data formats than over any third-party tool.

  5. Andrea Nagaron 16 Nov 2008 at 2:59 pm

    Hi,
    I would like to use your code to track downloads using google analytics.
    I tried injecting the standard Google Analytics javascript before the PHP redirect but I get an warning

    Warning: Cannot modify header information - headers already sent

    Is there a way around this?

  6. Dennis Craneon 16 Nov 2008 at 3:19 pm

    You are not allowed to output any characters (e.g. GA code) before sending HTTP headers through PHP header() function. I.e. headers goes first, and then the rest output (HTML, txt, media) goes.

Trackback URI | Comments RSS

Leave a Reply

Captcha
Enter the letters you see above.