Sats for file downloads?

joel31

New Member
Messages
5
Reaction score
0
Points
1
I'm using google analytics to track stats for my site, but I also have a file that I allow users to download. I'd like to see how many times it is downloaded. I understand javascript could track it if they download it from a link on my site, but there may be people who download it directly and so I'd like to see how often that happens.

Is there server side statistics available or some way to do that?
 

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
There is a couple of ways it can be done but the most common is using the .htaccess file to prevent hot-linking completely. You can also use this to bounce the visitor onto a page that does have analytics. Another is using a 'GateKeeper' script that checks the referer value of all incoming requests to make sure the user followed a link from inside your site. It's not a completely foolproof method but it stops most people. Failing that you are into the realms of dynamically changing file names every hour or so and that's a bit more complex.

like this :
Code:
http://yoursite.com/downloads/gate_keeper.php?file=priviate_stuff.jpg

if somebody tries
Code:
http://yoursite.com/downloads/priviate_stuff.jpg
they find nothing.

PHP:
<?php
$dir='secret-directory-name-here/';
if ((!$file=realpath($dir.$_GET['file']))
    || strpos($file,realpath($dir))!==0 || substr($file,-4)=='.php'){
  header('HTTP/1.0 404 Not Found');
  exit();
}
$ref=$_SERVER['HTTP_REFERER'];
if (strpos($ref,'http://www.yoursite.com/')===0 || strpos($ref,'http')!==0){
  $mime=array(
    'jpg'=>'image/jpeg',
    'png'=>'image/png',
    'mid'=>'audio/x-midi',
    'wav'=>'audio/x-wav'
  );
  $stat=stat($file);
  header('Content-Type: '.$mime[substr($file,-3)]);
  header('Content-Length: '.$stat[7]);
  header('Last-Modified: '.gmdate('D, d M Y H:i:s',$stat[9]).' GMT');
  readfile($file);
  exit();
//// insert script to increment database counter or flat text file to record who, what, when

}
header('Pragma: no-cache');
header('Cache-Control: no-cache, no-store, must-revalidate');
include($file.'.php');
?>
 
Last edited:

joel31

New Member
Messages
5
Reaction score
0
Points
1
Last edited:

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
Well if you take the script above and remove the referer check it could do that with a bit more code. Plenty of tutorials out there describing this process in detail for logging downloads with PHP.

However:
This php-ga system I am a little dubious about, Google have have some strict rules about the way people interact with their services
though shalt not screw with our code
sums it up neatly so sending data directly from the a sites backend to the G-analytics server, bypassing the visitors web browser completely seems a sure fire way to upset Google. I would ask in Google's own analytics user forum before using it.

php-ga.png



God forbid anybody gets this kind of system to work with third party adverts or there will be chaos!
 
Last edited:

joel31

New Member
Messages
5
Reaction score
0
Points
1

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
The problem with google product forums is that staff rarely put in an appearance especially when somebody in authority is required to make a ruling over what's allowed and what is not. The adsense forum suffers badly because of it and often you just have to go on the probability things will be alright.

That image based tracking solution mobile websites is well odd, true some mobile phone users do block images to save bandwidth but fewer still actually disable javascript. On the flip-side lots of folk have cookies blocked for privacy reasons and dynamically created images are a good substitute for tagging a users browser with a unique ID. It's one of the methods used by the infamous everCookie script and didn't know Google was using this system

thanks for that!
 
Top