diff --git a/Dockerfile b/Dockerfile index 313abd72..9c7fc973 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,6 +13,8 @@ ENV DEBUG="True" 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 d1252a32..a473c016 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -64,6 +64,7 @@ MIDDLEWARE = [ 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'social_django.middleware.SocialAuthExceptionMiddleware', + 'applicationinsights.django.ApplicationInsightsMiddleware', ] ROOT_URLCONF = 'app.urls' @@ -82,6 +83,9 @@ TEMPLATES = [ 'social_django.context_processors.backends', 'social_django.context_processors.login_redirect', ], + 'libraries': { + 'analytics': 'server.templatetags.analytics', + }, }, }, ] @@ -185,4 +189,10 @@ 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') + +APPLICATION_INSIGHTS = { + '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 1639f8e0..c8a4c25c 100644 --- a/app/server/templates/base.html +++ b/app/server/templates/base.html @@ -1,15 +1,9 @@ {% load static %} +{% load analytics %}
- - - + {% google_analytics %} @@ -41,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/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..c1fa1a50 --- /dev/null +++ b/app/server/templatetags/analytics.py @@ -0,0 +1,18 @@ +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} + + +@register.inclusion_tag('tags/azure_appinsights.html') +def azure_appinsights(): + return { + 'DEBUG': settings.DEBUG, + 'azure_appinsights_ikey': settings.APPLICATION_INSIGHTS['ikey'], + } diff --git a/azuredeploy.json b/azuredeploy.json index 2174b146..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": { @@ -102,26 +102,20 @@ "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')]" - } - ] + "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", @@ -199,7 +193,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", @@ -224,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,7 +239,32 @@ "siteConfig": { "linuxFxVersion": "[concat('DOCKER|', parameters('dockerImageName'))]", "alwaysOn": true, - "appSettings": "[variables('env')]" + "appSettings": [ + { + "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" + }, + { + "name": "SECRET_KEY", + "value": "[parameters('secretKey')]" + }, + { + "name": "DATABASE_URL", + "value": "[variables('databaseConnectionString')]" + } + ] }, "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('appServicePlanName'))]" } diff --git a/requirements.txt b/requirements.txt index f532c1c2..34b93d0a 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