Skip to content

Commit a3399cd

Browse files
domlanderabeddow91
andcommitted
Create SectionTracker component
Co-authored-by: Anna Beddow <[email protected]>
1 parent 8a275ec commit a3399cd

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

dotcom-rendering/src/components/FrontSection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ export const FrontSection = ({
624624
data-component={ophanComponentName}
625625
data-container-name={containerName}
626626
data-container-level={containerLevel}
627-
data-tracking={true}
627+
data-collection-tracking={true}
628628
css={[
629629
fallbackStyles,
630630
containerStylesUntilLeftCol,

dotcom-rendering/src/components/SectionTracker.importable.tsx

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,66 @@
1+
import { log } from '@guardian/libs';
2+
import type { ComponentEvent } from '@guardian/ophan-tracker-js';
13
import { useEffect } from 'react';
2-
import { getOphan } from '../client/ophan/ophan';
4+
import { submitComponentEvent } from '../client/ophan/ophan';
35

6+
/**
7+
* Send submitComponentEvent event with action: 'INSERT' when the collection is rendered on the page.
8+
* Send submitComponentEvent event with action: 'VIEW' when a collection is viewed by the user.
9+
*/
410
export const SectionTracker = () => {
511
useEffect(() => {
612
const elements: NodeListOf<HTMLElement> = document.querySelectorAll(
7-
'[data-tracking="true"]',
13+
'[data-collection-tracking="true"]',
814
);
915

10-
const trackAttention = async () => {
16+
const recordInsertEvent = () => {
17+
try {
18+
for (const [index, el] of elements.entries()) {
19+
const sectionName = el.getAttribute('data-component');
20+
if (sectionName === null) break;
21+
22+
const ophanComponentEvent: ComponentEvent = {
23+
component: {
24+
componentType: 'CONTAINER',
25+
id: el.id,
26+
labels: [
27+
sectionName,
28+
index.toString(),
29+
el.getBoundingClientRect().top.toFixed(0),
30+
],
31+
},
32+
action: 'INSERT',
33+
};
34+
35+
void submitComponentEvent(ophanComponentEvent, 'Web');
36+
}
37+
} catch (error) {
38+
log('dotcom', 'Failed to track video attention:', error);
39+
}
40+
};
41+
42+
const recordViewEvent = () => {
1143
try {
12-
const ophan = await getOphan('Web');
1344
for (const el of elements) {
14-
ophan.trackComponentAttention(el.id, el, 0.1, false);
45+
const ophanComponentEvent: ComponentEvent = {
46+
component: {
47+
componentType: 'CONTAINER',
48+
id: el.id,
49+
labels: ['label'],
50+
},
51+
action: 'VIEW',
52+
};
53+
54+
void submitComponentEvent(ophanComponentEvent, 'Web');
1555
}
1656
} catch (error) {
17-
console.error('Failed to track video attention:', error);
57+
log('dotcom', 'Failed to track video attention:', error);
1858
}
1959
};
20-
void trackAttention();
60+
61+
void recordInsertEvent();
62+
63+
void recordViewEvent();
2164
}, []);
2265

2366
return null;

dotcom-rendering/src/layouts/FrontLayout.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { LabsHeader } from '../components/LabsHeader';
2424
import { LabsSection } from '../components/LabsSection';
2525
import { Masthead } from '../components/Masthead/Masthead';
2626
import { Section } from '../components/Section';
27+
import { SectionTracker } from '../components/SectionTracker.importable';
2728
import { Snap } from '../components/Snap';
2829
import { SnapCssSandbox } from '../components/SnapCssSandbox';
2930
import { StickyBottomBanner } from '../components/StickyBottomBanner.importable';
@@ -51,7 +52,6 @@ import type {
5152
} from '../types/front';
5253
import { pageSkinContainer } from './lib/pageSkin';
5354
import { BannerWrapper, Stuck } from './lib/stickiness';
54-
import { SectionTracker } from '../components/SectionTracker.importable';
5555

5656
interface Props {
5757
front: Front;
@@ -191,9 +191,6 @@ export const FrontLayout = ({ front, NAV }: Props) => {
191191

192192
return (
193193
<>
194-
<Island priority={'critical'}>
195-
<SectionTracker />
196-
</Island>
197194
<div data-print-layout="hide" id="bannerandheader">
198195
{renderAds && (
199196
<Stuck>
@@ -750,6 +747,10 @@ export const FrontLayout = ({ front, NAV }: Props) => {
750747
/>
751748
</Island>
752749
</BannerWrapper>
750+
751+
<Island priority="enhancement" defer={{ until: 'idle' }}>
752+
<SectionTracker />
753+
</Island>
753754
</>
754755
);
755756
};

0 commit comments

Comments
 (0)