Skip to content

Commit

Permalink
Merge pull request #3334 from webkom/ivarnakken/aba-428-use-data-from…
Browse files Browse the repository at this point in the history
…-plausible
  • Loading branch information
ivarnakken committed Apr 30, 2023
2 parents b4e7679 + 144d86e commit 684e66a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lego/apps/events/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from datetime import datetime
from urllib.parse import quote

from django.conf import settings
from django.db import transaction
from django.db.models import Count, Prefetch, Q
from django.http import Http404
Expand All @@ -9,6 +13,7 @@
from rest_framework.response import Response
from rest_framework.serializers import BaseSerializer

import requests
from celery.canvas import chain

from lego.apps.events import constants
Expand Down Expand Up @@ -250,6 +255,36 @@ def allergies(self, request, *args, **kwargs):
event_data = serializer(event).data
return Response(event_data)

@decorators.action(detail=True, methods=["GET"])
def statistics(self, request, *args, **kwargs):
event_id = self.kwargs.get("pk", None)
event = Event.objects.get(pk=event_id)

headers = {
"Authorization": f"Bearer {settings.PLAUSIBLE_KEY}",
"Content-Type": "application/json",
}

created_at = event.created_at.strftime("%Y-%m-%d")
now = datetime.now().strftime("%Y-%m-%d")
# Plausible wants the date on this schema: YYYY-MM-DD,YYYY-MM-DD
date = f"{created_at},{now}"

url_path = f"/events/{event.id}"

filters = f"event:page=={quote(url_path)}"
api_url = (
"https://ls.webkom.dev/api/v1/stats/timeseries"
"?site_id=abakus.no"
"&metrics=visitors,pageviews,bounce_rate,visit_duration"
"&period=custom&date={date}"
"&filters={filters}"
).format(date=date, filters=filters)

response = requests.get(api_url, headers=headers)

return Response(response.json())

@decorators.action(detail=True, methods=["POST"], serializer_class=BaseSerializer)
def payment(self, request, *args, **kwargs):
event_id = self.kwargs.get("pk", None)
Expand Down
3 changes: 3 additions & 0 deletions lego/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
# Captcha
CAPTCHA_KEY = env("CAPTCHA_KEY")

# Plausible
PLAUSIBLE_KEY = env("PLAUSIBLE_KEY")

# LDAP
LDAP_SERVER = env("LDAP_SERVER")
LDAP_USER = env("LDAP_USER")
Expand Down

0 comments on commit 684e66a

Please sign in to comment.