Skip to content

Commit 1182172

Browse files
UralKrcHofmannZ
andauthored
feat: ✨ add consent api support
--------- Co-authored-by: Zino Hofmann <zino@hofmann.amsterdam>
1 parent 5c2ea5a commit 1182172

22 files changed

+435
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ A type-safe wrapper around the Cloudflare Zaraz Web API.
66

77
- Basic types for `zaraz.track()`;
88
- Basic types for `zaraz.set()`;
9+
- Basic types for `zaraz.consent()`;
910
- Extensive types for `zaraz.ecommerce()`;
1011
- Cheks for `zaraz` being available in the window.
1112

@@ -17,22 +18,22 @@ Import zaraz and call the desired method. That's it!
1718
import { zaraz } from 'zaraz-ts';
1819

1920
// Track custom events on your website, that might happen in real time.
20-
await zaraz.track("button clicked", { userId: "ABC-123", value: 200 })
21+
await zaraz.track('button clicked', { userId: 'ABC-123', value: 200 });
2122
```
2223

2324
```ts
2425
import { zaraz } from 'zaraz-ts';
2526

26-
// Make a variable available in all your events without manually setting it
27+
// Make a variable available in all your events without manually setting it
2728
// every time you are using zaraz.track().
2829
zaraz.set('user_id', '123456');
2930
```
3031

3132
```ts
3233
import { zaraz } from 'zaraz-ts';
3334

34-
// Track common events of the e-commerce user journey, such as when a user adds
35-
// a product to cart, starts the checkout funnel or completes an order.
35+
// Track common events of the e-commerce user journey, such as when a user adds
36+
// a product to cart, starts the checkout funnel or completes an order.
3637
await zaraz.ecommerce('Order Completed', {
3738
checkout_id: '616727740',
3839
order_id: '817286897056801',
@@ -72,7 +73,6 @@ Checkout the official Cloudflare docs for more details: https://developers.cloud
7273
This package is maintained and actively used by [Expatfile.tax][expatfile-site].
7374
The #1 US expat tax e-filing software. 🇺🇸
7475

75-
7676
[build-url]: https://img.shields.io/github/checks-status/expatfile/zaraz-ts/main
7777
[cov-img]: https://codecov.io/gh/expatfile/zaraz-ts/branch/main/graph/badge.svg?token=mbGgsweFuP
7878
[cov-url]: https://codecov.io/gh/expatfile/zaraz-ts
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { getAllCheckboxes } from './get-all-checkboxes';
2+
3+
declare global {
4+
interface Window {
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6+
zaraz: any;
7+
}
8+
}
9+
10+
let windowObj: Window & typeof globalThis;
11+
12+
beforeAll(() => {
13+
windowObj = window;
14+
});
15+
16+
afterAll(() => {
17+
window = windowObj;
18+
});
19+
20+
describe('getAllCheckboxes()', () => {
21+
it('should return all checkboxes from zaraz consent', () => {
22+
const checkboxesMock = { key1: true, key2: false };
23+
window.zaraz = {
24+
consent: {
25+
getAllCheckboxes: jest.fn().mockReturnValue(checkboxesMock),
26+
},
27+
};
28+
29+
const checkboxes = getAllCheckboxes();
30+
31+
expect(checkboxes).toEqual(checkboxesMock);
32+
});
33+
});

src/consent/get-all-checkboxes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getZaraz } from '../helpers/get-zaraz';
2+
3+
/**
4+
* Returns an object with the checkbox status of all purposes.
5+
*/
6+
export function getAllCheckboxes(): { [key: string]: boolean } {
7+
return getZaraz().consent.getAllCheckboxes();
8+
}

src/consent/get-all.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { getAll } from './get-all';
2+
3+
declare global {
4+
interface Window {
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6+
zaraz: any;
7+
}
8+
}
9+
10+
let windowObj: Window & typeof globalThis;
11+
12+
beforeAll(() => {
13+
windowObj = window;
14+
});
15+
16+
afterAll(() => {
17+
window = windowObj;
18+
});
19+
20+
describe('getAll()', () => {
21+
it('should return all consents from zaraz consent', () => {
22+
const consentsMock = { key1: true, key2: false };
23+
window.zaraz = {
24+
consent: {
25+
getAll: jest.fn().mockReturnValue(consentsMock),
26+
},
27+
};
28+
29+
const consents = getAll();
30+
31+
expect(consents).toEqual(consentsMock);
32+
});
33+
});

src/consent/get-all.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { getZaraz } from '../helpers/get-zaraz';
2+
3+
/**
4+
* Returns an object with the consent status of all purposes.
5+
*/
6+
export function getAll(): { [key: string]: boolean } {
7+
return getZaraz().consent.getAll();
8+
}

src/consent/get-purposes.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { getPurposes } from './get-purposes';
2+
3+
declare global {
4+
interface Window {
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6+
zaraz: any;
7+
}
8+
}
9+
10+
let windowObj: Window & typeof globalThis;
11+
12+
beforeAll(() => {
13+
windowObj = window;
14+
});
15+
16+
afterAll(() => {
17+
window = windowObj;
18+
});
19+
20+
describe('getPurposes()', () => {
21+
it('should return all purposes from zaraz consent', () => {
22+
const purposesMock = {
23+
key1: { name: 'Name 1', description: 'Description 1', order: 1 },
24+
key2: { name: 'Name 2', description: 'Description 2', order: 2 },
25+
};
26+
window.zaraz = {
27+
consent: {
28+
purposes: purposesMock,
29+
},
30+
};
31+
32+
const purposes = getPurposes();
33+
34+
expect(purposes).toEqual(purposesMock);
35+
});
36+
});

src/consent/get-purposes.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getZaraz } from '../helpers/get-zaraz';
2+
3+
type Purpose = {
4+
name: string;
5+
description: string;
6+
order: number;
7+
};
8+
9+
/**
10+
* An object containing all configured purposes, with their ID, name, description, and order.
11+
*/
12+
export function getPurposes(): { [key: string]: Purpose } {
13+
return getZaraz().consent.purposes;
14+
}

