Skip to content

Commit

Permalink
Merge pull request #39 from Financial-Times/matth/fix-dom-ready-inter…
Browse files Browse the repository at this point in the history
…active

Fix DOM interactive/loaded performance tracking
  • Loading branch information
i-like-robots authored Jan 31, 2020
2 parents 21f0fd2 + cb5e1f8 commit bfb23d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
15 changes: 15 additions & 0 deletions src/client/trackers/real-user-performance/documentReady.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const isReady = () => document.readyState === 'complete';

export const documentReady = () => {
return new Promise((resolve) => {
if (isReady()) {
resolve();
} else {
document.addEventListener('readystatechange', () => {
if (isReady()) {
resolve();
}
});
}
});
};
34 changes: 17 additions & 17 deletions src/client/trackers/realUserMonitoringForPerformance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@ import Perfume from 'perfume.js';
import { broadcast } from '../broadcast';
import { seedIsInSample } from '../utils/seedIsInSample';
import { getSpoorId } from '../utils/getSpoorId';

// Perfume.js is a web performance package.
// @see https://zizzamia.github.io/perfume/#/default-options/
const options = {
logging: false,
firstPaint: true,
largestContentfulPaint: true,
firstInputDelay: true,
largestContentfulPaint: true,
navigationTiming: true,
};
import { documentReady } from './real-user-performance/documentReady';

// @see "Important metrics to measure" https://web.dev/metrics
const requiredMetrics = [
Expand Down Expand Up @@ -47,10 +37,14 @@ export const realUserMonitoringForPerformance = () => {
// @see: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming/type
if (navigation.type !== 'navigate') return;

const context = {
domInteractive: Math.round(navigation.domInteractive),
domComplete: Math.round(navigation.domComplete),
};
const context = {};

documentReady.then(() => {
// <https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming/domInteractive>
context.domInteractive = Math.round(navigation.domInteractive);
// <https://developer.mozilla.org/en-US/docs/Web/API/PerformanceNavigationTiming/domComplete>
context.domComplete = Math.round(navigation.domComplete);
});

/**
* analyticsTracker()
Expand All @@ -61,7 +55,7 @@ export const realUserMonitoringForPerformance = () => {
*/
let hasAlreadyBroadcast = false;

options.analyticsTracker = (({ metricName, duration, data }) => {
const analyticsTracker = (({ metricName, duration, data }) => {
if (hasAlreadyBroadcast) return;

if (duration) {
Expand All @@ -70,6 +64,8 @@ export const realUserMonitoringForPerformance = () => {
context[metricName] = Math.round(duration);
}

// Metrics with "data":
// navigationTiming, networkInformation
if (metricName === 'navigationTiming') {
context.timeToFirstByte = Math.round(data.timeToFirstByte);
}
Expand All @@ -87,5 +83,9 @@ export const realUserMonitoringForPerformance = () => {
}
});

new Perfume(options);
new Perfume({
analyticsTracker,
logging: false,
largestContentfulPaint: true
});
};

0 comments on commit bfb23d4

Please sign in to comment.