Rogue Wolves is the personal site of .

I'm currently a research scientist with Oculus Info Inc. in Toronto, Ontario Canada.

My research interests include: adaptive user interfaces, machine learning, Bayesian reasoning and distributed artificial intelligence.

Drupal and FeedBurner StandardStats

I've decided to try out the FeedBurner StandardStats feature to keep an eye on various site statistics such as referrers, geographic location of visitors, browsers used, etc.

Note: This is something the Drupal logs could do a much better job at. A nice dashboard similar to what StandardStats gives you with pretty graphs, etc.

The FeedBurner instructions to integrate StandardStats into your site didn't work well for me. There are Drupal specific instructions, but they fail to mention which template file you should put the code into. Here is the code FeedBurner suggests:

<script src="<?php print $node_url ?>" type="text/javascript" charset="utf-8"></script>

You will need to change YOURFEEDNAME and to your site specific info. After some reading I found that $node_url is only available in Node.tpl.php and not in Page.tpl.php so the code must go into Node.tpl.php. At first I thought it was working but then I noticed that for anonymous users (the majority of site visitors) the script was not showing up in the html. After a bit of head scratching I realized this must be a cache issue. Now how to clear the Drupal cache? I thought there used to be a button on one of the admin screens to do this? I can't seem to find it (if it ever existed) so I had to clear the cache the manual way: I truncated the cache_page table in mysql. Bingo! Things were working now.

Note: Drupal really should have an admin feature to clear the cache.

After some more testing I noticed a problem with this technique. Using the Node.tpl.php will cause the FeedBurner script to be repeated for every node that is on the page. For node view/edit pages there is only one node so this is not an issue, but for the front page or a node list page (term pages, etc) the script will be included for each node. This means that if a user visits the front page of your site in FeedBurner StandardStats will report the user visited every node listed on the front page. Needless to say this will skew your stats. I'd rather be more accurate and know that they visited the front page.

To fix this behaviour I decided to not put the script in Node.tpl.php. Instead I put code in Page.tpl.php so there is only one notification to FeedBurner StandardStats: whether the user is on the front page or on a particular node page. Since the $node_url variable is only applicable to Node.tpl.php I needed a new technique to report the node url. My first attempt was to add this code to the Page.tpl.php:

<script src="<?php print $node->path ?>" type="text/javascript" charset="utf-8"></script>

This script worked for logged in users, but mysteriously $node->path always returned blank for anonymous users. After some digging on, I found this bug report about $node->path returning null for anonymous users. They have a good work around: Use url("node/$node->nid") instead of $node->path

My working script is as follows:

<script src="<?php if ($node->nid != "") print url("node/$node->nid"); ?>" type="text/javascript" charset="utf-8"></script>

The if ($node->nid != "") will only append the node url if we are currently on a node page. This isn't a perfect solution, since this will only return a specific node url or your root url (ex: A better solution would always return the actual URL being visited whether it is a search page, term page, node page, etc. Consider that a future enhancement...

I also wanted to mention Dave Ried's new FeedBurner Module. I have not yet tried it out but it sounds like it has great promise and will eliminate the need to do much of this kind of stuff manually:

Integrates Drupal with the services provided by FeedBurner. Currently this module provides the means to redirect requests for your site's feeds to user-specified/created FeedBurner feeds. Special user agents, like FeedBurner and Feed Validator (this can be customized) are still allowed access to the direct feeds so there is no need for any special .htaccess hacking.

Update: Here is a link to another way you can integrate FeedBurner StandardStats.