diff --git a/lms/djangoapps/discussion/django_comment_client/base/views.py b/lms/djangoapps/discussion/django_comment_client/base/views.py index f0a7f65017..54f8e493b5 100644 --- a/lms/djangoapps/discussion/django_comment_client/base/views.py +++ b/lms/djangoapps/discussion/django_comment_client/base/views.py @@ -112,7 +112,7 @@ def add_truncated_title_to_event_data(event_data, full_title): event_data['title'] = full_title[:TRACKING_MAX_FORUM_TITLE] -def track_thread_created_event(request, course, thread, followed): +def track_thread_created_event(request, course, thread, followed, from_mfe_sidebar=False): """ Send analytics event for a newly created thread. """ @@ -124,6 +124,7 @@ def track_thread_created_event(request, course, thread, followed): 'anonymous': thread.anonymous, 'anonymous_to_peers': thread.anonymous_to_peers, 'options': {'followed': followed}, + 'from_mfe_sidebar': from_mfe_sidebar, # There is a stated desire for an 'origin' property that will state # whether this thread was created via courseware or the forum. # However, the view does not contain that data, and including it will @@ -133,7 +134,7 @@ def track_thread_created_event(request, course, thread, followed): track_created_event(request, event_name, course, thread, event_data) -def track_comment_created_event(request, course, comment, commentable_id, followed): +def track_comment_created_event(request, course, comment, commentable_id, followed, from_mfe_sidebar=False): """ Send analytics event for a newly created response or comment. """ @@ -143,6 +144,7 @@ def track_comment_created_event(request, course, comment, commentable_id, follow 'discussion': {'id': comment.thread_id}, 'commentable_id': commentable_id, 'options': {'followed': followed}, + 'from_mfe_sidebar': from_mfe_sidebar, } parent_id = comment.get('parent_id') if parent_id: @@ -179,13 +181,14 @@ def track_forum_search_event(request, course, search_event_data): tracker.emit(event_name, search_event_data) -def track_thread_viewed_event(request, course, thread): +def track_thread_viewed_event(request, course, thread, from_mfe_sidebar=False): """ Send analytics event for a viewed thread. """ event_name = _EVENT_NAME_TEMPLATE.format(obj_type='thread', action_name='viewed') event_data = {} event_data['commentable_id'] = thread.get('commentable_id', '') + event_data['from_mfe_sidebar'] = from_mfe_sidebar if hasattr(thread, 'username'): event_data['target_username'] = thread.get('username', '') add_truncated_title_to_event_data(event_data, thread.get('title', '')) diff --git a/lms/djangoapps/discussion/rest_api/api.py b/lms/djangoapps/discussion/rest_api/api.py index 506c95350c..dc2d1e314d 100644 --- a/lms/djangoapps/discussion/rest_api/api.py +++ b/lms/djangoapps/discussion/rest_api/api.py @@ -1191,6 +1191,7 @@ def get_comment_list(request, thread_id, endorsed, page, page_size, flagged=Fals """ response_skip = page_size * (page - 1) reverse_order = request.GET.get('reverse_order', False) + from_mfe_sidebar = request.GET.get("enable_in_context_sidebar", False) cc_thread, context = _get_thread_and_context( request, thread_id, @@ -1237,7 +1238,7 @@ def get_comment_list(request, thread_id, endorsed, page, page_size, flagged=Fals results = _serialize_discussion_entities(request, context, responses, requested_fields, DiscussionEntity.comment) paginator = DiscussionAPIPagination(request, page, num_pages, resp_total) - track_thread_viewed_event(request, context["course"], cc_thread) + track_thread_viewed_event(request, context["course"], cc_thread, from_mfe_sidebar) return paginator.get_paginated_response(results) @@ -1421,6 +1422,7 @@ def create_thread(request, thread_data): detail. """ course_id = thread_data.get("course_id") + from_mfe_sidebar = thread_data.pop("enable_in_context_sidebar", False) user = request.user if not course_id: raise ValidationError({"course_id": ["This field is required."]}) @@ -1452,7 +1454,8 @@ def create_thread(request, thread_data): api_thread = serializer.data _do_extra_actions(api_thread, cc_thread, list(thread_data.keys()), actions_form, context, request) - track_thread_created_event(request, course, cc_thread, actions_form.cleaned_data["following"]) + track_thread_created_event(request, course, cc_thread, actions_form.cleaned_data["following"], + from_mfe_sidebar) return api_thread @@ -1474,6 +1477,7 @@ def create_comment(request, comment_data): detail. """ thread_id = comment_data.get("thread_id") + from_mfe_sidebar = comment_data.pop("enable_in_context_sidebar", False) if not thread_id: raise ValidationError({"thread_id": ["This field is required."]}) cc_thread, context = _get_thread_and_context(request, thread_id) @@ -1497,7 +1501,8 @@ def create_comment(request, comment_data): api_comment = serializer.data _do_extra_actions(api_comment, cc_comment, list(comment_data.keys()), actions_form, context, request) - track_comment_created_event(request, course, cc_comment, cc_thread["commentable_id"], followed=False) + track_comment_created_event(request, course, cc_comment, cc_thread["commentable_id"], followed=False, + from_mfe_sidebar=from_mfe_sidebar) return api_comment diff --git a/lms/djangoapps/discussion/rest_api/tests/test_api.py b/lms/djangoapps/discussion/rest_api/tests/test_api.py index d34ce44b10..d9188463fe 100644 --- a/lms/djangoapps/discussion/rest_api/tests/test_api.py +++ b/lms/djangoapps/discussion/rest_api/tests/test_api.py @@ -1888,7 +1888,8 @@ class CreateThreadTest( 'body': 'Test body', 'url': '', 'user_forums_roles': [FORUM_ROLE_STUDENT], - 'user_course_roles': [] + 'user_course_roles': [], + 'from_mfe_sidebar': False, } def test_basic_in_blackout_period(self): @@ -1975,6 +1976,7 @@ class CreateThreadTest( "url": "", "user_forums_roles": [FORUM_ROLE_STUDENT, FORUM_ROLE_MODERATOR], "user_course_roles": [], + "from_mfe_sidebar": False, } ) @@ -2007,7 +2009,8 @@ class CreateThreadTest( 'body': 'Test body', 'url': '', 'user_forums_roles': [FORUM_ROLE_STUDENT], - 'user_course_roles': [] + 'user_course_roles': [], + 'from_mfe_sidebar': False, } @ddt.data( @@ -2257,6 +2260,7 @@ class CreateCommentTest( "url": "", "user_forums_roles": [FORUM_ROLE_STUDENT], "user_course_roles": [], + "from_mfe_sidebar": False, } if parent_id: expected_event_data["response"] = {"id": parent_id} @@ -2353,6 +2357,7 @@ class CreateCommentTest( "url": "", "user_forums_roles": [FORUM_ROLE_STUDENT, FORUM_ROLE_MODERATOR], "user_course_roles": [], + "from_mfe_sidebar": False, } if parent_id: expected_event_data["response"] = {"id": parent_id}