From 13b6ed901aabb1680a78417fb1a1d96bf378d747 Mon Sep 17 00:00:00 2001 From: Ken Clary Date: Fri, 30 Jul 2021 10:23:23 -0400 Subject: [PATCH] feat: add explicit courserun_key parameter to /event endpoint We add 'courserun_key' (aka "course_id" though that's technically a misnomer) as an optional parameter to the /event endpoint url. If it is not present, it will still be parsed out of the url, if the url is of the right format. Additionally, Logger.log() in js adds this parameter to its /event call, pulling it from the $$course_id global. This provides opportunity for MFEs to (separately) provide the key without concern about url parsing. TNL-7752 --- common/djangoapps/track/contexts.py | 33 ++++++++++++----------- common/djangoapps/track/views/__init__.py | 6 +++-- common/static/js/src/logger.js | 1 + 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/common/djangoapps/track/contexts.py b/common/djangoapps/track/contexts.py index 531b62b385..db7923b1b8 100644 --- a/common/djangoapps/track/contexts.py +++ b/common/djangoapps/track/contexts.py @@ -12,26 +12,27 @@ from openedx.core.lib.request_utils import COURSE_REGEX log = logging.getLogger(__name__) -def course_context_from_url(url): +def course_context_from_url(url, course_id_string=None): """ - Extracts the course_context from the given `url` and passes it on to - `course_context_from_course_id()`. + If course_id_string string is not present, extracts it from the given `url`. Either way, then passes + it on to `course_context_from_course_id()`. """ url = url or '' - - match = COURSE_REGEX.match(url) course_id = None - if match: - course_id_string = match.group('course_id') - try: - course_id = CourseKey.from_string(course_id_string) - except InvalidKeyError: - log.warning( - 'unable to parse course_id "{course_id}"'.format( - course_id=course_id_string - ), - exc_info=True - ) + + if course_id_string is None: + match = COURSE_REGEX.match(url) + if match: + course_id_string = match.group('course_id') + try: + course_id = CourseKey.from_string(course_id_string) + except InvalidKeyError: + log.warning( + 'unable to parse course_id "{course_id}"'.format( + course_id=course_id_string + ), + exc_info=True + ) return course_context_from_course_id(course_id) diff --git a/common/djangoapps/track/views/__init__.py b/common/djangoapps/track/views/__init__.py index 2d3deb3bde..9021183224 100644 --- a/common/djangoapps/track/views/__init__.py +++ b/common/djangoapps/track/views/__init__.py @@ -69,7 +69,8 @@ def user_track(request): """ Log when POST call to "event" URL is made by a user. - GET or POST call should provide "event_type", "event", and "page" arguments. + GET or POST call should provide "event_type", "event", and "page" arguments. It may optionally provide + a "courserun_key" argument (otherwise may be extracted from the page). """ try: username = request.user.username @@ -78,6 +79,7 @@ def user_track(request): name = _get_request_value(request, 'event_type') data = _get_request_value(request, 'event', {}) + course_id_string = _get_request_value(request, 'courserun_key', None) page = _get_request_value(request, 'page') if isinstance(data, str) and len(data) > 0: @@ -87,7 +89,7 @@ def user_track(request): except ValueError: pass - context_override = contexts.course_context_from_url(page) + context_override = contexts.course_context_from_url(page, course_id_string) context_override['username'] = username context_override['event_source'] = 'browser' context_override['page'] = page diff --git a/common/static/js/src/logger.js b/common/static/js/src/logger.js index 2eb0710148..c031b55e0e 100644 --- a/common/static/js/src/logger.js +++ b/common/static/js/src/logger.js @@ -59,6 +59,7 @@ return sendRequest({ event_type: eventType, event: JSON.stringify(data), + courserun_key: $$course_id, page: window.location.href }, requestOptions); },