From bde3e6f4061890dcd08a9fcc8d98a221ac42bbf3 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Sun, 12 Mar 2023 23:37:55 +0530 Subject: [PATCH 1/2] feat: jitsu events for sign in and sign up --- apiserver/plane/api/views/authentication.py | 91 ++++++++++++++++++++- apiserver/plane/api/views/oauth.py | 42 +++++++++- apiserver/plane/settings/local.py | 3 + apiserver/plane/settings/production.py | 3 + apiserver/plane/settings/staging.py | 3 + 5 files changed, 140 insertions(+), 2 deletions(-) diff --git a/apiserver/plane/api/views/authentication.py b/apiserver/plane/api/views/authentication.py index 58d75a04952..3e8f269d4ed 100644 --- a/apiserver/plane/api/views/authentication.py +++ b/apiserver/plane/api/views/authentication.py @@ -3,6 +3,7 @@ import random import string import json +import requests # Django imports from django.utils import timezone @@ -85,6 +86,28 @@ def post(self, request): "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 @@ def post(self, request): 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": f"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 @@ def post(self, request): 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": f"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 @@ def post(self, request): 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": f"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 994cb0466ab..1c35b598913 100644 --- a/apiserver/plane/api/views/oauth.py +++ b/apiserver/plane/api/views/oauth.py @@ -5,6 +5,7 @@ # 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 @@ def post(self, request): "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 @@ def post(self, request): "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 ccb3880125b..9ec5aee85fb 100644 --- a/apiserver/plane/settings/local.py +++ b/apiserver/plane/settings/local.py @@ -78,3 +78,6 @@ 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 c1cde24a016..93d5eca141c 100644 --- a/apiserver/plane/settings/production.py +++ b/apiserver/plane/settings/production.py @@ -226,3 +226,6 @@ 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 0e58ab224de..e5d26f652c3 100644 --- a/apiserver/plane/settings/staging.py +++ b/apiserver/plane/settings/staging.py @@ -187,3 +187,6 @@ 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) From cb22750ae926a4c8d50f7b0ce94c844c485d0ebb Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Sun, 12 Mar 2023 23:46:18 +0530 Subject: [PATCH 2/2] dev: update event data --- apiserver/plane/api/views/authentication.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apiserver/plane/api/views/authentication.py b/apiserver/plane/api/views/authentication.py index 3e8f269d4ed..d6a9cdac359 100644 --- a/apiserver/plane/api/views/authentication.py +++ b/apiserver/plane/api/views/authentication.py @@ -148,7 +148,7 @@ def post(self, request): json={ "event_id": uuid.uuid4().hex, "event_data": { - "medium": f"email", + "medium": "email", }, "user": {"email": email}, "device_ctx": { @@ -322,7 +322,7 @@ def post(self, request): json={ "event_id": uuid.uuid4().hex, "event_data": { - "medium": f"code", + "medium": "code", }, "user": {"email": email}, "device_ctx": { @@ -352,7 +352,7 @@ def post(self, request): json={ "event_id": uuid.uuid4().hex, "event_data": { - "medium": f"code", + "medium": "code", }, "user": {"email": email}, "device_ctx": {