From 4ffc3bdef73182ca9ca58f458538d694332d7c68 Mon Sep 17 00:00:00 2001 From: havk Date: Thu, 23 Feb 2023 22:39:26 +0100 Subject: [PATCH] Respect dnt in JS tracker (#257) --- shynet/analytics/templates/analytics/scripts/page.js | 8 ++++++++ shynet/analytics/views/ingress.py | 10 +++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/shynet/analytics/templates/analytics/scripts/page.js b/shynet/analytics/templates/analytics/scripts/page.js index 35aa7dd..9574cdc 100644 --- a/shynet/analytics/templates/analytics/scripts/page.js +++ b/shynet/analytics/templates/analytics/scripts/page.js @@ -5,7 +5,13 @@ // // This script only sends the current URL, the referrer URL, and the page load time. That's it! +{% if dnt %} var Shynet = { + dnt: true +}; +{% else %} +var Shynet = { + dnt: false, idempotency: null, heartbeatTaskId: null, skipHeartbeat: false, @@ -53,6 +59,8 @@ var Shynet = { }; window.addEventListener("load", Shynet.newPageLoad); +{% endif %} + {% if script_inject %} // The following is script is not part of Shynet, and was instead diff --git a/shynet/analytics/views/ingress.py b/shynet/analytics/views/ingress.py index b6b7609..49dcfda 100644 --- a/shynet/analytics/views/ingress.py +++ b/shynet/analytics/views/ingress.py @@ -15,7 +15,7 @@ from django.shortcuts import render, reverse from django.utils import timezone from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt -from django.views.generic import TemplateView, View +from django.views.generic import View from ipware import get_client_ip from core.models import Service @@ -119,7 +119,7 @@ class ScriptView(ValidateServiceOriginsMixin, View): "service_uuid": self.kwargs.get("service_uuid"), }, ) - if self.kwargs.get("identifier") == None + if self.kwargs.get("identifier") is None else reverse( "ingress:endpoint_script_id", kwargs={ @@ -129,6 +129,9 @@ class ScriptView(ValidateServiceOriginsMixin, View): ) ) heartbeat_frequency = settings.SCRIPT_HEARTBEAT_FREQUENCY + dnt = self.request.META.get("HTTP_DNT", "0").strip() == "1" + service_uuid = self.kwargs.get("service_uuid") + service = Service.objects.get(pk=service_uuid, status=Service.ACTIVE) return render( self.request, "analytics/scripts/page.js", @@ -138,6 +141,7 @@ class ScriptView(ValidateServiceOriginsMixin, View): "protocol": protocol, "heartbeat_frequency": heartbeat_frequency, "script_inject": self.get_script_inject(), + "dnt": dnt and service.respect_dnt, } ), content_type="application/javascript", @@ -159,7 +163,7 @@ class ScriptView(ValidateServiceOriginsMixin, View): def get_script_inject(self): service_uuid = self.kwargs.get("service_uuid") script_inject = cache.get(f"script_inject_{service_uuid}") - if script_inject == None: + if script_inject is None: service = Service.objects.get(uuid=service_uuid) script_inject = service.script_inject cache.set(f"script_inject_{service_uuid}", script_inject, timeout=3600)