user3463098
user3463098

Reputation: 21

How to track File Downloads on Server?

I have place an exe file on my server in a directory and shared the link of that file on hundreds of sites (that are not mine) on the web. I have attached different ids at the end of each link. Now I am having downloads of my file that is on the server. I want to track the site from which the link is clicked ( the sits/s can be any of the site with download link and id on the web) and also some other stats related to the downloads.

Upvotes: 1

Views: 5429

Answers (2)

pbarnhart
pbarnhart

Reputation: 41

Actually, you could simply reverse DKasipovic's answer with a bit of rewriting. For example:

RewriteEngine on
RewriteRule ^(.*).(exe|zip)$ /download.php?file=$1.$2 [R,L]

Then in download.php:

$filename = filter_input(INPUT_GET, 'file', FILTER_SANITIZE_SPECIAL_CHARS);
$path     = $_SERVER['DOCUMENT_ROOT']."/".filename;
$referer  = $_SERVER['HTTP_REFERER'];
if ($fd = fopen ($fullPath, "r")) {

   //assume you are tracking via Google Analytics - add UA and filename

   $data = array(
    'v' => 1,
    'tid' =>'UA-XXXXXXXX-X',
    'cid' => gen_uuid(), 
    'dh' => 'example.com',
    't' => 'event', 
    'ec' => 'Download', 
    'ea' => "$filename", 
    'el' => "$referer",
    'ev' => "",
    'ni'=>1
    );

    $url = 'http://www.google-analytics.com/collect'; 
    $content = http_build_query($data);
    $content = utf8_encode($content); 
    $user_agent = 'OFFLINE TRACK'; 

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
    curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
    curl_setopt($ch,CURLOPT_POST, TRUE);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
    curl_exec($ch);
    curl_close($ch);

    // now give them their file
    $fsize = filesize($path);
    $path_parts = pathinfo($path);
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=".$path_parts["basename"]."");
    header("Content-length: $fsize");
    header("Cache-control: private");
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;

function gen_uuid() {
  return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  mt_rand( 0, 0xffff ),
  mt_rand( 0, 0x0fff ) | 0x4000,
  mt_rand( 0, 0x3fff ) | 0x8000,
  mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
  );
}

You can report all sorts of data back to GA, see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#content

Upvotes: 4

dkasipovic
dkasipovic

Reputation: 6130

Since you have provided direct access link for your .exe file, only thing you can do to track the downloads is view it in apache access log (or access log of whatever web server software you are using).

Or if you are using shared hosting (presumably with cPanel), you probably already have some log statistics tools like awstats.

Better way would be to write small script (download.php for example), which would write entry to database when opened and then forward the exe file to the user. That way you could have a database of each download.

Then you would share http://yoursite.com/download.php which would act as a download counter.

Upvotes: 1

Related Questions