Permalink
Browse files

Strip Google Analytics cookies to prepare for session caching.

See this link for an explanation of the problem that this changeset fixes:

http://www.randallmorey.com/blog/2010/feb/17/django-cache-sessions-and-google-analytics/
  • Loading branch information...
1 parent e1bdfd4 commit 06808c256879d20828553d829e382e0ad6203c56 @sjl sjl committed Jun 21, 2012
Showing with 12 additions and 0 deletions.
  1. +11 −0 middleware.py
  2. +1 −0 settings.py
View
@@ -1,3 +1,4 @@
+import re
import random
import time
@@ -187,3 +188,13 @@ def executemany(self, query, params_list):
django.db.backends.mysql.base.CursorWrapper = MetricsCursorWrapper
+
+# http://www.randallmorey.com/blog/2010/feb/17/django-cache-sessions-and-google-analytics/
+class StripGoogleAnalyticsCookieMiddleware(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
View
@@ -311,6 +311,7 @@ def rel(*x):
MIDDLEWARE_CLASSES = (
'middleware.ResponseTimeMiddleware',
+ 'middleware.StripGoogleAnalyticsCookieMiddleware',
'utils.ajaxmiddleware.AjaxErrorMiddleware',
'localeurl.middleware.LocaleURLMiddleware',
'django.middleware.common.CommonMiddleware',

0 comments on commit 06808c2

Please sign in to comment.