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]
1 2 3 4 5 6 7 8 9 | import re
class StripCookieMiddleware(object):
strip_re = re.compile(r'(__utm.=.+?(?:; |$))')
def process_request(self, request):
try:
cookie = self.strip_re.sub('', request.META['HTTP_COOKIE'])
request.META['HTTP_COOKIE'] = cookie
except: pass
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 5 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
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:
Also, this will reserve the double underscore namespace for any cookies I may want to plant that shouldn't affect server-side caching.
#
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!
#
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?
#
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.
#
This snippet worked really nice. Thanks!
#
Please be aware that using
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.