diff --git a/django_umami/core.py b/django_umami/core.py index efa9d99..c727b50 100644 --- a/django_umami/core.py +++ b/django_umami/core.py @@ -53,7 +53,6 @@ class UmamiPayload: def dict(self): data = {"website": self.website} if self.data: - print("has data") data |= self.data return data @@ -87,6 +86,10 @@ def send(self, payload: UmamiPayload): return self.options.session.post(url=f"{self.options.host_url}/api/send", json=data, headers=headers) return requests.post(url=f"{self.options.host_url}/api/send", json=data, headers=headers) + def track_event_name(self, event_name: str): + payload = UmamiPayload(website=self.options.website_id, data={"name": event_name}) + return self.send(payload=payload) + def track(self, event: UmamiEventData | str, event_data=None): website_id = self.options.website_id diff --git a/django_umami/decorators.py b/django_umami/decorators.py index a369ab9..e8ffa96 100644 --- a/django_umami/decorators.py +++ b/django_umami/decorators.py @@ -1,15 +1,42 @@ from functools import wraps +from typing import Optional + +from django.http import HttpRequest from django_umami.core import umami, UmamiEventData -def track(event_name: str, event_data: UmamiEventData = None): +def track(event: UmamiEventData | str, event_data: Optional[UmamiEventData] = None): def decorator(func): @wraps(func) def wrapper(*args, **kwargs): - umami.track(event_name, event_data) + umami.track(event, event_data) return func(*args, **kwargs) return wrapper return decorator + + +def track_visit(event_data: Optional[UmamiEventData] = None): + def decorator(func): + @wraps(func) + def wrapper(request: HttpRequest, *args, **kwargs): + new_event_data: UmamiEventData = event_data or UmamiEventData() + + data: UmamiEventData = UmamiEventData( + url=request.path, + referrer=request.META.get("HTTP_REFERER", ""), + title=request.META.get("HTTP_TITLE", ""), + ) + + new_event_data.update( + {k: v for k, v in data.items() if k not in new_event_data} # type: ignore[typeddict-item] + ) + + umami.track(new_event_data) + return func(request, *args, **kwargs) + + return wrapper + + return decorator