Skip to content

Commit

Permalink
Respect dnt in JS tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
haaavk committed Feb 15, 2023
1 parent f6bbb71 commit fd2185e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 8 additions & 0 deletions shynet/analytics/templates/analytics/scripts/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions shynet/analytics/views/ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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
Expand Down Expand Up @@ -119,7 +119,7 @@ def get(self, *args, **kwargs):
"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={
Expand All @@ -129,6 +129,9 @@ def get(self, *args, **kwargs):
)
)
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",
Expand All @@ -138,6 +141,7 @@ def get(self, *args, **kwargs):
"protocol": protocol,
"heartbeat_frequency": heartbeat_frequency,
"script_inject": self.get_script_inject(),
"dnt": dnt and service.respect_dnt,
}
),
content_type="application/javascript",
Expand All @@ -159,7 +163,7 @@ def post(self, *args, **kwargs):
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)
Expand Down

0 comments on commit fd2185e

Please sign in to comment.