Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WSTEAM1-1549: Adds click view tracking to Live Header Media #12276

Merged
merged 10 commits into from
Jan 17, 2025
63 changes: 63 additions & 0 deletions src/app/components/LiveHeaderMedia/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
render,
fireEvent,
} from '../react-testing-library-with-providers';
import * as viewTracking from '../../hooks/useViewTracker';
import * as clickTracking from '../../hooks/useClickTrackerHandler';

const fixtureData = mundoLiveFixture.data.mediaCollections;

Expand Down Expand Up @@ -159,4 +161,65 @@ describe('liveMediaStream', () => {

expect(window.mediaPlayers.p0gh4n67.play).toHaveBeenCalledTimes(playCalls);
});

describe('Event Tracking', () => {
beforeEach(() => {
jest.clearAllMocks();
});
const eventTrackingData = { componentName: 'live-header-media' };

describe('Click tracking', () => {
const clickTrackerSpy = jest.spyOn(clickTracking, 'default');

it('should not be enabled if event tracking data not provided', () => {
render(
<LiveHeaderMedia
mediaCollection={fixtureData as MediaCollection[]}
/>,
);
const playCloseButton = screen.getByTestId('watch-now-close-button');
fireEvent.click(playCloseButton);
expect(clickTrackerSpy).toHaveBeenCalledWith(undefined);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this expected? Or do we expect the click tracker spy not to have been called at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. For the tests I copied the logic used in src/app/components/Billboard/index.test.tsx.

Since we're no longer passing in eventTrackingData in as a prop, we expect the click/ view trackers to always be called, so I suppose I can remove these tests.

All that said... I had a bit of trouble with these tests. If I follow the pattern here and call .mockImplementation(); then I get error Error: Uncaught [TypeError: clickTrackerHandler is not a function]. I haven't been able to work out why.

});
it('should register click tracker if event tracking data provided', () => {
Isabella-Mitchell marked this conversation as resolved.
Show resolved Hide resolved
render(
<LiveHeaderMedia
mediaCollection={fixtureData as MediaCollection[]}
eventTrackingData={{
componentName: 'live-header-media',
}}
/>,
);
expect(clickTrackerSpy).toHaveBeenCalledWith(eventTrackingData);
expect(clickTrackerSpy).toHaveBeenCalledTimes(1);
});
});

describe('View tracking', () => {
const viewTrackerSpy = jest.spyOn(viewTracking, 'default');

it('should not be enabled if event tracking data not provided', () => {
render(
<LiveHeaderMedia
mediaCollection={fixtureData as MediaCollection[]}
/>,
);

expect(viewTrackerSpy).toHaveBeenCalledWith(undefined);
karinathomasbbc marked this conversation as resolved.
Show resolved Hide resolved
});

it('should register view tracker if event tracking data provided', () => {
Isabella-Mitchell marked this conversation as resolved.
Show resolved Hide resolved
render(
<LiveHeaderMedia
mediaCollection={fixtureData as MediaCollection[]}
eventTrackingData={{
componentName: 'live-header-media',
}}
/>,
);

expect(viewTrackerSpy).toHaveBeenCalledWith(eventTrackingData);
});
});
});
});
23 changes: 18 additions & 5 deletions src/app/components/LiveHeaderMedia/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import MediaLoader from '#app/components/MediaLoader';
import filterForBlockType from '#app/lib/utilities/blockHandlers';
import { ServiceContext } from '#app/contexts/ServiceContext';
import { RequestContext } from '#app/contexts/RequestContext';
import useViewTracker from '#app/hooks/useViewTracker';
import useClickTrackerHandler from '#app/hooks/useClickTrackerHandler';
import { EventTrackingMetadata } from '#app/models/types/eventTracking';
import styles from './index.styles';
import WARNING_LEVELS from '../MediaLoader/configs/warningLevels';
import VisuallyHiddenText from '../VisuallyHiddenText';
Expand All @@ -20,8 +23,9 @@ type WarningItem = {
short_description: string;
};

type Props = {
type LiveHeaderMediaProps = {
mediaCollection: MediaCollection[] | null;
eventTrackingData?: EventTrackingMetadata;
clickCallback?: () => void;
};

Expand All @@ -34,12 +38,16 @@ const MemoizedMediaPlayer = memo(MediaLoader);

const LiveHeaderMedia = ({
mediaCollection,
eventTrackingData,
clickCallback = () => null,
}: Props) => {
}: LiveHeaderMediaProps) => {
const clickTrackerHandler = useClickTrackerHandler(eventTrackingData);
const { translations } = useContext(ServiceContext);
const { isLite } = useContext(RequestContext);
const [showMedia, setShowMedia] = useState(false);

const viewRef = useViewTracker(eventTrackingData);

let warningLevel = WARNING_LEVELS.NO_WARNING;

if (isLite || mediaCollection == null || mediaCollection.length === 0) {
Expand Down Expand Up @@ -80,7 +88,7 @@ const LiveHeaderMedia = ({
warningLevel = WARNING_LEVELS[highestWarning.warning_code];
}

const handleClick = () => {
const clickToggleMedia = () => {
const mediaPlayer = window.mediaPlayers?.[vpid];
if (showMedia) {
mediaPlayer?.pause();
Expand All @@ -95,6 +103,11 @@ const LiveHeaderMedia = ({
clickCallback();
};

const handleClick = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
clickTrackerHandler(e);
clickToggleMedia();
};

const description = (
<>
<Text
Expand Down Expand Up @@ -138,10 +151,10 @@ const LiveHeaderMedia = ({
<p>{description}</p>
<strong>{noJs}</strong>
</noscript>
<div css={styles.componentContainer}>
<div css={styles.componentContainer} ref={viewRef}>
<button
type="button"
onClick={() => handleClick()}
onClick={e => handleClick(e)}
data-testid="watch-now-close-button"
className="focusIndicatorInvert"
css={[
Expand Down
3 changes: 3 additions & 0 deletions ws-nextjs-app/pages/[service]/live/[id]/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ const Header = ({
{mediaCollections && (
<LiveHeaderMedia
mediaCollection={mediaCollections}
eventTrackingData={{
componentName: 'live-header-media',
}}
Isabella-Mitchell marked this conversation as resolved.
Show resolved Hide resolved
Isabella-Mitchell marked this conversation as resolved.
Show resolved Hide resolved
clickCallback={watchVideoClickHandler}
/>
)}
Expand Down
Loading