diff --git a/apiserver/plane/api/views/authentication.py b/apiserver/plane/api/views/authentication.py index 58d75a049..d6a9cdac3 100644 --- a/apiserver/plane/api/views/authentication.py +++ b/apiserver/plane/api/views/authentication.py @@ -3,6 +3,7 @@ import uuid import random import string import json +import requests # Django imports from django.utils import timezone @@ -85,6 +86,28 @@ class SignInEndpoint(BaseAPIView): "user": serialized_user, } + # Send event to Jitsu for tracking + if settings.JITSU_BASE_API: + _ = requests.post( + settings.JITSU_BASE_API, + headers={ + "Content-Type": "application/json", + "X-Auth-Token": settings.JITSU_SECRET_KEY, + }, + json={ + "event_id": uuid.uuid4().hex, + "event_data": { + "medium": "email", + }, + "user": {"email": email}, + "device_ctx": { + "ip": request.META.get("REMOTE_ADDR"), + "user_agent": request.META.get("HTTP_USER_AGENT"), + }, + "event_type": "SIGN_UP", + }, + ) + return Response(data, status=status.HTTP_200_OK) # Sign in Process else: @@ -114,7 +137,27 @@ class SignInEndpoint(BaseAPIView): user.save() access_token, refresh_token = get_tokens_for_user(user) - + # Send event to Jitsu for tracking + if settings.JITSU_BASE_API: + _ = requests.post( + settings.JITSU_BASE_API, + headers={ + "Content-Type": "application/json", + "X-Auth-Token": settings.JITSU_SECRET_KEY, + }, + json={ + "event_id": uuid.uuid4().hex, + "event_data": { + "medium": "email", + }, + "user": {"email": email}, + "device_ctx": { + "ip": request.META.get("REMOTE_ADDR"), + "user_agent": request.META.get("HTTP_USER_AGENT"), + }, + "event_type": "SIGN_IN", + }, + ) data = { "access_token": access_token, "refresh_token": refresh_token, @@ -268,6 +311,29 @@ class MagicSignInEndpoint(BaseAPIView): if str(token) == str(user_token): if User.objects.filter(email=email).exists(): user = User.objects.get(email=email) + # Send event to Jitsu for tracking + if settings.JITSU_BASE_API: + _ = requests.post( + settings.JITSU_BASE_API, + headers={ + "Content-Type": "application/json", + "X-Auth-Token": settings.JITSU_SECRET_KEY, + }, + json={ + "event_id": uuid.uuid4().hex, + "event_data": { + "medium": "code", + }, + "user": {"email": email}, + "device_ctx": { + "ip": request.META.get("REMOTE_ADDR"), + "user_agent": request.META.get( + "HTTP_USER_AGENT" + ), + }, + "event_type": "SIGN_IN", + }, + ) else: user = User.objects.create( email=email, @@ -275,6 +341,29 @@ class MagicSignInEndpoint(BaseAPIView): password=make_password(uuid.uuid4().hex), is_password_autoset=True, ) + # Send event to Jitsu for tracking + if settings.JITSU_BASE_API: + _ = requests.post( + settings.JITSU_BASE_API, + headers={ + "Content-Type": "application/json", + "X-Auth-Token": settings.JITSU_SECRET_KEY, + }, + json={ + "event_id": uuid.uuid4().hex, + "event_data": { + "medium": "code", + }, + "user": {"email": email}, + "device_ctx": { + "ip": request.META.get("REMOTE_ADDR"), + "user_agent": request.META.get( + "HTTP_USER_AGENT" + ), + }, + "event_type": "SIGN_UP", + }, + ) user.last_active = timezone.now() user.last_login_time = timezone.now() diff --git a/apiserver/plane/api/views/oauth.py b/apiserver/plane/api/views/oauth.py index 994cb0466..1c35b5989 100644 --- a/apiserver/plane/api/views/oauth.py +++ b/apiserver/plane/api/views/oauth.py @@ -5,6 +5,7 @@ import os # Django imports from django.utils import timezone +from django.conf import settings # Third Party modules from rest_framework.response import Response @@ -204,7 +205,26 @@ class OauthEndpoint(BaseAPIView): "last_login_at": timezone.now(), }, ) - + if settings.JITSU_BASE_API: + _ = requests.post( + settings.JITSU_BASE_API, + headers={ + "Content-Type": "application/json", + "X-Auth-Token": settings.JITSU_SECRET_KEY, + }, + json={ + "event_id": uuid.uuid4().hex, + "event_data": { + "medium": f"oauth-{medium}", + }, + "user": {"email": email}, + "device_ctx": { + "ip": request.META.get("REMOTE_ADDR"), + "user_agent": request.META.get("HTTP_USER_AGENT"), + }, + "event_type": "SIGN_IN", + }, + ) return Response(data, status=status.HTTP_200_OK) except User.DoesNotExist: @@ -253,6 +273,26 @@ class OauthEndpoint(BaseAPIView): "user": serialized_user, "permissions": [], } + if settings.JITSU_BASE_API: + _ = requests.post( + settings.JITSU_BASE_API, + headers={ + "Content-Type": "application/json", + "X-Auth-Token": settings.JITSU_SECRET_KEY, + }, + json={ + "event_id": uuid.uuid4().hex, + "event_data": { + "medium": f"oauth-{medium}", + }, + "user": {"email": email}, + "device_ctx": { + "ip": request.META.get("REMOTE_ADDR"), + "user_agent": request.META.get("HTTP_USER_AGENT"), + }, + "event_type": "SIGN_UP", + }, + ) SocialLoginConnection.objects.update_or_create( medium=medium, diff --git a/apiserver/plane/settings/local.py b/apiserver/plane/settings/local.py index ccb388012..9ec5aee85 100644 --- a/apiserver/plane/settings/local.py +++ b/apiserver/plane/settings/local.py @@ -78,3 +78,6 @@ if DOCKERIZED: WEB_URL = os.environ.get("WEB_URL", "localhost:3000") PROXY_BASE_URL = os.environ.get("PROXY_BASE_URL", False) + +JITSU_SECRET_KEY = os.environ.get("JITSU_SECRET_KEY", False) +JITSU_BASE_API = os.environ.get("JITSU_BASE_API", False) diff --git a/apiserver/plane/settings/production.py b/apiserver/plane/settings/production.py index c1cde24a0..93d5eca14 100644 --- a/apiserver/plane/settings/production.py +++ b/apiserver/plane/settings/production.py @@ -226,3 +226,6 @@ RQ_QUEUES = { WEB_URL = os.environ.get("WEB_URL") PROXY_BASE_URL = os.environ.get("PROXY_BASE_URL", False) + +JITSU_SECRET_KEY = os.environ.get("JITSU_SECRET_KEY", False) +JITSU_BASE_API = os.environ.get("JITSU_BASE_API", False) diff --git a/apiserver/plane/settings/staging.py b/apiserver/plane/settings/staging.py index 0e58ab224..e5d26f652 100644 --- a/apiserver/plane/settings/staging.py +++ b/apiserver/plane/settings/staging.py @@ -187,3 +187,6 @@ RQ_QUEUES = { WEB_URL = os.environ.get("WEB_URL") PROXY_BASE_URL = os.environ.get("PROXY_BASE_URL", False) + +JITSU_SECRET_KEY = os.environ.get("JITSU_SECRET_KEY", False) +JITSU_BASE_API = os.environ.get("JITSU_BASE_API", False)