1

I've got a greasemonkey (.user.js) file people can download on my webpage. I'd like to keep track of how many times it has been downloaded. I could use onclick="pageTracker._trackPageview('/file_name.file_extension') but the file probably gets downloaded more often directly rather than through the "official link".

How can I set this up? (Doesn't have to be through Google analytics, a custom solution is equally good) There are a few constraints, the solution has to work like this:

  • Firefox: Ask the user if she wants to install the script.
  • Opera: See the file directly when clicking on it (right click needs to be enabled to save it)
  • Chrome: I'm not sure how it works with Chrome :)
Laoujin
  • 9,962
  • 7
  • 42
  • 69

2 Answers2

3

If you did want to use Google Analytics, then I would recommend looking into PHP-GA - a PHP based Google Analytics library. So you would link to a PHP script, which logs the view in Google Analytics and then sends the file to the browser. This means that it will log every download, regardless of direct or from clicking on the link.

Here is a basic example off the Google Code page.

use UnitedPrototype\GoogleAnalytics;

// Initilize GA Tracker
$tracker = new GoogleAnalytics\Tracker('UA-12345678-9', 'example.com');

// Assemble Visitor information
// (could also get unserialized from database)
$visitor = new GoogleAnalytics\Visitor();
$visitor->setIpAddress($_SERVER['REMOTE_ADDR']);
$visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$visitor->setScreenResolution('1024x768');

// Assemble Session information
// (could also get unserialized from PHP session)
$session = new GoogleAnalytics\Session();

// Assemble Page information
$page = new GoogleAnalytics\Page('/page.html');
$page->setTitle('My Page');

// Track page view
$tracker->trackPageview($page, $session, $visitor);

Combine this with code to send the file to the browser via PHP, like so:

$file = 'file.js';
header('Content-disposition: attachment; filename='.$file);
header('Content-type: text/plain');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
Kieran
  • 56
  • 1
  • 7
0

To have the greasemonkey script execute the PHP code, you need to create or modify the .htaccess file in the same directory and add these lines:

<Files yourUserScript.user.js>
AddType application/x-httpd-php .user.js
</Files>

Once .htaccess is in place you can use PHP-GA to send the download hit to Google Analytics.

This makes it work for Opera, Firefox and for Chrome users with Tampermonkey. Without Tampermonkey, it becomes more difficult for users to install your script:

In order to install off-store extensions, the user must download them to a directory and drag them onto chrome://extensions/.

You can however use Inline Installation. You keep the visitors on your page and they don't have to know about downloading and dragging into extensions. Before you can use this inline you have to verify your website.

This answer was very helpful aswell: Manually adding a Userscript to Google Chrome

Sad thing here is that the Chrome Store will keep track of the Chrome downloads while Google Analytics keeps track of Firefox and Opera downloads.

Community
  • 1
  • 1
Laoujin
  • 9,962
  • 7
  • 42
  • 69