src/consent/get.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { get } from './get';
2+
3+
declare global {
4+
interface Window {
5+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6+
zaraz: any;
7+
}
8+
}
9+
10+
let windowObj: Window & typeof globalThis;
11+
12+
beforeAll(() => {
13+
windowObj = window;
14+
});
15+
16+
afterAll(() => {
17+
window = windowObj;
18+
});
19+
20+
describe('get()', () => {
21+
it('should return the consent status for a purpose from zaraz consent', () => {
22+
const getMock = jest.fn().mockReturnValue(true);
23+
window.zaraz = {
24+
consent: {
25+
get: getMock,
26+
},
27+
};
28+
29+
const consentStatus = get('key');
30+
31+
expect(consentStatus).toBe(true);
32+
expect(getMock).toHaveBeenCalledWith('key');
33+
});
34+
});

src/consent/get.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getZaraz } from '../helpers/get-zaraz';
2+
3+
/**
4+
* Get the current consent status for a purpose using the purpose ID.
5+
*
6+
* ```
7+
* true: The consent was granted.
8+
* false: The consent was not granted.
9+
* undefined: The purpose does not exist.
10+
* ```
11+
*/
12+
export function get(purposeId: string): boolean | undefined {
13+
return getZaraz().consent.get(purposeId);
14+
}

src/consent/index.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as consentModule from './index';
2+
3+
describe('consent', () => {
4+
it('should have the expected methods', () => {
5+
expect(consentModule.consent).toEqual({
6+
get: expect.any(Function),
7+
set: expect.any(Function),
8+
getAll: expect.any(Function),
9+
setAll: expect.any(Function),
10+
getAllCheckboxes: expect.any(Function),
11+
getPurposes: expect.any(Function),
12+
setCheckboxes: expect.any(Function),
13+
setAllCheckboxes: expect.any(Function),
14+
sendQueuedEvents: expect.any(Function),
15+
});
16+
});
17+
});

0 commit comments

Comments
 (0)