From 15c66924f009f299ddce072604ec173d4d26d286 Mon Sep 17 00:00:00 2001 From: Ural Date: Wed, 10 Apr 2024 10:58:04 +0200 Subject: [PATCH] feat(add-consent): add zaraz consent for cookies --- README.md | 10 +++++----- src/consent.spec.ts | 32 ++++++++++++++++++++++++++++++++ src/consent.ts | 10 ++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/consent.spec.ts create mode 100644 src/consent.ts diff --git a/README.md b/README.md index 4e69a50..edfed53 100644 --- a/README.md +++ b/README.md @@ -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. @@ -17,13 +18,13 @@ 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'); ``` @@ -31,8 +32,8 @@ 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', @@ -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 diff --git a/src/consent.spec.ts b/src/consent.spec.ts new file mode 100644 index 0000000..533bd6c --- /dev/null +++ b/src/consent.spec.ts @@ -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); + }); +}); diff --git a/src/consent.ts b/src/consent.ts new file mode 100644 index 0000000..27a6500 --- /dev/null +++ b/src/consent.ts @@ -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'); + } +}