Skip to content

Commit

Permalink
feat(add-consent): add zaraz consent for cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
UralKrc committed Apr 10, 2024
1 parent 5c2ea5a commit 15c6692
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ A type-safe wrapper around the Cloudflare Zaraz Web API.

- Basic types for `zaraz.track()`;
- Basic types for `zaraz.set()`;
- Basic types for `zaraz.consent()`;
- Extensive types for `zaraz.ecommerce()`;
- Cheks for `zaraz` being available in the window.

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

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

```ts
import { zaraz } from 'zaraz-ts';

// Make a variable available in all your events without manually setting it
// Make a variable available in all your events without manually setting it
// every time you are using zaraz.track().
zaraz.set('user_id', '123456');
```

```ts
import { zaraz } from 'zaraz-ts';

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


[build-url]: https://img.shields.io/github/checks-status/expatfile/zaraz-ts/main
[cov-img]: https://codecov.io/gh/expatfile/zaraz-ts/branch/main/graph/badge.svg?token=mbGgsweFuP
[cov-url]: https://codecov.io/gh/expatfile/zaraz-ts
Expand Down
32 changes: 32 additions & 0 deletions src/consent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { setConsent } from './consent';

const setConsentMock = jest.fn();

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
zaraz: any;
}
}

let windowObj: Window & typeof globalThis;

beforeAll(() => {
windowObj = window;
});

afterAll(() => {
window = windowObj;
});

describe('setConsent()', () => {
it('should call zaraz consent when called', () => {
window.zaraz = {
consent: setConsentMock,
};

setConsent('key', true);

expect(setConsentMock).toHaveBeenCalledWith('key', true);
});
});
10 changes: 10 additions & 0 deletions src/consent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getZaraz } from './helpers/get-zaraz';

export function setConsent(key: string, value: boolean): void {
const zaraz = getZaraz();
if (zaraz && zaraz.consent) {
zaraz.consent[key] = value;
} else {
console.error('Zaraz consent API is not available');

Check warning on line 8 in src/consent.ts

View workflow job for this annotation

GitHub Actions / Lint (18)

Unexpected console statement

Check warning on line 8 in src/consent.ts

View workflow job for this annotation

GitHub Actions / Lint (20)

Unexpected console statement
}
}

0 comments on commit 15c6692

Please sign in to comment.