Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

The middleware doesn't like the new analytics code, so I'm just addin…

…g it manually.
  • Loading branch information...
commit bb14304456c9563f5e60f22d28763f6da6d04bb9 1 parent 1caa184
Bryan Veloso bryanveloso authored
0  applications/__init__.py
No changes.
123 applications/core/middleware/analytics.py
... ... @@ -1,123 +0,0 @@
1   -"""
2   - Analytics Middleware for Django
3   - Copyright (C) 2007 Will R Larson (lethain@gmail.com)
4   -
5   - This library is free software; you can redistribute it and/or
6   - modify it under the terms of the GNU Lesser General Public
7   - License as published by the Free Software Foundation; either
8   - version 2.1 of the License, or (at your option) any later version.
9   -
10   - This library is distributed in the hope that it will be useful,
11   - but WITHOUT ANY WARRANTY; without even the implied warranty of
12   - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   - Lesser General Public License for more details.
14   -
15   - You should have received a copy of the GNU Lesser General Public
16   - License along with this library; if not, write to the Free Software
17   - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18   -
19   -
20   - This is a fairly simple Middleware that will inject the necessary html
21   - in order to hook pages generated by Django with your Google Analytics
22   - account.
23   -
24   - Usage
25   - add a global variable ANALYTICS_ID = "myGoogleAnalyticsID" to your settings.py
26   - file. You may optionally add an ANALYTICS_IGNORE_ADMIN = True field to your
27   - settings.py if you would like for pages rendered for admin users to not
28   - have the Google Analytics code injected.
29   -
30   - The last step to enable the AnalyticsMiddleware is to then add the
31   - middleware to the MIDDLEWARE_CLASSES tuple within settings.py.
32   - The exact entry will vary on where you place the middleware,
33   - but if you place the analytics.py file within your django.middleware
34   - folder, then the entry would look like:
35   - 'django.middleware.analytics.GoogleAnalyticsMiddleware',
36   - Alternatively if you placed it within a project folder, your entry
37   - might look like
38   - 'myproject.analytics.GoogleAnalyticsMiddleware',
39   -
40   - The position of this entry within the MIDDLE_WARE_CLASSES
41   - list is not signifigant (it can be the first, the last, or
42   - anywhere inbetween).
43   -"""
44   -
45   -
46   -from django.conf import settings
47   -
48   -
49   -class GoogleAnalyticsMiddleware(object):
50   - def __init__(self):
51   - 'Constructor for GoogleAnalyticsMiddleware'
52   - try:
53   - # if id has been set in settings.py,
54   - # store it locally and set process_response
55   - # to insert the analytics code
56   - id = settings.ANALYTICS_ID
57   - self.html = self.form_analytics_string(id)
58   -
59   - # uncomment below to test without
60   - # actually sending data to Google Analytics
61   - #self.html = "<h2>GOOGLE_ANALYTICS_MIDDLEWARE_IS_ACTIVE</h2>"
62   -
63   - self.process_response = self.insert_analytics
64   - try:
65   - # check settings.py for ANALYTICS_IGNORE_ADMIN
66   - # if true then don't render analytics for
67   - # pages loaded by admins
68   - ignore = settings.ANALYTICS_IGNORE_ADMIN
69   - if ignore is True:
70   - self.process_response = self.ignore_admin(
71   - self.process_response)
72   - except AttributeError:
73   - pass
74   -
75   - except AttributeError:
76   - # if no id is found in settings.py,
77   - # then we will simply return any response
78   - # unaltered
79   - self.process_response = self.return_unaltered
80   -
81   -
82   - def insert_analytics(self, request, response):
83   - 'Insert Google Analytics call into response.'
84   - content = response.content
85   - index = content.upper().find('</BODY>')
86   - if index == -1: return response
87   - response.content = content[:index] +\
88   - self.html +\
89   - content[index:]
90   - return response
91   -
92   -
93   - def return_unaltered(self, request, response):
94   - 'Return response without making any changes.'
95   - return response
96   -
97   -
98   - @staticmethod
99   - def ignore_admin(func):
100   - 'Curry ignore_if_admin onto function passed as argument. '
101   - def ignore_if_admin(request, response):
102   - if request.user.is_authenticated() and request.user.is_staff:
103   - return response
104   - else:
105   - return func(request, response)
106   -
107   - return ignore_if_admin
108   -
109   -
110   - @staticmethod
111   - def form_analytics_string(id):
112   - 'Generate html for calling Google Analytics.'
113   - return """
114   -<script type="text/javascript">
115   - var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
116   - document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
117   -</script>
118   -<script type="text/javascript">
119   - var pageTracker = _gat._getTracker("%s");
120   - pageTracker._initData();
121   - pageTracker._trackPageview();
122   -</script>
123   -""" % ( str(id) )
1  settings.py
@@ -35,7 +35,6 @@
35 35 'django.middleware.doc.XViewMiddleware',
36 36 'django.middleware.http.ConditionalGetMiddleware',
37 37 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
38   - 'applciations.core.middleware.analytics.GoogleAnalyticsMiddleware', # Custom Middleware
39 38 )
40 39 USE_ETAGS = True
41 40 APPEND_SLASH = True
10 templates/base.html
@@ -58,6 +58,16 @@ <h1 class="grid_10"><a href="/">Django Pluggables</a></h1>
58 58 <div id="footer" class="container_16 clearfix">
59 59 <p class="grid_16">Django Pluggables. From <a href="http://revyver.com">Revyver</a>, with love. &copy;{% now "Y" %} All rights reserved. <a href="http://djangoproject.com/">Django</a> is a registered trademark of the <a href="http://www2.ljworld.com/">Lawrence Journal-World</a>.</p>
60 60 </div>
  61 +
  62 + <script type="text/javascript">
  63 + var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  64 + document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  65 + </script>
  66 + <script type="text/javascript">
  67 + var pageTracker = _gat._getTracker("UA-4090048-6");
  68 + pageTracker._initData();
  69 + pageTracker._trackPageview();
  70 + </script>
61 71 </body>
62 72
63 73 {% endautoescape %}

0 comments on commit bb14304

Please sign in to comment.
Something went wrong with that request. Please try again.