diff --git a/src/backend/core/authentication/backends.py b/src/backend/core/authentication/backends.py index 898bd12e4..d168d2665 100644 --- a/src/backend/core/authentication/backends.py +++ b/src/backend/core/authentication/backends.py @@ -12,6 +12,7 @@ from lasuite.oidc_login.backends import ( ) from core.models import DuplicateEmailError +from core.utils.analytics import PosthogEventName, posthog_capture logger = logging.getLogger(__name__) @@ -77,3 +78,6 @@ class OIDCAuthenticationBackend(LaSuiteOIDCAuthenticationBackend): create_or_update_contact.delay( email=user.email, attributes={"DOCS_SOURCE": ["SIGNIN"]} ) + + if user: + posthog_capture(PosthogEventName.USER_LOGIN, user) diff --git a/src/backend/core/tests/authentication/test_backends.py b/src/backend/core/tests/authentication/test_backends.py index 0ff0a78aa..0a20319fd 100644 --- a/src/backend/core/tests/authentication/test_backends.py +++ b/src/backend/core/tests/authentication/test_backends.py @@ -17,6 +17,7 @@ from core.authentication.backends import ( create_or_update_contact, ) from core.factories import UserFactory +from core.utils.analytics import PosthogEventName pytestmark = pytest.mark.django_db @@ -502,13 +503,22 @@ def test_authentication_post_get_or_create_user_new_user_to_marketing_email(sett settings.SIGNUP_NEW_USER_TO_MARKETING_EMAIL = True klass = OIDCAuthenticationBackend() - with mock.patch.object( - create_or_update_contact, "delay" - ) as mock_create_or_update_contact: + with ( + mock.patch.object( + create_or_update_contact, "delay" + ) as mock_create_or_update_contact, + mock.patch( + "core.authentication.backends.posthog_capture" + ) as mock_posthog_capture, + ): klass.post_get_or_create_user(user, {}, True) mock_create_or_update_contact.assert_called_once_with( email=user.email, attributes={"DOCS_SOURCE": ["SIGNIN"]} ) + mock_posthog_capture.assert_called_once_with( + PosthogEventName.USER_LOGIN, + user, + ) def test_authentication_post_get_or_create_user_new_user_to_marketing_email_disabled( @@ -523,11 +533,20 @@ def test_authentication_post_get_or_create_user_new_user_to_marketing_email_disa settings.SIGNUP_NEW_USER_TO_MARKETING_EMAIL = False klass = OIDCAuthenticationBackend() - with mock.patch.object( - create_or_update_contact, "delay" - ) as mock_create_or_update_contact: + with ( + mock.patch.object( + create_or_update_contact, "delay" + ) as mock_create_or_update_contact, + mock.patch( + "core.authentication.backends.posthog_capture" + ) as mock_posthog_capture, + ): klass.post_get_or_create_user(user, {}, True) mock_create_or_update_contact.assert_not_called() + mock_posthog_capture.assert_called_once_with( + PosthogEventName.USER_LOGIN, + user, + ) def test_authentication_post_get_or_create_user_existing_user_to_marketing_email( @@ -542,11 +561,20 @@ def test_authentication_post_get_or_create_user_existing_user_to_marketing_email settings.SIGNUP_NEW_USER_TO_MARKETING_EMAIL = True klass = OIDCAuthenticationBackend() - with mock.patch.object( - create_or_update_contact, "delay" - ) as mock_create_or_update_contact: + with ( + mock.patch.object( + create_or_update_contact, "delay" + ) as mock_create_or_update_contact, + mock.patch( + "core.authentication.backends.posthog_capture" + ) as mock_posthog_capture, + ): klass.post_get_or_create_user(user, {}, False) mock_create_or_update_contact.assert_not_called() + mock_posthog_capture.assert_called_once_with( + PosthogEventName.USER_LOGIN, + user, + ) def test_authentication_post_get_or_create_user_existing_user_to_marketing_email_disabled( @@ -561,8 +589,17 @@ def test_authentication_post_get_or_create_user_existing_user_to_marketing_email settings.SIGNUP_NEW_USER_TO_MARKETING_EMAIL = False klass = OIDCAuthenticationBackend() - with mock.patch.object( - create_or_update_contact, "delay" - ) as mock_create_or_update_contact: + with ( + mock.patch.object( + create_or_update_contact, "delay" + ) as mock_create_or_update_contact, + mock.patch( + "core.authentication.backends.posthog_capture" + ) as mock_posthog_capture, + ): klass.post_get_or_create_user(user, {}, False) mock_create_or_update_contact.assert_not_called() + mock_posthog_capture.assert_called_once_with( + PosthogEventName.USER_LOGIN, + user, + ) diff --git a/src/backend/core/utils/analytics.py b/src/backend/core/utils/analytics.py index 46bb675c6..6120fa3d3 100644 --- a/src/backend/core/utils/analytics.py +++ b/src/backend/core/utils/analytics.py @@ -16,6 +16,7 @@ class PosthogEventName(StrEnum): DOC_CREATED = "doc_created" DOC_DELETED = "doc_deleted" + USER_LOGIN = "user_login" def posthog_capture(