Skip to content

Commit 665e5c2

Browse files
authored
Update web-vitals to accept custom entities (#1273)
* Update web-vitals to accept custom entities * Update changelogs
1 parent 8a8d357 commit 665e5c2

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
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/browser-plugin-web-vitals",
5+
"comment": "Add `context` support for the web_vitals event",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-plugin-web-vitals"
10+
}

plugins/browser-plugin-web-vitals/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ newTracker('sp1', '{{collector}}', { plugins: [ WebVitalsPlugin(/* pluginOptions
4343
* {
4444
* loadWebVitalsScript: Should the plugin immediately load the Core Web Vitals measurement script from UNPKG CDN.
4545
* webVitalsSource: The URL endpoint the Web Vitals script should be loaded from. Defaults to the UNPKG CDN.
46-
* }
46+
* context: Array of entity objects or entity-generating functions (the web_vitals payload is passed as a parameter) to attach to the web_vitals event.
47+
* }
4748
*/
4849
```
4950

plugins/browser-plugin-web-vitals/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BrowserPlugin, BrowserTracker, dispatchToTrackersInCollection } from '@snowplow/browser-tracker-core';
2-
import { buildSelfDescribingEvent } from '@snowplow/tracker-core';
2+
import { DynamicContext, buildSelfDescribingEvent, resolveDynamicContext } from '@snowplow/tracker-core';
33
import { WEB_VITALS_SCHEMA } from './schemata';
44
import { attachWebVitalsPageListeners, createWebVitalsScript, webVitalsListener } from './utils';
55

@@ -10,11 +10,13 @@ let listenersAttached = false;
1010
interface WebVitalsPluginOptions {
1111
loadWebVitalsScript?: boolean;
1212
webVitalsSource?: string;
13+
context?: DynamicContext;
1314
}
1415

1516
const defaultPluginOptions = {
1617
loadWebVitalsScript: true,
1718
webVitalsSource: WEB_VITALS_SOURCE,
19+
context: [],
1820
};
1921

2022
/**
@@ -45,7 +47,8 @@ export function WebVitalsPlugin(pluginOptions: WebVitalsPluginOptions = defaultP
4547
schema: WEB_VITALS_SCHEMA,
4648
data: webVitalsObject,
4749
},
48-
})
50+
}),
51+
resolveDynamicContext(options.context ?? [], webVitalsObject)
4952
);
5053
});
5154
}

plugins/browser-plugin-web-vitals/test/__snapshots__/web-vitals.test.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ Object {
1414
"schema": "iglu:com.snowplowanalytics.snowplow/web_vitals/jsonschema/1-0-0",
1515
}
1616
`;
17+
18+
exports[`Web Vitals plugin Returns values for Web Vitals properties: context 1`] = `
19+
Array [
20+
Object {
21+
"data": Array [
22+
Object {
23+
"data": Object {
24+
"ok": true,
25+
},
26+
"schema": "iglu:com.example/test/jsonschema/1-0-0",
27+
},
28+
],
29+
"schema": "iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0",
30+
},
31+
]
32+
`;

plugins/browser-plugin-web-vitals/test/web-vitals.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ describe('Web Vitals plugin', () => {
4040
const core = trackerCore({
4141
corePlugins: [],
4242
callback: (payloadBuilder) => {
43-
const { data } = payloadBuilder.getJson()[0].json;
44-
expect(data).toMatchSnapshot();
43+
const [data, ...context] = payloadBuilder.getJson();
44+
expect(data.json.data).toMatchSnapshot();
45+
expect(context.map(({ json }) => json)).toMatchSnapshot('context');
4546
done();
4647
},
4748
});
4849

49-
WebVitalsPlugin({ loadWebVitalsScript: false }).activateBrowserPlugin?.({ core } as BrowserTracker);
50+
WebVitalsPlugin({
51+
loadWebVitalsScript: false,
52+
context: [{ schema: 'iglu:com.example/test/jsonschema/1-0-0', data: { ok: true } }],
53+
}).activateBrowserPlugin?.({ core } as BrowserTracker);
5054
const pagehideEvent = new PageTransitionEvent('pagehide');
5155
jsdom.window.dispatchEvent(pagehideEvent);
5256
});

0 commit comments

Comments
 (0)