Browse Source

Add client-side analytics via Azure AppInsights

See https://docs.microsoft.com/en-us/azure/azure-monitor/app/javascript
pull/76/head
Clemens Wolff 5 years ago
parent
commit
7f6cce2f6e
5 changed files with 25 additions and 0 deletions
  1. 1
      Dockerfile
  2. 1
      app/app/settings.py
  3. 1
      app/server/templates/base.html
  4. 14
      app/server/templates/tags/azure_appinsights.html
  5. 8
      app/server/templatetags/analytics.py

1
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

1
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)

1
app/server/templates/base.html

@ -35,6 +35,7 @@
<meta name="msapplication-TileImage" content="{% static 'images/favicons/ms-icon-144x144.png' %}">
<meta name="theme-color" content="#ffffff">
{% block header %}{% endblock %}
{% azure_appinsights %}
</head>
<body>

14
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 %}
<script type="text/javascript">
var appInsights=window.appInsights||function(a){
function b(a){c[a]=function(){var b=arguments;c.queue.push(function(){c[a].apply(c,b)})}}var c={config:a},d=document,e=window;setTimeout(function(){var b=d.createElement("script");b.src=a.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js",d.getElementsByTagName("script")[0].parentNode.appendChild(b)});try{c.cookie=d.cookie}catch(a){}c.queue=[];for(var f=["Event","Exception","Metric","PageView","Trace","Dependency"];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext"),b("clearAuthenticatedUserContext"),b("startTrackEvent"),b("stopTrackEvent"),b("startTrackPage"),b("stopTrackPage"),b("flush"),!a.disableExceptionTracking){f="onerror",b("_"+f);var g=e[f];e[f]=function(a,b,d,e,h){var i=g&&g(a,b,d,e,h);return!0!==i&&c["_"+f](a,b,d,e,h),i}}return c
}({
{% if DEBUG %}
enableDebug: true,
{% endif %}
instrumentationKey: "{{ azure_appinsights_ikey }}"
});
window.appInsights=appInsights,appInsights.queue&&0===appInsights.queue.length&&appInsights.trackPageView();
</script>
{% endif %}

8
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,
}
Loading…
Cancel
Save