From 5c17ae24b0f3078b175f18463b72991489254903 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Fri, 25 Jan 2019 17:21:21 -0500 Subject: [PATCH 1/6] Make Google Analytics Tracking ID configurable --- Dockerfile | 1 + app/app/settings.py | 5 +++++ app/server/templates/base.html | 10 ++-------- app/server/templates/tags/google_analytics.html | 10 ++++++++++ app/server/templatetags/__init__.py | 0 app/server/templatetags/analytics.py | 10 ++++++++++ 6 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 app/server/templates/tags/google_analytics.html create mode 100644 app/server/templatetags/__init__.py create mode 100644 app/server/templatetags/analytics.py diff --git a/Dockerfile b/Dockerfile index 313abd72..ac7fd99f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,7 @@ ENV DEBUG="True" ENV SECRET_KEY="change-me-in-production" ENV BIND="0.0.0.0:80" ENV WORKERS="2" +ENV GOOGLE_TRACKING_ID="" EXPOSE 80 diff --git a/app/app/settings.py b/app/app/settings.py index c1286f3c..66a6e879 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -80,6 +80,9 @@ TEMPLATES = [ 'social_django.context_processors.backends', 'social_django.context_processors.login_redirect', ], + 'libraries': { + 'analytics': 'server.templatetags.analytics', + }, }, }, ] @@ -181,4 +184,6 @@ SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') # on the import phase IMPORT_BATCH_SIZE = 500 +GOOGLE_TRACKING_ID = os.getenv('GOOGLE_TRACKING_ID', 'UA-125643874-2') + django_heroku.settings(locals(), test_runner=False) diff --git a/app/server/templates/base.html b/app/server/templates/base.html index 1639f8e0..91574c1f 100644 --- a/app/server/templates/base.html +++ b/app/server/templates/base.html @@ -1,15 +1,9 @@ {% load static %} +{% load analytics %} - - - + {% google_analytics %} diff --git a/app/server/templates/tags/google_analytics.html b/app/server/templates/tags/google_analytics.html new file mode 100644 index 00000000..eebdd1ab --- /dev/null +++ b/app/server/templates/tags/google_analytics.html @@ -0,0 +1,10 @@ +{% if google_tracking_id %} + + + +{% endif %} diff --git a/app/server/templatetags/__init__.py b/app/server/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/server/templatetags/analytics.py b/app/server/templatetags/analytics.py new file mode 100644 index 00000000..9667a560 --- /dev/null +++ b/app/server/templatetags/analytics.py @@ -0,0 +1,10 @@ +from django import template + +from app import settings + +register = template.Library() + + +@register.inclusion_tag('tags/google_analytics.html') +def google_analytics(): + return {'google_tracking_id': settings.GOOGLE_TRACKING_ID} From 7f6cce2f6e2293ff2a0b2a783640e1765c6165d8 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Sun, 27 Jan 2019 14:14:24 -0500 Subject: [PATCH 2/6] Add client-side analytics via Azure AppInsights See https://docs.microsoft.com/en-us/azure/azure-monitor/app/javascript --- Dockerfile | 1 + app/app/settings.py | 1 + app/server/templates/base.html | 1 + app/server/templates/tags/azure_appinsights.html | 14 ++++++++++++++ app/server/templatetags/analytics.py | 8 ++++++++ 5 files changed, 25 insertions(+) create mode 100644 app/server/templates/tags/azure_appinsights.html diff --git a/Dockerfile b/Dockerfile index ac7fd99f..9c7fc973 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,7 @@ ENV SECRET_KEY="change-me-in-production" ENV BIND="0.0.0.0:80" ENV WORKERS="2" ENV GOOGLE_TRACKING_ID="" +ENV AZURE_APPINSIGHTS_IKEY="" EXPOSE 80 diff --git a/app/app/settings.py b/app/app/settings.py index 66a6e879..7a358791 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -185,5 +185,6 @@ SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') IMPORT_BATCH_SIZE = 500 GOOGLE_TRACKING_ID = os.getenv('GOOGLE_TRACKING_ID', 'UA-125643874-2') +AZURE_APPINSIGHTS_IKEY = os.getenv('AZURE_APPINSIGHTS_IKEY') django_heroku.settings(locals(), test_runner=False) diff --git a/app/server/templates/base.html b/app/server/templates/base.html index 91574c1f..c8a4c25c 100644 --- a/app/server/templates/base.html +++ b/app/server/templates/base.html @@ -35,6 +35,7 @@ {% block header %}{% endblock %} + {% azure_appinsights %} diff --git a/app/server/templates/tags/azure_appinsights.html b/app/server/templates/tags/azure_appinsights.html new file mode 100644 index 00000000..951e906b --- /dev/null +++ b/app/server/templates/tags/azure_appinsights.html @@ -0,0 +1,14 @@ +{# See https://apmtips.com/blog/2015/03/18/javascript-snippet-explained/ #} +{% if azure_appinsights_ikey %} + +{% endif %} diff --git a/app/server/templatetags/analytics.py b/app/server/templatetags/analytics.py index 9667a560..a623cb71 100644 --- a/app/server/templatetags/analytics.py +++ b/app/server/templatetags/analytics.py @@ -8,3 +8,11 @@ register = template.Library() @register.inclusion_tag('tags/google_analytics.html') def google_analytics(): return {'google_tracking_id': settings.GOOGLE_TRACKING_ID} + + +@register.inclusion_tag('tags/azure_appinsights.html') +def azure_appinsights(): + return { + 'DEBUG': settings.DEBUG, + 'azure_appinsights_ikey': settings.AZURE_APPINSIGHTS_IKEY, + } From 3d199f8e938c32f94b8e4a8a8182417f77588d83 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Mon, 28 Jan 2019 07:52:35 -0500 Subject: [PATCH 3/6] Inline env variable --- azuredeploy.json | 52 +++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/azuredeploy.json b/azuredeploy.json index 2174b146..7e89be35 100644 --- a/azuredeploy.json +++ b/azuredeploy.json @@ -102,24 +102,6 @@ "databaseServerName": "[concat(parameters('appName'),'-state')]", "setupScriptName": "[concat(parameters('appName'),'-setup')]", "appServicePlanName": "[concat(parameters('appName'),'-hosting')]", - "env": [ - { - "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE", - "value": "false" - }, - { - "name": "DEBUG", - "value": "False" - }, - { - "name": "SECRET_KEY", - "value": "[parameters('secretKey')]" - }, - { - "name": "DATABASE_URL", - "value": "[variables('databaseConnectionString')]" - } - ] }, "resources": [ { @@ -199,7 +181,20 @@ "[parameters('adminContactEmail')]", "[parameters('adminPassword')]" ], - "environmentVariables": "[variables('env')]", + "environmentVariables": [ + { + "name": "DEBUG", + "value": "False" + }, + { + "name": "SECRET_KEY", + "value": "[parameters('secretKey')]" + }, + { + "name": "DATABASE_URL", + "value": "[variables('databaseConnectionString')]" + } + ], "resources": { "requests": { "cpu": "1", @@ -231,7 +226,24 @@ "siteConfig": { "linuxFxVersion": "[concat('DOCKER|', parameters('dockerImageName'))]", "alwaysOn": true, - "appSettings": "[variables('env')]" + "appSettings": [ + { + "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE", + "value": "false" + }, + { + "name": "DEBUG", + "value": "False" + }, + { + "name": "SECRET_KEY", + "value": "[parameters('secretKey')]" + }, + { + "name": "DATABASE_URL", + "value": "[variables('databaseConnectionString')]" + } + ] }, "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]" } From 5beaf785e800546037d919d36242e23b8fa2ed75 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Mon, 28 Jan 2019 07:52:49 -0500 Subject: [PATCH 4/6] Add Application Insights to Azure deployment --- azuredeploy.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/azuredeploy.json b/azuredeploy.json index 7e89be35..c3b83fcc 100644 --- a/azuredeploy.json +++ b/azuredeploy.json @@ -102,8 +102,20 @@ "databaseServerName": "[concat(parameters('appName'),'-state')]", "setupScriptName": "[concat(parameters('appName'),'-setup')]", "appServicePlanName": "[concat(parameters('appName'),'-hosting')]", + "analyticsName": "[concat(parameters('appName'),'-analytics')]" }, "resources": [ + { + "type": "Microsoft.Insights/components", + "apiVersion": "2015-05-01", + "name": "[variables('analyticsName')]", + "location": "[variables('location')]", + "tags": {}, + "kind": "web", + "properties": { + "Application_Type": "web" + } + }, { "apiVersion": "2017-08-01", "type": "Microsoft.Web/serverfarms", @@ -219,6 +231,7 @@ "location": "[variables('location')]", "dependsOn": [ "[resourceId('Microsoft.DBforPostgreSQL/servers/', variables('databaseServerName'))]", + "[resourceId('Microsoft.Insights/components', variables('analyticsName'))]", "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]" ], "properties": { @@ -231,6 +244,14 @@ "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE", "value": "false" }, + { + "name": "AZURE_APPINSIGHTS_IKEY", + "value": "[reference(resourceId('Microsoft.Insights/components', variables('analyticsName')), '2014-04-01').InstrumentationKey]" + }, + { + "name": "GOOGLE_TRACKING_ID", + "value": "" + }, { "name": "DEBUG", "value": "False" From e4fd74ec6f5eb6c0e5bcbbe1dd6785fd6d6defae Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Mon, 28 Jan 2019 08:08:54 -0500 Subject: [PATCH 5/6] Add server-side analytics via AppInsights --- app/app/settings.py | 6 +++++- app/server/templatetags/analytics.py | 2 +- requirements.txt | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/app/settings.py b/app/app/settings.py index 7a358791..740405ee 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -62,6 +62,7 @@ MIDDLEWARE = [ 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'social_django.middleware.SocialAuthExceptionMiddleware', + 'applicationinsights.django.ApplicationInsightsMiddleware', ] ROOT_URLCONF = 'app.urls' @@ -185,6 +186,9 @@ SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') IMPORT_BATCH_SIZE = 500 GOOGLE_TRACKING_ID = os.getenv('GOOGLE_TRACKING_ID', 'UA-125643874-2') -AZURE_APPINSIGHTS_IKEY = os.getenv('AZURE_APPINSIGHTS_IKEY') + +APPLICATION_INSIGHTS = { + 'ikey': os.getenv('AZURE_APPINSIGHTS_IKEY'), +} django_heroku.settings(locals(), test_runner=False) diff --git a/app/server/templatetags/analytics.py b/app/server/templatetags/analytics.py index a623cb71..c1fa1a50 100644 --- a/app/server/templatetags/analytics.py +++ b/app/server/templatetags/analytics.py @@ -14,5 +14,5 @@ def google_analytics(): def azure_appinsights(): return { 'DEBUG': settings.DEBUG, - 'azure_appinsights_ikey': settings.AZURE_APPINSIGHTS_IKEY, + 'azure_appinsights_ikey': settings.APPLICATION_INSIGHTS['ikey'], } diff --git a/requirements.txt b/requirements.txt index e931cc8f..4d120b3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +applicationinsights==0.11.7 dj-database-url==0.5.0 Django==2.1.5 django-filter==2.0.0 From 11cf8e2d394c22783740c5cb06b665eb1bb6282f Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Mon, 28 Jan 2019 08:32:44 -0500 Subject: [PATCH 6/6] Fix spacing in JSON file --- azuredeploy.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/azuredeploy.json b/azuredeploy.json index c3b83fcc..b533c329 100644 --- a/azuredeploy.json +++ b/azuredeploy.json @@ -2,35 +2,35 @@ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { - "appName":{ + "appName": { "type": "string", - "minLength": 1, + "minLength": 1, "metadata": { "description": "The name for the webapp. Must be globally unique." } }, - "secretKey":{ + "secretKey": { "type": "securestring", - "minLength": 16, + "minLength": 16, "metadata": { "description": "The value to use as the Django secret key." } }, - "adminUserName":{ + "adminUserName": { "type": "string", - "minLength": 1, + "minLength": 1, "metadata": { "description": "The user name for the admin account." } }, - "adminContactEmail":{ + "adminContactEmail": { "type": "string", - "minLength": 1, + "minLength": 1, "metadata": { "description": "The contact email address for the admin account." } }, - "adminPassword":{ + "adminPassword": { "type": "securestring", "minLength": 16, "metadata": {