@dash0/sdk-web 0.23.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dash0.iife.js +1 -1
- package/dist/dash0.iife.js.map +1 -1
- package/dist/dash0.js +1 -1
- package/dist/dash0.js.map +1 -1
- package/dist/dash0.umd.cjs +1 -1
- package/dist/dash0.umd.cjs.map +1 -1
- package/dist/modules/api/start-view.js +47 -0
- package/dist/modules/api/start-view_test.js +91 -0
- package/dist/modules/entrypoint/npm-package.js +1 -0
- package/dist/modules/entrypoint/npm-package_test.js +7 -0
- package/dist/modules/entrypoint/script.js +2 -0
- package/dist/modules/instrumentations/navigation/event.js +42 -10
- package/dist/modules/instrumentations/navigation/event_test.js +128 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/api/start-view.d.ts +33 -0
- package/dist/types/api/start-view_test.d.ts +1 -0
- package/dist/types/entrypoint/npm-package.d.ts +2 -0
- package/dist/types/entrypoint/npm-package_test.d.ts +1 -0
- package/dist/types/instrumentations/navigation/event.d.ts +18 -0
- package/dist/types/instrumentations/navigation/event_test.d.ts +1 -0
- package/package.json +2 -2
- package/src/api/start-view.ts +68 -0
- package/src/api/start-view_test.ts +125 -0
- package/src/entrypoint/npm-package.ts +2 -0
- package/src/entrypoint/npm-package_test.ts +8 -0
- package/src/entrypoint/script.ts +2 -0
- package/src/instrumentations/navigation/event.ts +63 -15
- package/src/instrumentations/navigation/event_test.ts +171 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { transmitManualPageViewEvent } from "../instrumentations/navigation/event";
|
|
2
|
+
import { debug, nowNanos, win } from "../utils";
|
|
3
|
+
import { vars } from "../vars";
|
|
4
|
+
/**
|
|
5
|
+
* Manually records a page view, side-effect free: this never calls `history.pushState` /
|
|
6
|
+
* `history.replaceState` and never mutates `location`. Intended for single-page applications
|
|
7
|
+
* that own their own router and cannot let the SDK touch navigation state (e.g. Electron apps
|
|
8
|
+
* serving the whole app from one root URL, where automatic page-view tracking would report
|
|
9
|
+
* every screen as "/").
|
|
10
|
+
*
|
|
11
|
+
* The emitted event is indistinguishable from an automatic virtual page view downstream
|
|
12
|
+
* (same `browser.page_view` event name, same `type` value), with two differences: it is never
|
|
13
|
+
* accompanied by a `change_state` value, since no history mutation occurred, and the
|
|
14
|
+
* `pageViewInstrumentation`'s `generateMetadata` callback is not invoked for manual views —
|
|
15
|
+
* supply title and attributes directly instead.
|
|
16
|
+
*
|
|
17
|
+
* @param name The name of the view, e.g. "/settings". Transmitted as the page view's title.
|
|
18
|
+
* @param opts Additional page view details.
|
|
19
|
+
*/
|
|
20
|
+
export function startView(name, opts) {
|
|
21
|
+
if (vars.endpoints.length === 0) {
|
|
22
|
+
debug("Dash0 SDK has not been initialized. Ignoring startView call.");
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// The script entrypoint forwards dash0("startView", ...) arguments without type checking,
|
|
26
|
+
// so malformed calls must degrade to a logged no-op instead of throwing. An uncaught throw
|
|
27
|
+
// here would abort the command-queue drain and drop all subsequently queued api calls.
|
|
28
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
29
|
+
debug("startView requires a non-empty view name. Ignoring startView call.");
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
let url;
|
|
33
|
+
if (opts?.url != null) {
|
|
34
|
+
try {
|
|
35
|
+
url = new URL(opts.url, win?.location.href);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
debug("Failed to parse startView url option. Falling back to the current location.", e);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
transmitManualPageViewEvent({
|
|
42
|
+
timeUnixNano: nowNanos(),
|
|
43
|
+
title: name,
|
|
44
|
+
url,
|
|
45
|
+
attributes: opts?.attributes,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
vi.mock("../transport", () => ({
|
|
3
|
+
sendLog: vi.fn(),
|
|
4
|
+
}));
|
|
5
|
+
import { sendLog } from "../transport";
|
|
6
|
+
import { startView } from "./start-view";
|
|
7
|
+
import { vars } from "../vars";
|
|
8
|
+
const sendLogMock = sendLog;
|
|
9
|
+
describe("startView", () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
sendLogMock.mockClear();
|
|
12
|
+
vars.endpoints = [{ url: "https://example.com", authToken: "auth_abc123" }];
|
|
13
|
+
vars.pageViewInstrumentation = { trackVirtualPageViews: true, includeParts: [] };
|
|
14
|
+
});
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
vi.clearAllMocks();
|
|
17
|
+
vars.endpoints = [];
|
|
18
|
+
});
|
|
19
|
+
it("accepts a string shorthand and uses it as the title", () => {
|
|
20
|
+
startView("/settings");
|
|
21
|
+
expect(sendLogMock).toHaveBeenCalledTimes(1);
|
|
22
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
23
|
+
const bodyValues = log.body?.kvlistValue?.values;
|
|
24
|
+
expect(bodyValues).toEqual(expect.arrayContaining([{ key: "title", value: { stringValue: "/settings" } }]));
|
|
25
|
+
});
|
|
26
|
+
it("accepts an options object with attributes", () => {
|
|
27
|
+
startView("/settings", { attributes: { "app.screen": "settings" } });
|
|
28
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
29
|
+
const bodyValues = log.body?.kvlistValue?.values;
|
|
30
|
+
expect(bodyValues).toEqual(expect.arrayContaining([{ key: "title", value: { stringValue: "/settings" } }]));
|
|
31
|
+
expect(log.attributes).toEqual(expect.arrayContaining([{ key: "app.screen", value: { stringValue: "settings" } }]));
|
|
32
|
+
});
|
|
33
|
+
it("parses a relative url option and reflects it in page.url.path", () => {
|
|
34
|
+
startView("/settings", { url: "/settings" });
|
|
35
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
36
|
+
expect(log.attributes).toEqual(expect.arrayContaining([{ key: "page.url.path", value: { stringValue: "/settings" } }]));
|
|
37
|
+
});
|
|
38
|
+
it("falls back to the current location on an invalid url", () => {
|
|
39
|
+
startView("/settings", { url: "http://" });
|
|
40
|
+
expect(sendLogMock).toHaveBeenCalledTimes(1);
|
|
41
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
42
|
+
expect(log.attributes).toEqual(expect.arrayContaining([
|
|
43
|
+
// eslint-disable-next-line no-restricted-globals
|
|
44
|
+
{ key: "page.url.full", value: { stringValue: window.location.href } },
|
|
45
|
+
// eslint-disable-next-line no-restricted-globals
|
|
46
|
+
{ key: "page.url.domain", value: { stringValue: window.location.hostname } },
|
|
47
|
+
]));
|
|
48
|
+
});
|
|
49
|
+
it("resolves an absolute cross-origin url override", () => {
|
|
50
|
+
startView("/settings", { url: "https://other-origin.example/path" });
|
|
51
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
52
|
+
expect(log.attributes).toEqual(expect.arrayContaining([
|
|
53
|
+
{ key: "page.url.full", value: { stringValue: "https://other-origin.example/path" } },
|
|
54
|
+
{ key: "page.url.domain", value: { stringValue: "other-origin.example" } },
|
|
55
|
+
{ key: "page.url.path", value: { stringValue: "/path" } },
|
|
56
|
+
]));
|
|
57
|
+
});
|
|
58
|
+
it("does not touch history or location", () => {
|
|
59
|
+
// eslint-disable-next-line no-restricted-globals
|
|
60
|
+
const originalHref = window.location.href;
|
|
61
|
+
// eslint-disable-next-line no-restricted-globals
|
|
62
|
+
const originalLength = window.history.length;
|
|
63
|
+
startView("/settings");
|
|
64
|
+
// eslint-disable-next-line no-restricted-globals
|
|
65
|
+
expect(window.location.href).toBe(originalHref);
|
|
66
|
+
// eslint-disable-next-line no-restricted-globals
|
|
67
|
+
expect(window.history.length).toBe(originalLength);
|
|
68
|
+
});
|
|
69
|
+
it("ignores calls without a name, as they can arrive via the untyped script entrypoint", () => {
|
|
70
|
+
// @ts-expect-error deliberately calling without arguments, mirroring dash0("startView")
|
|
71
|
+
startView();
|
|
72
|
+
startView(undefined);
|
|
73
|
+
startView(null);
|
|
74
|
+
expect(sendLogMock).not.toHaveBeenCalled();
|
|
75
|
+
});
|
|
76
|
+
it("ignores calls with a non-string or empty name", () => {
|
|
77
|
+
startView(123);
|
|
78
|
+
startView({ name: "/settings" });
|
|
79
|
+
startView("");
|
|
80
|
+
expect(sendLogMock).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
it("tolerates a nullish options argument", () => {
|
|
83
|
+
startView("/settings", null);
|
|
84
|
+
expect(sendLogMock).toHaveBeenCalledTimes(1);
|
|
85
|
+
});
|
|
86
|
+
it("is a no-op before init (no endpoints configured)", () => {
|
|
87
|
+
vars.endpoints = [];
|
|
88
|
+
startView("/settings");
|
|
89
|
+
expect(sendLogMock).not.toHaveBeenCalled();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -7,6 +7,7 @@ export * from "../api/events";
|
|
|
7
7
|
export * from "../api/log-level";
|
|
8
8
|
export { terminateSession } from "../api/session";
|
|
9
9
|
export { reportError } from "../api/report-error";
|
|
10
|
+
export { startView } from "../api/start-view";
|
|
10
11
|
export function init(opts) {
|
|
11
12
|
debug(`${INIT_MESSAGE} (via package)`);
|
|
12
13
|
initApi(opts);
|
|
@@ -7,6 +7,7 @@ import { terminateSession } from "../api/session";
|
|
|
7
7
|
import { addSignalAttribute, removeSignalAttribute } from "../api/attributes";
|
|
8
8
|
import { sendEvent } from "../api/events";
|
|
9
9
|
import { setActiveLogLevel } from "../api/log-level";
|
|
10
|
+
import { startView } from "../api/start-view";
|
|
10
11
|
/**
|
|
11
12
|
* All the APIs exposed through the script tag via `dash0('{{api name}}')`
|
|
12
13
|
*/
|
|
@@ -20,6 +21,7 @@ const scriptApis = {
|
|
|
20
21
|
removeSignalAttribute,
|
|
21
22
|
setActiveLogLevel,
|
|
22
23
|
sendEvent,
|
|
24
|
+
startView,
|
|
23
25
|
};
|
|
24
26
|
init();
|
|
25
27
|
function init() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { addAttribute, getTraceContextForPageLoad } from "../../utils/otel";
|
|
1
|
+
import { addAttribute, addAttributes, getTraceContextForPageLoad } from "../../utils/otel";
|
|
2
2
|
import { EVENT_NAME, EVENT_NAMES, LOG_SEVERITIES, PAGE_VIEW_CHANGE_STATE, PAGE_VIEW_CHANGE_STATE_VALUES, PAGE_VIEW_TYPE, PAGE_VIEW_TYPE_VALUES, } from "../../semantic-conventions";
|
|
3
3
|
import { doc, NO_VALUE_FALLBACK } from "../../utils";
|
|
4
4
|
import { addCommonAttributes } from "../../attributes";
|
|
@@ -9,23 +9,26 @@ function getPageViewMeta(url) {
|
|
|
9
9
|
return {};
|
|
10
10
|
return vars.pageViewInstrumentation.generateMetadata?.(url) ?? {};
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
const meta = getPageViewMeta(url);
|
|
12
|
+
function buildAndSendPageViewLog(opts) {
|
|
14
13
|
const attributes = [];
|
|
15
14
|
addAttribute(attributes, EVENT_NAME, EVENT_NAMES.PAGE_VIEW);
|
|
16
|
-
if (
|
|
17
|
-
Object.entries(
|
|
15
|
+
if (opts.metaAttributes) {
|
|
16
|
+
Object.entries(opts.metaAttributes).forEach(([key, value]) => addAttribute(attributes, key, value));
|
|
18
17
|
}
|
|
19
|
-
addCommonAttributes(attributes, { url });
|
|
18
|
+
addCommonAttributes(attributes, { url: opts.url });
|
|
19
|
+
// Add custom attributes last to allow overrides
|
|
20
|
+
addAttributes(attributes, opts.customAttributes);
|
|
20
21
|
const bodyAttributes = [];
|
|
21
|
-
addAttribute(bodyAttributes, "title",
|
|
22
|
+
addAttribute(bodyAttributes, "title", opts.title ?? doc?.title ?? NO_VALUE_FALLBACK);
|
|
22
23
|
if (doc?.referrer) {
|
|
23
24
|
addAttribute(bodyAttributes, "referrer", doc.referrer);
|
|
24
25
|
}
|
|
25
|
-
addAttribute(bodyAttributes, PAGE_VIEW_TYPE,
|
|
26
|
-
|
|
26
|
+
addAttribute(bodyAttributes, PAGE_VIEW_TYPE, opts.pageViewType);
|
|
27
|
+
if (opts.changeState) {
|
|
28
|
+
addAttribute(bodyAttributes, PAGE_VIEW_CHANGE_STATE, opts.changeState);
|
|
29
|
+
}
|
|
27
30
|
const log = {
|
|
28
|
-
timeUnixNano: timeUnixNano,
|
|
31
|
+
timeUnixNano: opts.timeUnixNano,
|
|
29
32
|
attributes: attributes,
|
|
30
33
|
severityNumber: LOG_SEVERITIES.INFO,
|
|
31
34
|
severityText: "INFO",
|
|
@@ -42,3 +45,32 @@ export function transmitPageViewEvent(timeUnixNano, url, virtual, replaced) {
|
|
|
42
45
|
}
|
|
43
46
|
sendLog(log);
|
|
44
47
|
}
|
|
48
|
+
export function transmitPageViewEvent(timeUnixNano, url, virtual, replaced) {
|
|
49
|
+
const meta = getPageViewMeta(url);
|
|
50
|
+
buildAndSendPageViewLog({
|
|
51
|
+
timeUnixNano,
|
|
52
|
+
url,
|
|
53
|
+
title: meta.title,
|
|
54
|
+
metaAttributes: meta.attributes,
|
|
55
|
+
pageViewType: virtual ? PAGE_VIEW_TYPE_VALUES.VIRTUAL : PAGE_VIEW_TYPE_VALUES.INITIAL,
|
|
56
|
+
changeState: replaced ? PAGE_VIEW_CHANGE_STATE_VALUES.REPLACE : PAGE_VIEW_CHANGE_STATE_VALUES.PUSH,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Emits a manually-triggered page view log, e.g. from the public `startView` API.
|
|
61
|
+
* Deliberately does NOT read `vars.pageViewInstrumentation.generateMetadata` and does NOT
|
|
62
|
+
* include a `change_state` body key (a manual view is neither a pushState nor replaceState).
|
|
63
|
+
* The emitted `type` is PAGE_VIEW_TYPE_VALUES.VIRTUAL, matching automatic virtual page views —
|
|
64
|
+
* manual views are deliberately indistinguishable from virtual ones downstream.
|
|
65
|
+
* Caller-provided attributes are added after the SDK-generated ones so they can override them,
|
|
66
|
+
* matching the sendEvent semantics.
|
|
67
|
+
*/
|
|
68
|
+
export function transmitManualPageViewEvent(opts) {
|
|
69
|
+
buildAndSendPageViewLog({
|
|
70
|
+
timeUnixNano: opts.timeUnixNano,
|
|
71
|
+
url: opts.url,
|
|
72
|
+
title: opts.title,
|
|
73
|
+
customAttributes: opts.attributes,
|
|
74
|
+
pageViewType: PAGE_VIEW_TYPE_VALUES.VIRTUAL,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
vi.mock("../../transport", () => ({
|
|
3
|
+
sendLog: vi.fn(),
|
|
4
|
+
}));
|
|
5
|
+
import { sendLog } from "../../transport";
|
|
6
|
+
import { transmitPageViewEvent, transmitManualPageViewEvent } from "./event";
|
|
7
|
+
import { vars } from "../../vars";
|
|
8
|
+
const sendLogMock = sendLog;
|
|
9
|
+
describe("transmitPageViewEvent", () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
sendLogMock.mockClear();
|
|
12
|
+
vars.pageViewInstrumentation = { trackVirtualPageViews: true, includeParts: [] };
|
|
13
|
+
});
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
vi.clearAllMocks();
|
|
16
|
+
});
|
|
17
|
+
it("transmits an initial page view with type=INITIAL and change_state=pushState", () => {
|
|
18
|
+
transmitPageViewEvent("1700000000000000000", new URL("https://example.com/landing"));
|
|
19
|
+
expect(sendLogMock).toHaveBeenCalledTimes(1);
|
|
20
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
21
|
+
expect(log.timeUnixNano).toBe("1700000000000000000");
|
|
22
|
+
expect(log.severityNumber).toBe(9);
|
|
23
|
+
expect(log.severityText).toBe("INFO");
|
|
24
|
+
expect(log.attributes).toEqual(expect.arrayContaining([{ key: "event.name", value: { stringValue: "browser.page_view" } }]));
|
|
25
|
+
const bodyValues = log.body?.kvlistValue?.values;
|
|
26
|
+
expect(bodyValues).toEqual(expect.arrayContaining([
|
|
27
|
+
{ key: "type", value: { doubleValue: 0 } },
|
|
28
|
+
{ key: "change_state", value: { stringValue: "pushState" } },
|
|
29
|
+
]));
|
|
30
|
+
});
|
|
31
|
+
it("transmits a virtual page view with type=VIRTUAL and change_state=replaceState when replaced", () => {
|
|
32
|
+
transmitPageViewEvent("1700000000000000000", new URL("https://example.com/settings"), true, true);
|
|
33
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
34
|
+
const bodyValues = log.body?.kvlistValue?.values;
|
|
35
|
+
expect(bodyValues).toEqual(expect.arrayContaining([
|
|
36
|
+
{ key: "type", value: { doubleValue: 1 } },
|
|
37
|
+
{ key: "change_state", value: { stringValue: "replaceState" } },
|
|
38
|
+
]));
|
|
39
|
+
});
|
|
40
|
+
it("applies generateMetadata title and attributes when provided", () => {
|
|
41
|
+
vars.pageViewInstrumentation = {
|
|
42
|
+
trackVirtualPageViews: true,
|
|
43
|
+
includeParts: [],
|
|
44
|
+
generateMetadata: () => ({ title: "Custom Title", attributes: { "app.screen": "settings" } }),
|
|
45
|
+
};
|
|
46
|
+
transmitPageViewEvent("1700000000000000000", new URL("https://example.com/settings"), true);
|
|
47
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
48
|
+
expect(log.attributes).toEqual(expect.arrayContaining([{ key: "app.screen", value: { stringValue: "settings" } }]));
|
|
49
|
+
const bodyValues = log.body?.kvlistValue?.values;
|
|
50
|
+
expect(bodyValues).toEqual(expect.arrayContaining([{ key: "title", value: { stringValue: "Custom Title" } }]));
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe("transmitManualPageViewEvent", () => {
|
|
54
|
+
beforeEach(() => {
|
|
55
|
+
sendLogMock.mockClear();
|
|
56
|
+
vars.pageViewInstrumentation = {
|
|
57
|
+
trackVirtualPageViews: true,
|
|
58
|
+
includeParts: [],
|
|
59
|
+
generateMetadata: () => ({ title: "should not be used", attributes: { should_not_appear: true } }),
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
afterEach(() => {
|
|
63
|
+
vi.clearAllMocks();
|
|
64
|
+
vars.pageViewInstrumentation = { trackVirtualPageViews: true, includeParts: [] };
|
|
65
|
+
});
|
|
66
|
+
it("emits type=VIRTUAL and no change_state key", () => {
|
|
67
|
+
transmitManualPageViewEvent({ timeUnixNano: "1700000000000000000", title: "/settings" });
|
|
68
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
69
|
+
const bodyValues = log.body?.kvlistValue?.values;
|
|
70
|
+
expect(bodyValues).toEqual(expect.arrayContaining([
|
|
71
|
+
{ key: "type", value: { doubleValue: 1 } },
|
|
72
|
+
{ key: "title", value: { stringValue: "/settings" } },
|
|
73
|
+
]));
|
|
74
|
+
expect(bodyValues.find((v) => v.key === "change_state")).toBeUndefined();
|
|
75
|
+
});
|
|
76
|
+
it("does not invoke vars.pageViewInstrumentation.generateMetadata", () => {
|
|
77
|
+
const generateMetadata = vi.fn().mockReturnValue({ title: "ignored" });
|
|
78
|
+
vars.pageViewInstrumentation = { trackVirtualPageViews: true, includeParts: [], generateMetadata };
|
|
79
|
+
transmitManualPageViewEvent({ timeUnixNano: "1700000000000000000", title: "/settings" });
|
|
80
|
+
expect(generateMetadata).not.toHaveBeenCalled();
|
|
81
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
82
|
+
const bodyValues = log.body?.kvlistValue?.values;
|
|
83
|
+
expect(bodyValues).toEqual(expect.arrayContaining([{ key: "title", value: { stringValue: "/settings" } }]));
|
|
84
|
+
});
|
|
85
|
+
it("merges custom attributes into signal attributes", () => {
|
|
86
|
+
transmitManualPageViewEvent({
|
|
87
|
+
timeUnixNano: "1700000000000000000",
|
|
88
|
+
title: "/settings",
|
|
89
|
+
attributes: { "app.screen": "settings", "app.tab_index": 2 },
|
|
90
|
+
});
|
|
91
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
92
|
+
expect(log.attributes).toEqual(expect.arrayContaining([
|
|
93
|
+
{ key: "app.screen", value: { stringValue: "settings" } },
|
|
94
|
+
{ key: "app.tab_index", value: { doubleValue: 2 } },
|
|
95
|
+
]));
|
|
96
|
+
});
|
|
97
|
+
it("adds custom attributes after SDK-generated ones so they can override", () => {
|
|
98
|
+
transmitManualPageViewEvent({
|
|
99
|
+
timeUnixNano: "1700000000000000000",
|
|
100
|
+
title: "/settings",
|
|
101
|
+
url: new URL("https://example.com/real-path"),
|
|
102
|
+
attributes: { "page.url.path": "/custom-path" },
|
|
103
|
+
});
|
|
104
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
105
|
+
const pathAttributes = log.attributes.filter((attr) => attr.key === "page.url.path");
|
|
106
|
+
expect(pathAttributes.length).toBeGreaterThan(1);
|
|
107
|
+
expect(pathAttributes[pathAttributes.length - 1]).toEqual({
|
|
108
|
+
key: "page.url.path",
|
|
109
|
+
value: { stringValue: "/custom-path" },
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
it("passes url through to page.url.* attributes when provided", () => {
|
|
113
|
+
transmitManualPageViewEvent({
|
|
114
|
+
timeUnixNano: "1700000000000000000",
|
|
115
|
+
title: "/settings",
|
|
116
|
+
url: new URL("https://example.com/settings"),
|
|
117
|
+
});
|
|
118
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
119
|
+
expect(log.attributes).toEqual(expect.arrayContaining([{ key: "page.url.path", value: { stringValue: "/settings" } }]));
|
|
120
|
+
});
|
|
121
|
+
it("includes referrer the same way the auto path does", () => {
|
|
122
|
+
transmitManualPageViewEvent({ timeUnixNano: "1700000000000000000", title: "/settings" });
|
|
123
|
+
const log = sendLogMock.mock.calls[0][0];
|
|
124
|
+
const bodyValues = log.body?.kvlistValue?.values;
|
|
125
|
+
// doc.referrer is empty string in jsdom by default, so "referrer" key should be absent
|
|
126
|
+
expect(bodyValues.find((v) => v.key === "referrer")).toBeUndefined();
|
|
127
|
+
});
|
|
128
|
+
});
|