Google Analytics without javascript!

Yes, it is possible to send data to Google Analytics without executing javascript! In this tutorial I will try to explain how this can be done and I will give some good examples. Server side analytics enables you to measure data like rss, image or pdf visits.

Analytics without javascript?
When you look at the analytics javascript code you see that it combines several sets of data into an image request. This image request sends the right data to Google (not the javascript). When you know what url you should use for the image, you can call the image directly and send the same data. Of course you need to be able to request the image url and that isn’t easy from another image, rss feed or pdf. This is why we request it “server side”.

Server side image requests
Javascript is “client side”; this means the browser does all the calculations and actions that the script requires. PHP, ASP, Perl and many others are server side scripting languages; which means the server that hosts the website does all required calculations. This means javascript knows more about the action of the user and PHP can easily request data from a database or other source.

PHP (I’ll focus on PHP in this example) can also request a file from the internet without executing or showing it. Just requesting the analytics image is enough to trigger a hit in your report and it will show up in your account. The downside of requesting the image from your server is: You lose the IP address and other data that Google records at the moment the image is requested. The server IP of your web server will be recorded.

Server side requests in a client side report will obscure your data. Create a separate Analytics profile for everything you log server side.

Who requests my RSS feed?
RSS feeds don’t execute javascript, but in some feed readers you can execute javascript once a post is read. This is why you need to log requests of your RSS XML file differently. You can either use a service like Feedburner or send an Analytics image request from PHP or ASP. The following example shows how you send specific data to Google Analytics from your RSS in PHP.

  1. Create a new analytics profile with its own tracking code (not a copy of an existing profile). Activate the profile by temporarily changing the code on your homepage. After activating you return it to the original one.
  2. Add the following PHP code to the file that produces your RSS (preferably at the bottom): urchin-image.txt
  3. Update the urchin code, domain, user defined variable (if desired) and the fake page request you’d like to track.
  4. I left all the other data intact because it works with them and they don’t impact the report, but when I have time I can probably clean it up some more.

Tracking PDF or Image requests
The script that produces your RSS feed is already executing PHP code. Images and PDF files don’t execute anything, so you need to add something extra to track these in a similar way. The following code shows you how you can request an image, PDF, SWF or for instance downloads and request the Google Analytics (Urchin) code at the same time.

  1. Find out if your server supports mod_rewrite. If it does, you can produce cleaner URLs. As a RewriteRule in your htaccess you add something like: “RewriteRule ^tracker/(.*).gif$ /tracker.php?url=$1&filetype=gif [L]“. This requests the file tracker.php whenever a gif image from the (non-existing) directory tracker is requested. You can do the same with all other filetypes that don’t execute PHP code themselves.
  2. Create tracker.php and add something like the following code: tracker.txt. Customize the script where needed and add content type headers for everything you’ve added to the htaccess.
  3. Now you can open the extra profile you should have created and under “Content Optimization > Content Performance > Content Drilldown” you find the directory “tracker” with all image/file requests logged in it.

Here are all values you can fill: image-url-explained.txt

Most of the information needed for the statistics is gathered by the javascript file (and you can influence that data), but the other half is collected when the image is requested from the Google server. This means that that information belongs to your server and not to the user that did the real request. Converting server-side to client-side analytics is very usefull, but not flawless. I haven’t seen this type of Analytics tweak anywhere else, so please let me know if you would like me to post more similar tweaks or more information in the future.

Google Analytics is very good once you implement some of the following tweaks.

Comments are closed.