Skip to content

Commit

Permalink
feat(index): support to attach custom URL parameters to collection URL
Browse files Browse the repository at this point in the history
  • Loading branch information
chicharr committed Sep 19, 2024
1 parent e08b9b0 commit 02f4d0b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ function trackCheckpoint(checkpoint, data, t) {
const sendPing = (pdata = data) => {
// eslint-disable-next-line object-curly-newline, max-len
const body = JSON.stringify({ weight, id, referer: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'](), checkpoint, t, ...data }, KNOWN_PROPERTIES);
const { href: url, origin } = new URL(`.rum/${weight}`, sampleRUM.collectBaseURL || sampleRUM.baseURL);
const urlParams = window.RUM_PARAMS ? `?${new URLSearchParams(window.RUM_PARAMS).toString()}` : '';
const { href: url, origin } = new URL(`.rum/${weight}${urlParams}`, sampleRUM.collectBaseURL);
if (window.location.origin === origin) {
const headers = { type: 'application/json' };
navigator.sendBeacon(url, new Blob([body], headers));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"mocha": "10.7.3",
"mocha-multi-reporters": "1.5.1",
"rollup": "4.21.3",
"rollup-plugin-cleanup": "3.2.1",
"rollup-plugin-checksum": "1.0.1",
"rollup-plugin-cleanup": "3.2.1",
"rollup-plugin-eslint-bundle": "9.0.0",
"semantic-release": "24.1.1",
"web-vitals": "4.2.3"
Expand Down
62 changes: 62 additions & 0 deletions test/it/rum-params.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<html>

<head>
<title>Test Runner</title>
<script>
// we load from localhost, and have the ability to
// change the scripts that are being served. Check the
// web-test-runner.config.js file for details
window.RUM_BASE = window.origin;
// we log what's being sent to the "server"
window.called = [];
// and navigator.sendBeacon has been replaced with
// a call to fakeSendBeacon
window.fakeSendBeacon = function (url, payload) {
// if payload is a string, we assume it's a JSON string
const searchParams = new URL(url).searchParams;
if (typeof payload === 'string') {
window.called.push({...JSON.parse(payload), searchParams});
} else {
// it's a blob
payload.text().then((text) => {
window.called.push({...JSON.parse(text), searchParams});
});
}
};
</script>
<script defer type="text/javascript" data-program="pXXXXXX" data-environment="eYYYYYY" src="/.rum/@adobe/helix-rum-js@^2/dist/rum-standalone.js"></script>
</head>

<body>
<div class="block" data-block-status="loaded">
The first block
<img src="/test/fixtures/fire.jpg" height="200" width="200">
</div>
<script type="module">
import { runTests } from '@web/test-runner-mocha';
import { sendMouse, setViewport } from '@web/test-runner-commands';
import { assert } from '@esm-bundle/chai';

runTests(async () => {
describe('Test Rum Params set as data attributes', () => {
it('All cheeckpoints include routing parameters', async () => {

await setViewport({ width: 800, height: 600 });

await sendMouse({ type: 'click', position: [100, 100] });

await new Promise((resolve) => {
setTimeout(resolve, 0);
});

await new Promise((resolve) => {
setTimeout(resolve, 100);
});

await sendMouse({ type: 'click', position: [10, 10] });
assert(window.called.every((call) => (call.searchParams?.get('program') === 'pXXXXXX' && call.searchParams?.get('environment') === 'eYYYYYY')), 'rum params missing in beacon url');
});
});
});
</script>
</body>

0 comments on commit 02f4d0b

Please sign in to comment.