diff --git a/datacenterlight/templates/datacenterlight/base.html b/datacenterlight/templates/datacenterlight/base.html
index 6074ece9..9f666c62 100644
--- a/datacenterlight/templates/datacenterlight/base.html
+++ b/datacenterlight/templates/datacenterlight/base.html
@@ -33,6 +33,9 @@
         <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
         <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
     <![endif]-->
+    <!-- Google analytics -->
+    {% include "google_analytics.html" %}
+    <!-- End Google Analytics -->
 </head>
 
 <body>
diff --git a/datacenterlight/templates/datacenterlight/order_detail.html b/datacenterlight/templates/datacenterlight/order_detail.html
index 42f916e3..273074cf 100644
--- a/datacenterlight/templates/datacenterlight/order_detail.html
+++ b/datacenterlight/templates/datacenterlight/order_detail.html
@@ -67,7 +67,7 @@
                     <p><b>{% trans "Disk space"%}</b> <span class="pull-right">{{vm.disk_size}} GB</span></p>
                     <hr>
                     <p><b>{% trans "Configuration"%}</b> <span class="pull-right">{{request.session.template.name}}</span></p>
-                    <hr>                    
+                    <hr>
                     <h4>{% trans "Total"%}<p class="pull-right"><b>{{vm.price}} CHF</b></p></h4>
                 {% endwith %}
             </div>
@@ -83,8 +83,8 @@
     {% endif %}
 </div>
 
- 
-<script type="text/javascript"> 
+
+<script type="text/javascript">
 
     window.onload = function () {
             var locale_date = moment.utc(document.getElementById("order-created_at").textContent,'YYYY-MM-DD HH:mm').toDate();
diff --git a/digitalglarus/templates/base_glarus.html b/digitalglarus/templates/base_glarus.html
index 424f3aac..b94c813e 100644
--- a/digitalglarus/templates/base_glarus.html
+++ b/digitalglarus/templates/base_glarus.html
@@ -39,27 +39,13 @@
     <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
     <!--[if lt IE 9]>
-    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
-    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
+        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
+        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
     <![endif]-->
 
-    <script>
-        (function (i, s, o, g, r, a, m) {
-            i['GoogleAnalyticsObject'] = r;
-            i[r] = i[r] || function () {
-                        (i[r].q = i[r].q || []).push(arguments)
-                    }, i[r].l = 1 * new Date();
-            a = s.createElement(o),
-                    m = s.getElementsByTagName(o)[0];
-            a.async = 1;
-            a.src = g;
-            m.parentNode.insertBefore(a, m)
-        })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
-
-        ga('create', 'UA-62285904-2', 'auto');
-        ga('send', 'pageview');
-
-    </script>
+    <!-- Google analytics -->
+    {% include 'google_analytics.html' %}
+    <!-- End Google Analytics -->
 </head>
 
 <body>
diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py
index d276f022..ba304ff9 100644
--- a/dynamicweb/settings/base.py
+++ b/dynamicweb/settings/base.py
@@ -118,6 +118,7 @@ INSTALLED_APPS = (
 
 MIDDLEWARE_CLASSES = (
     'django.contrib.sessions.middleware.SessionMiddleware',
+    'utils.middleware.MultipleProxyMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
@@ -138,6 +139,7 @@ TEMPLATES = [
     {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
         'DIRS': [os.path.join(PROJECT_DIR, 'cms_templates/'),
+                 os.path.join(PROJECT_DIR, 'templates'),
                  os.path.join(PROJECT_DIR, 'cms_templates/djangocms_blog/'),
                  os.path.join(PROJECT_DIR, 'membership'),
                  os.path.join(PROJECT_DIR, 'hosting/templates/'),
@@ -161,6 +163,7 @@ TEMPLATES = [
                 "django.contrib.messages.context_processors.messages",
                 'sekizai.context_processors.sekizai',
                 'cms.context_processors.cms_settings',
+                'utils.context_processor.google_analytics',
             ],
         },
     },
@@ -509,3 +512,12 @@ OPENNEBULA_ENDPOINT = env('OPENNEBULA_ENDPOINT')
 # dcl email configurations
 DCL_TEXT = env('DCL_TEXT')
 DCL_SUPPORT_FROM_ADDRESS = env('DCL_SUPPORT_FROM_ADDRESS')
+
+# Settings for Google analytics
+GOOGLE_ANALYTICS_PROPERTY_IDS = {
+    'datacenterlight.ch': 'UA-62285904-9',
+    'digitalglarus.ch': 'UA-62285904-2',
+    '127.0.0.1:8000': 'localhost',
+    'dynamicweb-development.ungleich.ch': 'development',
+    'dynamicweb-staging.ungleich.ch': 'staging'
+}
diff --git a/templates/google_analytics.html b/templates/google_analytics.html
new file mode 100644
index 00000000..64dbae40
--- /dev/null
+++ b/templates/google_analytics.html
@@ -0,0 +1,13 @@
+{% if GOOGLE_ANALYTICS_PROPERTY_ID %}
+    <script>
+        (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+        })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
+
+        ga('create', '{{ GOOGLE_ANALYTICS_PROPERTY_ID }}', 'auto');
+        ga('send', 'pageview');
+    </script>
+{% else %}
+    <!-- Empty analytics -->
+{% endif %}
\ No newline at end of file
diff --git a/utils/context_processor.py b/utils/context_processor.py
new file mode 100644
index 00000000..740591ba
--- /dev/null
+++ b/utils/context_processor.py
@@ -0,0 +1,15 @@
+from django.conf import settings
+
+
+def google_analytics(request):
+    """
+    Use the variables returned in this function to
+    render your Google Analytics tracking code template.
+    """
+    host = request.get_host()
+    ga_prop_id = getattr(settings, 'GOOGLE_ANALYTICS_PROPERTY_IDS', False).get(host)
+    if not settings.DEBUG and ga_prop_id:
+        return {
+            'GOOGLE_ANALYTICS_PROPERTY_ID': ga_prop_id
+        }
+    return {}
diff --git a/utils/middleware.py b/utils/middleware.py
new file mode 100644
index 00000000..d37bdbfd
--- /dev/null
+++ b/utils/middleware.py
@@ -0,0 +1,17 @@
+class MultipleProxyMiddleware(object):
+    FORWARDED_FOR_FIELDS = [
+        'HTTP_X_FORWARDED_FOR',
+        'HTTP_X_FORWARDED_HOST',
+        'HTTP_X_FORWARDED_SERVER',
+    ]
+
+    def process_request(self, request):
+        """
+        Rewrites the proxy headers so that only the most
+        recent proxy is used.
+        """
+        for field in self.FORWARDED_FOR_FIELDS:
+            if field in request.META:
+                if ',' in request.META[field]:
+                    parts = request.META[field].split(',')
+                    request.META[field] = parts[-1].strip()
\ No newline at end of file