Skip to content

Commit

Permalink
Update web-vitals to accept custom entities (#1273)
Browse files Browse the repository at this point in the history
* Update web-vitals to accept custom entities

* Update changelogs
  • Loading branch information
jethron authored Dec 13, 2023
1 parent 8a8d357 commit 665e5c2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@snowplow/browser-plugin-web-vitals",
"comment": "Add `context` support for the web_vitals event",
"type": "none"
}
],
"packageName": "@snowplow/browser-plugin-web-vitals"
}
3 changes: 2 additions & 1 deletion plugins/browser-plugin-web-vitals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ newTracker('sp1', '{{collector}}', { plugins: [ WebVitalsPlugin(/* pluginOptions
* {
* loadWebVitalsScript: Should the plugin immediately load the Core Web Vitals measurement script from UNPKG CDN.
* webVitalsSource: The URL endpoint the Web Vitals script should be loaded from. Defaults to the UNPKG CDN.
* }
* 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.
* }
*/
```

Expand Down
7 changes: 5 additions & 2 deletions plugins/browser-plugin-web-vitals/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BrowserPlugin, BrowserTracker, dispatchToTrackersInCollection } from '@snowplow/browser-tracker-core';
import { buildSelfDescribingEvent } from '@snowplow/tracker-core';
import { DynamicContext, buildSelfDescribingEvent, resolveDynamicContext } from '@snowplow/tracker-core';
import { WEB_VITALS_SCHEMA } from './schemata';
import { attachWebVitalsPageListeners, createWebVitalsScript, webVitalsListener } from './utils';

Expand All @@ -10,11 +10,13 @@ let listenersAttached = false;
interface WebVitalsPluginOptions {
loadWebVitalsScript?: boolean;
webVitalsSource?: string;
context?: DynamicContext;
}

const defaultPluginOptions = {
loadWebVitalsScript: true,
webVitalsSource: WEB_VITALS_SOURCE,
context: [],
};

/**
Expand Down Expand Up @@ -45,7 +47,8 @@ export function WebVitalsPlugin(pluginOptions: WebVitalsPluginOptions = defaultP
schema: WEB_VITALS_SCHEMA,
data: webVitalsObject,
},
})
}),
resolveDynamicContext(options.context ?? [], webVitalsObject)
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ Object {
"schema": "iglu:com.snowplowanalytics.snowplow/web_vitals/jsonschema/1-0-0",
}
`;

exports[`Web Vitals plugin Returns values for Web Vitals properties: context 1`] = `
Array [
Object {
"data": Array [
Object {
"data": Object {
"ok": true,
},
"schema": "iglu:com.example/test/jsonschema/1-0-0",
},
],
"schema": "iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0",
},
]
`;
10 changes: 7 additions & 3 deletions plugins/browser-plugin-web-vitals/test/web-vitals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ describe('Web Vitals plugin', () => {
const core = trackerCore({
corePlugins: [],
callback: (payloadBuilder) => {
const { data } = payloadBuilder.getJson()[0].json;
expect(data).toMatchSnapshot();
const [data, ...context] = payloadBuilder.getJson();
expect(data.json.data).toMatchSnapshot();
expect(context.map(({ json }) => json)).toMatchSnapshot('context');
done();
},
});

WebVitalsPlugin({ loadWebVitalsScript: false }).activateBrowserPlugin?.({ core } as BrowserTracker);
WebVitalsPlugin({
loadWebVitalsScript: false,
context: [{ schema: 'iglu:com.example/test/jsonschema/1-0-0', data: { ok: true } }],
}).activateBrowserPlugin?.({ core } as BrowserTracker);
const pagehideEvent = new PageTransitionEvent('pagehide');
jsdom.window.dispatchEvent(pagehideEvent);
});
Expand Down

0 comments on commit 665e5c2

Please sign in to comment.