From ed5f472b2f360dadb260b14e87a00c959224952e Mon Sep 17 00:00:00 2001 From: Bryan Wilson Date: Mon, 28 Aug 2023 18:55:22 -0700 Subject: [PATCH 1/2] Update Segment url intercepts for fetch-based Segment js API The analytics.js API from CDN started using fetch instead of XHR on March 1 2023. Replace custom Appsembler code which does the replication of frontendJS events to ours and customer's Segment backends Override fetch calls to swap api.segmentio calls with Open edX /segmentio/send/* requests --- lms/templates/widgets/segment-io.html | 30 ++++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lms/templates/widgets/segment-io.html b/lms/templates/widgets/segment-io.html index 0e4f40ece203..5f8da23000e8 100644 --- a/lms/templates/widgets/segment-io.html +++ b/lms/templates/widgets/segment-io.html @@ -5,19 +5,25 @@ % if settings.LMS_SEGMENT_KEY and settings.LMS_SEGMENT_SITE: ## begin Copy from edx-platform/cms/templates/widgets/segment-io.html ## Appsembler: begin Segment Site - + return resource; + } + const { fetch: originalFetch } = window; + window.fetch = async (...args) => { + let [resource, config ] = args; + resource = replaceFetchResourceForSegmentSite(resource); + const response = await originalFetch(resource, config); + return response; + }; + }(); + ## Appsembler: end Segment Site ## end Copy % endif From c3da2d92ee1d15b73d4941f625e5741670021902 Mon Sep 17 00:00:00 2001 From: Bryan Wilson Date: Tue, 29 Aug 2023 12:34:39 -0700 Subject: [PATCH 2/2] Update messageId format for faked analytics.js event call --- common/djangoapps/track/views/segmentio.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/djangoapps/track/views/segmentio.py b/common/djangoapps/track/views/segmentio.py index 82aa2923752e..a61c5fdf5a5e 100644 --- a/common/djangoapps/track/views/segmentio.py +++ b/common/djangoapps/track/views/segmentio.py @@ -289,8 +289,8 @@ def send_event(request, method, **params): segment_key = helpers.get_current_site_configuration().get_secret_value('SEGMENT_KEY') if segment_key: data['writeKey'] = segment_key - data['messageId'] = 'ajs-' + uuid.uuid4().hex - site_response = requests.post(url, json=data) + data['messageId'] = 'ajs-next-' + uuid.uuid4().hex + site_response = requests.post(url, json=data) # noqa: F841 return HttpResponse( main_response.content, status=main_response.status_code,