forked from github/plane
fix: event tracking
This commit is contained in:
parent
51e17643a2
commit
01e72c1dfb
8
apiserver/plane/analytics/urls.py
Normal file
8
apiserver/plane/analytics/urls.py
Normal file
@ -0,0 +1,8 @@
|
||||
# from django.contrib import admin
|
||||
from django.urls import path
|
||||
|
||||
from plane.analytics.views import EventTrackingEndpoint
|
||||
|
||||
urlpatterns = [
|
||||
path('track-event/', EventTrackingEndpoint.as_view(), name='event_tracking'),
|
||||
]
|
1
apiserver/plane/analytics/views/__init__.py
Normal file
1
apiserver/plane/analytics/views/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .event_tacking import EventTrackingEndpoint
|
52
apiserver/plane/analytics/views/event_tacking.py
Normal file
52
apiserver/plane/analytics/views/event_tacking.py
Normal file
@ -0,0 +1,52 @@
|
||||
# Python imports
|
||||
import requests
|
||||
import json
|
||||
|
||||
# Module imports
|
||||
from plane.app.views import BaseAPIView
|
||||
|
||||
# Third party imports
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
# Django imports
|
||||
from django.conf import settings
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
|
||||
|
||||
class EventTrackingEndpoint(BaseAPIView):
|
||||
permission_class = [
|
||||
IsAuthenticated,
|
||||
]
|
||||
|
||||
def post(self, request):
|
||||
event_name = request.data.get("event_name", None)
|
||||
extra = request.data.get("extra", None)
|
||||
|
||||
if not event_name or not extra:
|
||||
return Response(
|
||||
{"error": "event name and extra are required"},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
_ = requests.post(
|
||||
settings.ANALYTICS_BASE_API,
|
||||
params={"token": settings.ANALYTICS_SECRET_KEY},
|
||||
json=json.dumps(
|
||||
{
|
||||
"eventName": event_name,
|
||||
"extra": extra,
|
||||
"user": {
|
||||
"id": str(request.user.id),
|
||||
"email": str(request.user.email),
|
||||
"first_name": str(request.user.first_name),
|
||||
"last_name": str(request.user.last_name),
|
||||
"last_login_ip": str(request.META.get("REMOTE_ADDR")),
|
||||
"last_login_uagent": str(request.META.get("HTTP_USER_AGENT"))
|
||||
},
|
||||
},
|
||||
cls=DjangoJSONEncoder,
|
||||
),
|
||||
)
|
||||
return Response({"message": "success"}, status=status.HTTP_200_OK)
|
@ -22,10 +22,6 @@ from .api import urlpatterns as api_urls
|
||||
from .webhook import urlpatterns as webhook_urls
|
||||
|
||||
|
||||
# Django imports
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
*analytic_urls,
|
||||
*asset_urls,
|
||||
|
@ -25,7 +25,7 @@ from django_filters.rest_framework import DjangoFilterBackend
|
||||
# Module imports
|
||||
from plane.utils.paginator import BasePaginator
|
||||
from plane.bgtasks.webhook_task import send_webhook
|
||||
|
||||
from plane.bgtasks.track_events import track_event
|
||||
|
||||
class TimezoneMixin:
|
||||
"""
|
||||
@ -44,8 +44,8 @@ class TimezoneMixin:
|
||||
class WebhookMixin:
|
||||
webhook_event = None
|
||||
|
||||
def finalize_response(self, request, response, *args, **kwargs):
|
||||
response = super().finalize_response(request, response, *args, **kwargs)
|
||||
def initial_request(self, request, response, *args, **kwargs):
|
||||
response = super().initial_request(request, response, *args, **kwargs)
|
||||
|
||||
if (
|
||||
self.webhook_event
|
||||
@ -62,6 +62,30 @@ class WebhookMixin:
|
||||
return response
|
||||
|
||||
|
||||
class AnalyticsMixin:
|
||||
analytic_event = None
|
||||
print("did it come here or not")
|
||||
|
||||
def initial(self, request, *args, **kwargs):
|
||||
super().initial(request, *args, **kwargs)
|
||||
print("hieieie")
|
||||
print(self.workspace_slug,"workspace_slug")
|
||||
# if (
|
||||
# self.analytic_event
|
||||
# and self.request.method in ["POST", "PATCH", "DELETE"]
|
||||
# and response.status_code in [200, 201, 204]
|
||||
# ):
|
||||
# print(self.workspace_slug,"workspace_slug")
|
||||
# track_event.delay(
|
||||
# event_name=self.analytic_event,
|
||||
# slug=self.workspace_slug,
|
||||
# project_id=self.project_id,
|
||||
# user_id=self.request.user.id,
|
||||
# )
|
||||
|
||||
# return response
|
||||
|
||||
|
||||
class BaseViewSet(TimezoneMixin, ModelViewSet, BasePaginator):
|
||||
model = None
|
||||
|
||||
|
@ -26,7 +26,7 @@ from rest_framework import serializers
|
||||
from rest_framework.permissions import AllowAny
|
||||
|
||||
# Module imports
|
||||
from .base import BaseViewSet, BaseAPIView, WebhookMixin
|
||||
from .base import BaseViewSet, BaseAPIView, WebhookMixin, AnalyticsMixin
|
||||
from plane.app.serializers import (
|
||||
ProjectSerializer,
|
||||
ProjectListSerializer,
|
||||
@ -64,10 +64,11 @@ from plane.db.models import (
|
||||
from plane.bgtasks.project_invitation_task import project_invitation
|
||||
|
||||
|
||||
class ProjectViewSet(WebhookMixin, BaseViewSet):
|
||||
class ProjectViewSet(WebhookMixin, AnalyticsMixin, BaseViewSet):
|
||||
serializer_class = ProjectSerializer
|
||||
model = Project
|
||||
webhook_event = "project"
|
||||
analytic_event = "project"
|
||||
|
||||
permission_classes = [
|
||||
ProjectBasePermission,
|
||||
|
55
apiserver/plane/bgtasks/track_events.py
Normal file
55
apiserver/plane/bgtasks/track_events.py
Normal file
@ -0,0 +1,55 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
# Django imports
|
||||
from django.conf import settings
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
|
||||
# Module imports
|
||||
from plane.db.models import Workspace, Project, User
|
||||
|
||||
# Third party imports
|
||||
from celery import shared_task
|
||||
from sentry_sdk import capture_exception
|
||||
|
||||
|
||||
@shared_task
|
||||
def track_event(event_name, slug, project_id, user_id):
|
||||
try:
|
||||
workspace = Workspace.objects.get(slug=slug)
|
||||
project = Project.objects.get(slug=slug, id=project_id)
|
||||
user = User.objects.get(id=user_id)
|
||||
|
||||
extra = {
|
||||
"projectId": str(project.id),
|
||||
"projectIdentifier": str(project.identifier),
|
||||
"projectName": str(project.name),
|
||||
"workspaceId": str(workspace.id),
|
||||
"workspaceName": str(workspace.name),
|
||||
"workspaceSlug": str(workspace.slug),
|
||||
}
|
||||
|
||||
_ = requests.post(
|
||||
settings.ANALYTICS_BASE_API,
|
||||
params={"token": settings.ANALYTICS_SECRET_KEY},
|
||||
json=json.dumps(
|
||||
{
|
||||
"eventName": event_name,
|
||||
"extra": extra,
|
||||
"user": {
|
||||
"id": str(user.id),
|
||||
"email": str(user.email),
|
||||
"first_name": str(user.first_name),
|
||||
"last_name": str(user.last_name),
|
||||
"last_login_ip": str(user.last_login_ip),
|
||||
"last_login_uagent": str(user.last_login_uagent),
|
||||
},
|
||||
},
|
||||
cls=DjangoJSONEncoder,
|
||||
),
|
||||
)
|
||||
except Exception as e:
|
||||
if settings.DEBUG:
|
||||
print(e)
|
||||
capture_exception(e)
|
||||
return
|
@ -11,6 +11,7 @@ from django.conf import settings
|
||||
urlpatterns = [
|
||||
path("", TemplateView.as_view(template_name="index.html")),
|
||||
path("api/", include("plane.app.urls")),
|
||||
path("api/analytics/", include("plane.analytics.urls")),
|
||||
path("api/public/", include("plane.space.urls")),
|
||||
path("api/licenses/", include("plane.license.urls")),
|
||||
path("api/v1/", include("plane.proxy.urls")),
|
||||
|
Loading…
Reference in New Issue
Block a user