feat: jitsu events for sign in and sign up (#423)

* feat: jitsu events for sign in and sign up

* dev: update event data
This commit is contained in:
pablohashescobar 2023-03-15 23:25:38 +05:30 committed by GitHub
parent 88754e6fc0
commit 836dc4027b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 140 additions and 2 deletions

View File

@ -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()

View File

@ -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,

View File

@ -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)

View File

@ -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)

View File

@ -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)