-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from Financial-Times/rum-with-attribution
feature: Add attribution option to RUM
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './realUserMonitoringForPerformance'; | ||
export * from './realUserMonitoringWithAttribution'; | ||
export * from './pageAttention'; | ||
export * from './textCopying'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { onLCP, onFID, onCLS } from 'web-vitals/attribution'; | ||
import { broadcast } from '../broadcast'; | ||
import { seedIsInSample } from '../utils/seedIsInSample'; | ||
import { getSpoorId } from '../utils/getSpoorId'; | ||
|
||
// 1% of users | ||
const defaultSampleRate = 1; | ||
|
||
const dataFromCWV = (metric) => { | ||
if (metric.name === 'LCP') { | ||
return { | ||
name: metric.name, | ||
lcp: metric.value, | ||
lcpDelta: metric.delta, | ||
element: metric.attribution.largestShiftTarget | ||
}; | ||
} | ||
if (metric.name === 'FID') { | ||
return { | ||
name: metric.name, | ||
fid: metric.value, | ||
fidDelta: metric.delta, | ||
element: metric.attribution.eventTarget | ||
}; | ||
} | ||
if (metric.name === 'CLS') { | ||
return { | ||
name: metric.name, | ||
cls: metric.value, | ||
clsDelta: metric.delta, | ||
element: metric.attribution.largestShiftTarget | ||
}; | ||
} | ||
}; | ||
|
||
export const realUserMonitoringWithAttribution = ({ sampleRate } = {}) => { | ||
const broadcastCWV = (data) => { | ||
broadcast('oTracking.event', { | ||
action: 'performance-attribution', | ||
category: 'page', | ||
...dataFromCWV(data), | ||
}); | ||
}; | ||
|
||
// Check browser support. | ||
// @see https://developer.mozilla.org/en-US/docs/Web/API/PerformanceLongTaskTiming | ||
if (!'PerformanceLongTaskTiming' in window) return; | ||
|
||
const spoorId = getSpoorId(); | ||
const proportionToSample = sampleRate || defaultSampleRate; | ||
|
||
// Gather metrics for only a cohort of users. | ||
if (!seedIsInSample(spoorId, proportionToSample)) return; | ||
|
||
// Proceed only if the page load event is a "navigate". | ||
// @see: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming/type | ||
// When testing, you will need to open a new browser window and paste the URL in i.e. simply reloading the page will not work. | ||
const navigation = performance.getEntriesByType('navigation') && performance.getEntriesByType('navigation')[0]; | ||
if (!navigation || navigation.type !== 'navigate') return; | ||
|
||
onCLS(broadcastCWV, { reportAllChanges: true }); | ||
onFID(broadcastCWV); | ||
onLCP(broadcastCWV); | ||
}; |