- October 24, 2009
- middleware cache google analytics
- 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]
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
- "Magic Link" Management Command by webology 3 weeks, 4 days ago
- Closest ORM models to a latitude/longitude point by simonw 3 weeks, 4 days ago
- Log the time taken to execute each DB query by kennyx46 3 weeks, 4 days ago
- django database snippet by ItsRLuo 1 month ago
- Serialize a model instance by chriswedgwood 1 month, 4 weeks ago