Strip Google Analytics cookies for caching middleware purposes

October 24, 2009
7 (after 7 ratings)

You may notice that using Google Analytics's 'urchin' with the CacheMiddleware and SessionMiddleware or AuthenticationMiddleware middleware classes means that nothing is ever cached.

Google Analytics updates its cookies with every page view, and the Auth/Session middlewares add cookies to the caching engine's 'Vary' list. This means every page view is given a unique cache key.

This middleware class will remove urchin's '__utmX' cookies from the request object before it hits the caching middleware. Put it at the top of your MIDDLEWARE_CLASSES in settings.py.

nf / [email protected]

import re

class StripCookieMiddleware(object):
    strip_re = re.compile(r'(__utm.=.+?(?:; |$))')
    def process_request(self, request):
            cookie = self.strip_re.sub('', request.META['HTTP_COOKIE'])
            request.META['HTTP_COOKIE'] = cookie
        except: pass

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 4 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 4 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 11 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year ago
  5. Help text hyperlinks by sa2812 1 year ago


smulloni (on October 25, 2009):

Google ads introduces cookies with a similar effect, and also use a double-underscore prefix. So I'll trying this regex to deal with both:

strip_re = re.compile(r'\b(__[^=]+=.+?(?:; |$))')

Also, this will reserve the double underscore namespace for any cookies I may want to plant that shouldn't affect server-side caching.


nf (on October 26, 2009):

Good idea, smulloni!

I've just updated the code to wrap the replacement in a try/except, as if the user had no cookies when visiting the site it would raise a KeyError. Not good!


andybak (on October 26, 2009):

How does this relate to #6552 and #9249? I haven't looked into the caching internals in any depth and I naively assumed it was a 'switch on and forget' type of thing.

Does #6552 fix this? If not - does this snippet interfere with the workings of Google Analytics?


smulloni (on October 26, 2009):

The patch for Django bug #6552 doesn't fix this.

This snippet should have no effect on google analytics. It doesn't prevent the client from having the cookie or reporting it to google; it does cause Django itself to ignore the cookie. If you want to access google analytics cookies from within django, for some reason, this would make it impossible. But I doubt that you do.


italomaia (on January 11, 2011):

This snippet worked really nice. Thanks!


sk1p (on July 6, 2011):

Please be aware that using

strip_re = re.compile(r'\b(__[^=]+=.+?(?:; |$))')

as proposed in the first comment, will cause problems with the messaging framework (django.contrib.messages) in current django versions, as it uses __json_message for storing messages in cookies.


Please login first before commenting.