Respect dnt in JS tracker (#257)

This commit is contained in:
havk 2023-02-23 22:39:26 +01:00 committed by GitHub
parent d78fd9f6c5
commit 4ffc3bdef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -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

View File

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