- 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
- Google Analytics Template Tag by blinks 8 years ago
- Cookieless Session Middleware by juliocarlos 6 years, 11 months ago
- New view decorator to only cache pages for anonymous users by vaughnkoch 5 years, 1 month ago
- Set test cookie unless logged in by nipuL 7 years, 8 months ago
- Google Analytics Templatetag by rizumu 7 years, 1 month ago