Skip to content

Commit d248feb

Browse files
greg-elmatus-tomlein
authored andcommitted
Add option to disable performanceNavigationTiming plugin (#1375)
1 parent 967733d commit d248feb

File tree

4 files changed

+75
-8
lines changed

4 files changed

+75
-8
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/javascript-tracker",
5+
"comment": "Add `performanceNavigationTiming` option to JS contexts config",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/javascript-tracker"
10+
}

trackers/javascript-tracker/src/configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ export interface JavaScriptTrackerConfiguration extends TrackerConfiguration {
3939
geolocation: boolean;
4040
clientHints: boolean | { includeHighEntropy: boolean };
4141
webVitals: boolean | { loadWebVitalsScript?: boolean; webVitalsSource?: string };
42+
performanceNavigationTiming: boolean;
4243
};
4344
}

trackers/javascript-tracker/src/features.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@ import * as WebVitals from '@snowplow/browser-plugin-web-vitals';
3030
* @param configuration - The tracker configuration object
3131
*/
3232
export function Plugins(configuration: JavaScriptTrackerConfiguration) {
33-
const {
34-
performanceTiming,
35-
gaCookies,
36-
geolocation,
37-
clientHints,
38-
webVitals
39-
} = configuration?.contexts ?? {};
33+
const { performanceTiming, gaCookies, geolocation, clientHints, webVitals, performanceNavigationTiming } =
34+
configuration?.contexts ?? {};
4035
const activatedPlugins: Array<[BrowserPlugin, {} | Record<string, Function>]> = [];
4136

4237
if (plugins.performanceTiming && performanceTiming) {
@@ -148,7 +143,7 @@ export function Plugins(configuration: JavaScriptTrackerConfiguration) {
148143
activatedPlugins.push([EventSpecificationsPlugin(), apiMethods]);
149144
}
150145

151-
if (plugins.performanceNavigationTiming) {
146+
if (plugins.performanceNavigationTiming && performanceNavigationTiming) {
152147
const { PerformanceNavigationTimingPlugin, ...apiMethods } = PerformanceNavigationTiming;
153148
activatedPlugins.push([PerformanceNavigationTimingPlugin(), apiMethods]);
154149
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { SelfDescribingJson } from '@snowplow/tracker-core';
2+
import { Plugins } from '../../src/features';
3+
4+
describe('Performance Navigation Timing', () => {
5+
let windowSpy: any;
6+
7+
const otherContexts = {
8+
webPage: false,
9+
session: false,
10+
performanceTiming: false,
11+
gaCookies: false,
12+
geolocation: false,
13+
clientHints: false,
14+
webVitals: false,
15+
};
16+
17+
const hasPerformanceNavigationTimingContext = (plugins: ReturnType<typeof Plugins>): boolean => {
18+
const pluginContexts = plugins.map((plugin) => plugin[0]?.contexts?.());
19+
const hasPerformanceContext = pluginContexts.some((contexts?: SelfDescribingJson[]) =>
20+
contexts?.some(
21+
(context: { schema?: string }) => context.schema === 'iglu:org.w3/PerformanceNavigationTiming/jsonschema/1-0-0'
22+
)
23+
);
24+
return hasPerformanceContext;
25+
};
26+
27+
beforeEach(() => {
28+
windowSpy = jest.spyOn(global, 'window', 'get');
29+
30+
// The PerformanceNavigationTiming context will only be added if the plugin can:
31+
// - Access the `performance` object on the window
32+
// - See that a value is returned from `getEntriesByType`
33+
windowSpy.mockImplementation(() => ({
34+
performance: {
35+
getEntriesByType: () => [{}],
36+
},
37+
}));
38+
});
39+
40+
it('Is enabled if contexts.performanceNavigationTiming is true', () => {
41+
const plugins = Plugins({
42+
contexts: {
43+
performanceNavigationTiming: true,
44+
...otherContexts,
45+
},
46+
});
47+
48+
expect(hasPerformanceNavigationTimingContext(plugins)).toBe(true);
49+
});
50+
51+
it('Is disabled if contexts.performanceNavigationTiming is false', () => {
52+
const plugins = Plugins({
53+
contexts: {
54+
performanceNavigationTiming: false,
55+
...otherContexts,
56+
},
57+
});
58+
59+
expect(hasPerformanceNavigationTimingContext(plugins)).toBe(false);
60+
});
61+
});

0 commit comments

Comments
 (0)