From 3d5f4983c5cf93eaaa33b7bfb48a3509822bb2ff Mon Sep 17 00:00:00 2001 From: Muhammad Adeel Tajamul <77053848+muhammadadeeltajamul@users.noreply.github.com> Date: Mon, 2 Dec 2024 13:22:27 +0500 Subject: [PATCH] feat: added anonymous id in edx.bi.user.account.authenticated event (#35934) --- common/djangoapps/third_party_auth/pipeline.py | 13 ++++++++++++- openedx/core/djangoapps/user_authn/views/login.py | 12 +++++++++++- .../djangoapps/user_authn/views/tests/test_login.py | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/common/djangoapps/third_party_auth/pipeline.py b/common/djangoapps/third_party_auth/pipeline.py index 8e688208af..ef1e6f887c 100644 --- a/common/djangoapps/third_party_auth/pipeline.py +++ b/common/djangoapps/third_party_auth/pipeline.py @@ -715,8 +715,18 @@ def login_analytics(strategy, auth_entry, current_partial=None, *args, **kwargs) """ Sends login info to Segment """ event_name = None + anonymous_id = "" + additional_params = {} + + try: + request = kwargs['request'] + anonymous_id = request.COOKIES.get('ajs_anonymous_id', "") + except: # pylint: disable=bare-except + pass + if auth_entry == AUTH_ENTRY_LOGIN: event_name = 'edx.bi.user.account.authenticated' + additional_params['anonymous_id'] = anonymous_id elif auth_entry in [AUTH_ENTRY_ACCOUNT_SETTINGS]: event_name = 'edx.bi.user.account.linked' @@ -724,7 +734,8 @@ def login_analytics(strategy, auth_entry, current_partial=None, *args, **kwargs) segment.track(kwargs['user'].id, event_name, { 'category': "conversion", 'label': None, - 'provider': kwargs['backend'].name + 'provider': kwargs['backend'].name, + **additional_params }) diff --git a/openedx/core/djangoapps/user_authn/views/login.py b/openedx/core/djangoapps/user_authn/views/login.py index 6c7390406b..042ef90c9a 100644 --- a/openedx/core/djangoapps/user_authn/views/login.py +++ b/openedx/core/djangoapps/user_authn/views/login.py @@ -354,6 +354,11 @@ def _track_user_login(user, request): # .. pii: Username and email are sent to Segment here. Retired directly through Segment API call in Tubular. # .. pii_types: email_address, username # .. pii_retirement: third_party + anonymous_id = "" + try: + anonymous_id = request.COOKIES.get('ajs_anonymous_id', "") + except: # pylint: disable=bare-except + pass segment.identify( user.id, {"email": user.email, "username": user.username}, @@ -367,7 +372,12 @@ def _track_user_login(user, request): segment.track( user.id, "edx.bi.user.account.authenticated", - {"category": "conversion", "label": request.POST.get("course_id"), "provider": None}, + { + "category": "conversion", + "label": request.POST.get("course_id"), + "provider": None, + "anonymous_id": anonymous_id, + }, ) diff --git a/openedx/core/djangoapps/user_authn/views/tests/test_login.py b/openedx/core/djangoapps/user_authn/views/tests/test_login.py index 1e8a4c3ed5..aa34a076d4 100644 --- a/openedx/core/djangoapps/user_authn/views/tests/test_login.py +++ b/openedx/core/djangoapps/user_authn/views/tests/test_login.py @@ -1145,7 +1145,7 @@ class LoginSessionViewTest(ApiTestCase, OpenEdxEventsTestMixin): mock_segment.track.assert_called_once_with( expected_user_id, 'edx.bi.user.account.authenticated', - {'category': 'conversion', 'provider': None, 'label': track_label} + {'category': 'conversion', 'provider': None, 'label': track_label, 'anonymous_id': ''} ) def test_login_with_username(self):