How to setup FeedBurner Stats on Drupal

FeedBurnerI just setup FeedBurner Stats tracking code on this blog and had a few problems with the Drupal code that FeedBurner supplies. The main problem is that it doesn't get the proper page URL when put into a block.

Putting javascript tracking codes in blocks has a number of benefits, the main ones being that you can use the block visibility settings to show it only for specific user roles (eg - not site administrators).

The code that Feedburner provide is:

This code will work properly if placed in the Node.tpl.php file in your theme directory - which isn't a great option because

  1. It's hard to limit the roles that are tracked and
  2. It will only show on node pages and not any other system pages or views
  3. Not everyone has access to the theme files and wants to mess around with them

Here is the modified code that will work when placed in a block.

The code is by no means perfect but it does the job. If you are not using clean URLs (who isn't these days) you may want to add ?q= after otherwise the URLs in FeedBurner Stats won't match up correctly to the URLs on your site.


I created a content type 'widgets'. I then created a feedburner widget, and a statcounter widget and a google analytics widget. Then I created a View for this content type and provided a block. That block is then only displayed for unauthenticated and non-admin roles.

Hi Paul,

That seems like a lot of effort for such a small thing. Why not just put all the code into one block?

Addendum to previous comment: Which means I've obviously not really been looking at my stats in detail to see that I'm missing a lot of information.

This type of functionality is exactly what the url() function is for. This would eliminate the clean url's issue, and also let you track the aliased path rather than the standard drupal path.

Here's the updated code:

<script src="<?php print url($_GET['q'], NULL, NULL, TRUE) ?>" type="text/javascript" charset="utf-8"></script>

The additional arguments for the url parameter are necessary so that the url printed will be absolute.

Hi Roger,

I was thinking of doing this but I ended up deciding against it because I wanted to know whether people were accessing the pages from their system path (node/x) or their aliased path. If that is not an issue for you, then I agree that your code would be more suitable.

Or use the excellent FeedBurner module:

I installed the FeedBurner module and it doesn't look like it has stats functionality yet. I think that is still a future plan.

Add new comment