From 007fc794c38bf4d109ef146e47da8bd5b9d9d320 Mon Sep 17 00:00:00 2001 From: Andrii Marynets Date: Tue, 27 Jun 2017 23:47:30 +0300 Subject: [PATCH 1/6] Add google analytics to data center light --- datacenterlight/context_processor.py | 16 ++++++++++++++++ .../templates/datacenterlight/base.html | 3 +++ .../includes/_google_analytics.html | 15 +++++++++++++++ .../templates/datacenterlight/order_detail.html | 2 +- dynamicweb/settings/base.py | 5 +++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 datacenterlight/context_processor.py create mode 100644 datacenterlight/templates/datacenterlight/includes/_google_analytics.html diff --git a/datacenterlight/context_processor.py b/datacenterlight/context_processor.py new file mode 100644 index 00000000..b49ea364 --- /dev/null +++ b/datacenterlight/context_processor.py @@ -0,0 +1,16 @@ +from django.conf import settings + + +def google_analytics(request): + """ + Use the variables returned in this function to + render your Google Analytics tracking code template. + """ + ga_prop_id = getattr(settings, 'GOOGLE_ANALYTICS_PROPERTY_ID', False) + ga_domain = getattr(settings, 'GOOGLE_ANALYTICS_DOMAIN', False) + if not settings.DEBUG and ga_prop_id and ga_domain: + return { + 'GOOGLE_ANALYTICS_PROPERTY_ID': ga_prop_id, + 'GOOGLE_ANALYTICS_DOMAIN': ga_domain, + } + return {} diff --git a/datacenterlight/templates/datacenterlight/base.html b/datacenterlight/templates/datacenterlight/base.html index c62d92f7..bf0e0ca0 100644 --- a/datacenterlight/templates/datacenterlight/base.html +++ b/datacenterlight/templates/datacenterlight/base.html @@ -47,6 +47,9 @@ {% include "datacenterlight/includes/_footer.html" %} + + {% include "datacenterlight/includes/_google_analytics.html" %} + diff --git a/datacenterlight/templates/datacenterlight/includes/_google_analytics.html b/datacenterlight/templates/datacenterlight/includes/_google_analytics.html new file mode 100644 index 00000000..d074dfd3 --- /dev/null +++ b/datacenterlight/templates/datacenterlight/includes/_google_analytics.html @@ -0,0 +1,15 @@ + +{% if GOOGLE_ANALYTICS_PROPERTY_ID %} + +{% else %} + +{% endif %} + \ No newline at end of file diff --git a/datacenterlight/templates/datacenterlight/order_detail.html b/datacenterlight/templates/datacenterlight/order_detail.html index cb412558..8b53df5d 100644 --- a/datacenterlight/templates/datacenterlight/order_detail.html +++ b/datacenterlight/templates/datacenterlight/order_detail.html @@ -2,7 +2,7 @@ {% load staticfiles bootstrap3 %} {% load i18n %} {% block content %} - +{% include 'datacenterlight/includes/_google_analytics.html' %}
{% if messages %}
diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index d276f022..f3a80470 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -161,6 +161,7 @@ TEMPLATES = [ "django.contrib.messages.context_processors.messages", 'sekizai.context_processors.sekizai', 'cms.context_processors.cms_settings', + 'datacenterlight.context_processor.google_analytics', ], }, }, @@ -509,3 +510,7 @@ 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_ID = 'UA-62285904-9' +GOOGLE_ANALYTICS_DOMAIN = 'auto' From 2ad984504c584320ac7fe83125f2024df48028a3 Mon Sep 17 00:00:00 2001 From: Andrii Marynets Date: Fri, 30 Jun 2017 01:23:35 +0300 Subject: [PATCH 2/6] Add GOOGLE_ANALYTICS_PROPERTY_IDS for dynamic set ID --- datacenterlight/templates/datacenterlight/base.html | 6 +++--- .../templates/datacenterlight/order_detail.html | 3 +-- dynamicweb/settings/base.py | 9 ++++++--- .../google_analytics.html | 6 ++---- {datacenterlight => utils}/context_processor.py | 12 ++++++------ 5 files changed, 18 insertions(+), 18 deletions(-) rename datacenterlight/templates/datacenterlight/includes/_google_analytics.html => templates/google_analytics.html (75%) rename {datacenterlight => utils}/context_processor.py (52%) diff --git a/datacenterlight/templates/datacenterlight/base.html b/datacenterlight/templates/datacenterlight/base.html index bf0e0ca0..f4f659df 100644 --- a/datacenterlight/templates/datacenterlight/base.html +++ b/datacenterlight/templates/datacenterlight/base.html @@ -33,6 +33,9 @@ + + {% include "google_analytics.html" %} + @@ -47,9 +50,6 @@ {% include "datacenterlight/includes/_footer.html" %} - - {% include "datacenterlight/includes/_google_analytics.html" %} - diff --git a/datacenterlight/templates/datacenterlight/order_detail.html b/datacenterlight/templates/datacenterlight/order_detail.html index 8b53df5d..24511463 100644 --- a/datacenterlight/templates/datacenterlight/order_detail.html +++ b/datacenterlight/templates/datacenterlight/order_detail.html @@ -1,8 +1,7 @@ {% extends "hosting/base_short.html" %} {% load staticfiles bootstrap3 %} {% load i18n %} -{% block content %} -{% include 'datacenterlight/includes/_google_analytics.html' %} +{% block content %}
{% if messages %}
diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index f3a80470..31723853 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -138,6 +138,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,7 +162,7 @@ TEMPLATES = [ "django.contrib.messages.context_processors.messages", 'sekizai.context_processors.sekizai', 'cms.context_processors.cms_settings', - 'datacenterlight.context_processor.google_analytics', + 'utils.context_processor.google_analytics', ], }, }, @@ -512,5 +513,7 @@ DCL_TEXT = env('DCL_TEXT') DCL_SUPPORT_FROM_ADDRESS = env('DCL_SUPPORT_FROM_ADDRESS') # Settings for Google analytics -GOOGLE_ANALYTICS_PROPERTY_ID = 'UA-62285904-9' -GOOGLE_ANALYTICS_DOMAIN = 'auto' +GOOGLE_ANALYTICS_PROPERTY_IDS = { + 'datacenterlight.ch': 'UA-62285904-9', + 'digitalglarus.ch': 'UA-62285904-2' +} diff --git a/datacenterlight/templates/datacenterlight/includes/_google_analytics.html b/templates/google_analytics.html similarity index 75% rename from datacenterlight/templates/datacenterlight/includes/_google_analytics.html rename to templates/google_analytics.html index d074dfd3..64dbae40 100644 --- a/datacenterlight/templates/datacenterlight/includes/_google_analytics.html +++ b/templates/google_analytics.html @@ -1,4 +1,3 @@ - {% if GOOGLE_ANALYTICS_PROPERTY_ID %} {% else %} -{% endif %} - \ No newline at end of file +{% endif %} \ No newline at end of file diff --git a/datacenterlight/context_processor.py b/utils/context_processor.py similarity index 52% rename from datacenterlight/context_processor.py rename to utils/context_processor.py index b49ea364..43c27336 100644 --- a/datacenterlight/context_processor.py +++ b/utils/context_processor.py @@ -6,11 +6,11 @@ def google_analytics(request): Use the variables returned in this function to render your Google Analytics tracking code template. """ - ga_prop_id = getattr(settings, 'GOOGLE_ANALYTICS_PROPERTY_ID', False) - ga_domain = getattr(settings, 'GOOGLE_ANALYTICS_DOMAIN', False) - if not settings.DEBUG and ga_prop_id and ga_domain: + host = request.get_host() + ga_prop_id = getattr(settings, 'GOOGLE_ANALYTICS_PROPERTY_IDS', False).get(host) + print(ga_prop_id) + if ga_prop_id: return { - 'GOOGLE_ANALYTICS_PROPERTY_ID': ga_prop_id, - 'GOOGLE_ANALYTICS_DOMAIN': ga_domain, + 'GOOGLE_ANALYTICS_PROPERTY_ID': ga_prop_id } - return {} + return {} \ No newline at end of file From ebf8b0646d7a3ca94b79307dee7406ad497a15ff Mon Sep 17 00:00:00 2001 From: Andrii Marynets Date: Fri, 30 Jun 2017 01:33:12 +0300 Subject: [PATCH 3/6] Fix not add analytics if DEBUG=True --- utils/context_processor.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/utils/context_processor.py b/utils/context_processor.py index 43c27336..740591ba 100644 --- a/utils/context_processor.py +++ b/utils/context_processor.py @@ -8,9 +8,8 @@ def google_analytics(request): """ host = request.get_host() ga_prop_id = getattr(settings, 'GOOGLE_ANALYTICS_PROPERTY_IDS', False).get(host) - print(ga_prop_id) - if ga_prop_id: + if not settings.DEBUG and ga_prop_id: return { 'GOOGLE_ANALYTICS_PROPERTY_ID': ga_prop_id } - return {} \ No newline at end of file + return {} From 9431e9846c9d88ed0ed7cd7ec9ffd370a32eae79 Mon Sep 17 00:00:00 2001 From: Andrii Marynets Date: Fri, 30 Jun 2017 08:44:39 +0300 Subject: [PATCH 4/6] Add middleware for fix "get_host() method fails when the host is behind multiple proxies" --- dynamicweb/settings/base.py | 4 +++- utils/middleware.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 utils/middleware.py diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 31723853..08ed7fe5 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', @@ -515,5 +516,6 @@ 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' + 'digitalglarus.ch': 'UA-62285904-2', + '127.0.0.1:8000': 'test' } 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 From 1f658a425f5bdea15eb9cbd981af466bdee20c56 Mon Sep 17 00:00:00 2001 From: Andrii Marynets Date: Sun, 2 Jul 2017 17:56:49 +0300 Subject: [PATCH 5/6] Merge with master --- digitalglarus/templates/base_glarus.html | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) 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 @@ - + + {% include 'google_analytics.html' %} + From 5ea35bd4d8966759c0f30182a6aa7be554712483 Mon Sep 17 00:00:00 2001 From: Andrii Marynets Date: Sun, 2 Jul 2017 23:00:56 +0300 Subject: [PATCH 6/6] add ids for dev and staging server --- dynamicweb/settings/base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 08ed7fe5..ba304ff9 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -517,5 +517,7 @@ DCL_SUPPORT_FROM_ADDRESS = env('DCL_SUPPORT_FROM_ADDRESS') GOOGLE_ANALYTICS_PROPERTY_IDS = { 'datacenterlight.ch': 'UA-62285904-9', 'digitalglarus.ch': 'UA-62285904-2', - '127.0.0.1:8000': 'test' + '127.0.0.1:8000': 'localhost', + 'dynamicweb-development.ungleich.ch': 'development', + 'dynamicweb-staging.ungleich.ch': 'staging' }