Google Analytics, Tomcat and truncated cookies

Some time ago I posted an example of feeding Google Analytics with the backend data. The code worked fine in production except for one little thing – we were missing the referrer in the cookie leaving our friendly marketing people guess about the origins of e-commerce transactions. That’s not exactly what makes them happy.

A closer inspection of the Google Analytics cookies reveals that the __utmz cookie is truncated just before the “=” sign. There’s a monster in the house! Let’s take a look.

A good crunchy and tasty __utmz cookie processed by CF/JRun looks like that:

The very same cookie processed by CF/Tomcat looks different:

Let’s take a look at the same cookie on Railo/Tomcat:

As you see, both Railo and CF running on Tomcat truncate the cookie value string.

Tracking the headers in Chrome revealed nothing strange – all Analytics cookies were intact, the browser behaved correctly sending the cookie to the server without any alterations. The codebase was exactly the same. The only difference was the engine. It took me some time to find out what was going on until I stumbled upon a very good explanation by Nick Nett.

The summary: your Tomcat 6 dislikes “=” in cookie values. Whatever you do, it truncates the value to the last character before the “=” sign. The easiest solution to resolve this issue is to add a couple of lines to your catalina.property settings file. Mine is located at /Developer/Railo/tomcat/conf/catalina.properties. Just add these two lines to the end of your properties file:

org.apache.tomcat.util.http.ServerCookie.ALLOW_EQUALS_IN_VALUE=true
org.apache.tomcat.util.http.ServerCookie.ALLOW_HTTP_SEPARATORS_IN_V0=true

Restart your Tomcat:

cd /Developer/Railo/tomcat/bin/
sudo sh ./shutdown.sh
sudo sh ./startup.sh

The problem is solved, your Tomcat will from now on refrain from munching your Google Analytics cookies:

Have fun!