From fc37bbec1d0292a23fdf7c4d4afe65939e6af18f Mon Sep 17 00:00:00 2001 From: Syed Sajjad Hussain Shah <52817156+syedsajjadkazmii@users.noreply.github.com> Date: Fri, 24 Mar 2023 14:38:52 +0500 Subject: [PATCH] fix: fix recommendations viewed event count anomly (#787) Co-authored-by: Syed Sajjad Hussain Shah --- src/progressive-profiling/ProgressiveProfiling.jsx | 6 +++--- src/recommendations/RecommendationsPage.jsx | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/progressive-profiling/ProgressiveProfiling.jsx b/src/progressive-profiling/ProgressiveProfiling.jsx index 24fdf228..f9478b17 100644 --- a/src/progressive-profiling/ProgressiveProfiling.jsx +++ b/src/progressive-profiling/ProgressiveProfiling.jsx @@ -75,14 +75,14 @@ const ProgressiveProfiling = (props) => { }, [authenticatedUser, registrationResponse]); useEffect(() => { - if (registrationResponse) { + if (registrationResponse && authenticatedUser?.userId) { const queryParams = getAllPossibleQueryParams(registrationResponse.redirectUrl); if (enablePersonalizedRecommendations && !('enrollment_action' in queryParams)) { - const userIdStr = authenticatedUser?.userId.toString(); + const userIdStr = authenticatedUser.userId.toString(); const showRecommendations = activateRecommendationsExperiment(userIdStr); setShowRecommendationsPage(showRecommendations); if (!showRecommendations) { - trackRecommendationsViewed([], true, authenticatedUser?.userId); + trackRecommendationsViewed([], true, authenticatedUser.userId); } } } diff --git a/src/recommendations/RecommendationsPage.jsx b/src/recommendations/RecommendationsPage.jsx index baeba6aa..8ac2bdcb 100644 --- a/src/recommendations/RecommendationsPage.jsx +++ b/src/recommendations/RecommendationsPage.jsx @@ -24,6 +24,7 @@ const RecommendationsPage = (props) => { const [isLoading, setIsLoading] = useState(true); const [recommendations, setRecommendations] = useState([]); + const [algoliaRecommendations, setAlgoliaRecommendations] = useState([]); const educationLevel = EDUCATION_LEVEL_MAPPING[location.state?.educationLevel]; useEffect(() => { @@ -35,6 +36,7 @@ const RecommendationsPage = (props) => { ...course, courseKey: convertCourseRunKeytoCourseKey(course.activeRunKey), })); + setAlgoliaRecommendations(coursesWithKeys.slice(0, RECOMMENDATIONS_COUNT)); if (coursesWithKeys.length >= RECOMMENDATIONS_COUNT) { setRecommendations(coursesWithKeys.slice(0, RECOMMENDATIONS_COUNT)); @@ -55,12 +57,17 @@ const RecommendationsPage = (props) => { setRecommendations(generalRecommendations.slice(0, RECOMMENDATIONS_COUNT)); setIsLoading(false); }); - // We only want to track the recommendations returned by Algolia - const courseKeys = coursesWithKeys.map(course => course.courseKey); - trackRecommendationsViewed(courseKeys.slice(0, RECOMMENDATIONS_COUNT), false, userId); } }, [registrationResponse, DASHBOARD_URL, educationLevel, userId]); + useEffect(() => { + if (!isLoading) { + // We only want to track the recommendations returned by Algolia + const courseKeys = algoliaRecommendations.map(course => course.courseKey); + trackRecommendationsViewed(courseKeys, false, userId); + } + }, [isLoading, algoliaRecommendations, userId]); + if (!registrationResponse) { global.location.assign(DASHBOARD_URL); return null;