Skip to content

Commit

Permalink
feat(AU-1894): send track event when requesting translation (#1360)
Browse files Browse the repository at this point in the history
* feat(AU-1894): send track event when requesting translation

* feat: add source != target language condition
  • Loading branch information
Rodra authored Apr 17, 2024
1 parent 2da930f commit a418ba6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions plugins/UnitTranslationPlugin/translation-selection/index.jsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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({
Expand Down
21 changes: 21 additions & 0 deletions plugins/UnitTranslationPlugin/translation-selection/index.test.jsx
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -38,6 +40,7 @@ jest.mock('./useSelectLanguage', () => () => ({
setSelectedLanguage: jest.fn().mockName('setSelectedLanguage'),
}));
jest.mock('../feedback-widget', () => 'FeedbackWidget');
jest.mock('@edx/frontend-platform/analytics');

describe('<TranslationSelection />', () => {
const props = {
Expand All @@ -60,4 +63,22 @@ describe('<TranslationSelection />', () => {
const wrapper = shallow(<TranslationSelection {...props} />);
expect(wrapper.snapshot).toMatchSnapshot();
});
it('does not send track event when source != target language', async () => {
await initializeTestStore();
render(<TranslationSelection {...props} />);
expect(sendTrackEvent).not.toHaveBeenCalled();
});
it('sends track event when source != target language', async () => {
await initializeTestStore();
render(<TranslationSelection {...props} />);
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);
});
});

0 comments on commit a418ba6

Please sign in to comment.