Skip to content

Commit 48735de

Browse files
committed
feat: ✨ add spaPageview support
1 parent 5c2ea5a commit 48735de

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/helpers/get-zaraz.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type QueueItem = {
2-
method: 'track' | 'set' | 'ecommerce';
2+
method: 'track' | 'set' | 'ecommerce' | 'spaPageview';
33
arguments: unknown;
44
};
55

@@ -29,6 +29,9 @@ export function getZaraz() {
2929
ecommerce: (...args: unknown[]) => {
3030
queue.push({ method: 'ecommerce', arguments: args });
3131
},
32+
spaPageview: (...args: unknown[]) => {
33+
queue.push({ method: 'spaPageview', arguments: args });
34+
},
3235
};
3336
}
3437

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { ecommerce } from './ecommerce';
44
import { set } from './set';
5+
import { spaPageview } from './spa-pageview';
56
import { track } from './track';
67

78
declare global {
@@ -15,9 +16,11 @@ export const zaraz = {
1516
track,
1617
set,
1718
ecommerce,
19+
spaPageview,
1820
};
1921

2022
// Export typing etc.
2123
export * from './ecommerce';
2224
export * from './set';
2325
export * from './track';
26+
export * from './spa-pageview';

src/spa-pageview.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { spaPageview } from './spa-pageview';
2+
3+
const trackMock = jest.fn();
4+
const setMock = jest.fn();
5+
const ecommerceMock = jest.fn();
6+
const spaPageviewMock = jest.fn();
7+
8+
declare global {
9+
interface Window {
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
zaraz: any;
12+
}
13+
}
14+
15+
let windowObj: Window & typeof globalThis;
16+
17+
beforeAll(() => {
18+
windowObj = window;
19+
});
20+
21+
afterAll(() => {
22+
window = windowObj;
23+
});
24+
25+
describe('set()', () => {
26+
it('should call zaraz spaPageview when called', () => {
27+
window.zaraz = {
28+
track: trackMock,
29+
set: setMock,
30+
ecommerce: ecommerceMock,
31+
spaPageview: spaPageviewMock,
32+
};
33+
34+
spaPageview();
35+
36+
expect(spaPageviewMock).toHaveBeenCalledWith();
37+
});
38+
});

src/spa-pageview.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getZaraz } from './helpers/get-zaraz';
2+
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
export function spaPageview(): void {
5+
getZaraz().spaPageview();
6+
}

0 commit comments

Comments
 (0)