I also could do with this functionality. Am adding this feature req. as suggested here http://drupal.org/node/37582#comment-129708 - no one did so at the time AFAICS.

My present need is to track downloads of attachments (i.e. the link URL is generated automatically by Drupal). However the ability also to track content linked from within pages would be interesting.

A thought ... it might be possible (I'm guessing - I'm not a module developer!) to set this up globally, automatically, without having to modify page content on-the-fly, in the case where links employ the "private" download method whereby linked docs are referenced via URLs of the form ?q=system/files/file.ext or just system/files/file.ext (this download method seems to work whether the download method is set to "private" or "public" on the settings [file system settings] page). Ahhhh.... On second thoughts I'm not sure this would be so easy as it means that Drupal would somehow need to activate urchinTracker itself when it serves up the file ... is this possible without use of onClick???

Anyway if this worked it would be great as then all downloads of a file at www.mydomain.com/system/files/file.ext would get logged.

I guess the other way of doing it would be not to modify page content on the fly but to process pages offline (i.e. on creation/edit, or on demand), adding the relevant onClick JS then.

Anyway enough of my ramblings... My initial need is just for listed attachments to have this functionality.

Comments

budda’s picture

My initial thoughts would be to add a script to the pages which would recurse through the DOM and add an onclick event to all urls.

My JavaScript skills suck, so I'd prefer somebody else to do this and contribute it as a patch.

Alternatives, get the upload.module to pass in the onclick as an $attributes parameter to the l() function? (probably not going to happen!)

moshe weitzman’s picture

all urls might be a bit extreme but yes, i think the DOM solution is best. should be easy in 5.0 with jquery ... also note that drupal_add_js() changed significantly and this module will want to take advantage. see http://drupal.org/node/64279

gpk’s picture

What is the DOM (sorry, noob question!)

Presumably links for attachments would still need to have the onClick action added to the a tag on-the-fly?

Also the position of the basic tracking code would need to be changed because it needs to appear in the html before any further calls to urchinTracker (see http://www.google.com/support/analytics/bin/answer.py?answer=27242&hl=en). Currently it is inserted just before AFAICS. Perhaps this could be a configurable option, or maybe the activation of tracking on links could be an option which causes the position to change?

As I am currently using 4.6 I can insert the onClick JS manually for a couple of links (the tracking code appears just before ). It's fortunate I'm not using 4.7 as it wouldn't work because the tracking code would appear too late in the page. :-) So perhaps an option to control the position of the tracking code is needed anyway, assuming that it's preferable to have it as late as possible generally.

budda’s picture

The DOM is the DOcument Object Model which describes your HTML page in the browser. JavaScript can manipulate this.

Having to put the Urchin tracker include at the top of the page then causes the potential problem of making sites slow to load, as the web browser waits for Google to server the script file.

I guess it could have some logic to say when people want to track download links it inserts the tracker code in the html header. It's possible.

I wonder if it would still work okay if the onclick events are attached to the download anchors upon page load, which is after the urchin script is loaded in the footer of the page?

moshe weitzman’s picture

the position of the script doesn't matter. all the right bits will likely be in place before the user clicks a link. or maybe i am missing something.

in 5.0 links have unique identifiers which is really helpful for this.

gpk’s picture

Thanks ...

...top of the page then causes the potential problem of making sites slow to load, as the web browser waits...

the position of the script doesn't matter. all the right bits will likely be in place ......

I'm not familiar with the inner workings of Javascript and was only slavishly regurgitating Google's instructions from here: http://www.google.com/support/analytics/bin/answer.py?answer=26908

Important: if your pages include a call to urchinTracker(), utmLinker(), utmSetTrans(), or utmLinkPost(), the tracking code must be placed in your page's code above any of these calls.

The Google help also says:

urchin.js is designed to be downloaded once from Google and is then served from the visitor's cache.

The only other thought that comes to mind is that since all the pages on the site will have their own tracking code anyway, links to these pages won't want to have the additional tracking code in the link as well or else there will be double counting. i.e. only links to file downloads or external sites should have the tracking code inserted in the link. You;'d probably thought of that already ;-)

Cheers
gpk

coreb’s picture

Just adding a +1 for tracking file downloads.

captcha’s picture

There is a simple solution to this, something that worked for me.
I needed to display file attachments in a custom way, I used this snippet here http://drupal.org/node/86413, and within the function phptemplate_upload_attachments() I added the urchin code. The full code is here:

function phptemplate_upload_attachments($files) {
  $header = array(t('Type'),t('Attachment'), t('Size'));
  //edited
  $header = array();
  
  $rows = array();
  foreach ($files as $file) {
    if ($file->list) {
      $href = check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))));
	  //added for GA code
	  $attrs["onClick"] = "javascript:urchinTracker ("/downloads/$file->filename"); ";
      $text = check_plain($file->description ? $file->description : $file->filename);
      $rows[] = array(theme('file_icon', $file) ,l($text, $href,$attrs), '('.format_size($file->filesize).')');
    }
  }
  if (count($rows)) {
    return theme('table', $header, $rows, array('id' => 'attachments'));
  }
}

You'll need to see the reference link above for the phptemplate_file_icon function.
If you don't need to use file icons for attachments, you should be able to replace $rows[] with the following:
$rows[] = array(l($text, $href, $attrs), format_size($file->filesize));

captcha’s picture

To explain this a bit more, we need to add the link attributes, we add onClick to record the mouse click action; the operative lines are
$attrs["onClick"] = "javascript:urchinTracker ("/downloads/$file->filename"); ";

and the line where the link to the download is created, we add the link attributes array $attrs
l($text, $href,$attrs)

brmassa’s picture

Version: 4.7.x-1.x-dev » 5.x-1.x-dev
Assigned: Unassigned » brmassa

Guys,

a created a javascript that does this and track outgoing links. its required jquery, so its only useful for drupal 5.
see http://drupal.org/node/101509

regards,

massa

brmassa’s picture

Status: Active » Needs review
coreb’s picture

Status: Needs review » Closed (duplicate)

Since no patch exists in this issue to be reviewed, this should be marked as a duplicate of http://drupal.org/node/101509 , since that is where the actual patch exists.