diff --git a/plugins/UnitTranslationPlugin/translation-selection/index.jsx b/plugins/UnitTranslationPlugin/translation-selection/index.jsx index e4225c0d3a..177194f2f3 100644 --- a/plugins/UnitTranslationPlugin/translation-selection/index.jsx +++ b/plugins/UnitTranslationPlugin/translation-selection/index.jsx @@ -1,6 +1,7 @@ import React, { useContext, useEffect } from 'react'; import PropTypes from 'prop-types'; +import { sendTrackEvent } from '@edx/frontend-platform/analytics'; import { AppContext } from '@edx/frontend-platform/react'; import { IconButton, Icon, ProductTour } from '@openedx/paragon'; import { Language } from '@openedx/paragon/icons'; @@ -30,6 +31,20 @@ const TranslationSelection = ({ language, }); + useEffect(() => { + if ((courseId && language && selectedLanguage && unitId && userId) && (language !== selectedLanguage)) { + const eventName = 'edx.whole_course_translations.translation_requested'; + const eventProperties = { + courseId, + sourceLanguage: language, + targetLanguage: selectedLanguage, + unitId, + userId, + }; + sendTrackEvent(eventName, eventProperties); + } + }, [courseId, language, selectedLanguage, unitId, userId]); + useEffect(() => { dispatch( registerOverrideMethod({ diff --git a/plugins/UnitTranslationPlugin/translation-selection/index.test.jsx b/plugins/UnitTranslationPlugin/translation-selection/index.test.jsx index 3ec012b21a..9c117a5c12 100644 --- a/plugins/UnitTranslationPlugin/translation-selection/index.test.jsx +++ b/plugins/UnitTranslationPlugin/translation-selection/index.test.jsx @@ -1,6 +1,8 @@ +import { sendTrackEvent } from '@edx/frontend-platform/analytics'; import { shallow } from '@edx/react-unit-test-utils'; import TranslationSelection from './index'; +import { initializeTestStore, render } from '../../../src/setupTest'; jest.mock('react', () => ({ ...jest.requireActual('react'), @@ -38,6 +40,7 @@ jest.mock('./useSelectLanguage', () => () => ({ setSelectedLanguage: jest.fn().mockName('setSelectedLanguage'), })); jest.mock('../feedback-widget', () => 'FeedbackWidget'); +jest.mock('@edx/frontend-platform/analytics'); describe('', () => { const props = { @@ -60,4 +63,22 @@ describe('', () => { const wrapper = shallow(); expect(wrapper.snapshot).toMatchSnapshot(); }); + it('does not send track event when source != target language', async () => { + await initializeTestStore(); + render(); + expect(sendTrackEvent).not.toHaveBeenCalled(); + }); + it('sends track event when source != target language', async () => { + await initializeTestStore(); + render(); + const eventName = 'edx.whole_course_translations.translation_requested'; + const eventProperties = { + courseId: props.courseId, + sourceLanguage: props.language, + targetLanguage: 'en', + unitId: props.unitId, + userId: '123', + }; + expect(sendTrackEvent).not.toHaveBeenCalledWith(eventName, eventProperties); + }); });