Jun 26th, 2007
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


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?
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).
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).
Nir,
This is just one of the techniques we use.
I don’t say we track only downloads. We track everything
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.
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?
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.