This document details the platform.privacy library. This package provides tools to help manage user privacy and consent within your Angular application. It leverages NgRx for state management (with actions, effects, reducers, selectors, and facades), defines API routes and configuration tokens, and offers reusable UI components and services for handling cookie consent, IP location tracking, and related privacy tasks.
The platform.privacy package assists with managing user privacy preferences by providing:
A high-level overview of the privacy library layout (located in /packages/platform/privacy):
.eslintrc.json, jest.config.ts, ng-package.json, package.json, project.jsontsconfig.json, tsconfig.lib.json, tsconfig.lib.prod.json, tsconfig.spec.jsonindex.ts – Main entry point re-exporting the module and public APIs.test-setup.ts – Jest test configuration.identifiers.ts – Defines namespaced keys (e.g. PLATFORM_PRIVACY_ACTION_IDENTIFIER, IP_LOCATION_STATE_FEATURE_KEY, COOKIE_CONSENT_STATE_FEATURE_KEY).cookie-consent.actions.ts, ip-location.actions.ts).cookie-consent.effects.ts, ip-location.effects.ts).ZWPPrivacyFacade) for interacting with privacy-related state.cookie-banner.component.ts and an index file exporting them.tokens.ts, config/ folder, and IP location models).zwp.privacy.module.ts).zwp.ip-location.service.ts for IP location queries.zwp.cookie.service.ts for cookie management.ZWP_PRIVACY_MODULE_ROOT_CONFIG injection token.forRoot() method accepts a configuration object of type ZWPPrivacyModuleRootConfig to enable or disable IP location tracking and configure cookie banner options.PLATFORM_PRIVACY_ACTION_IDENTIFIERIP_LOCATION_STATE_FEATURE_KEYCOOKIE_CONSENT_STATE_FEATURE_KEYipLocationReducer and cookieConsentReducer) and re-exported via a central state index.Actions are grouped into two main domains:
cookie-consent.actions.tscookie-consent.effects.ts: Trigger callbacks when cookie status changes or delay preference resets.ip-location.effects.ts: Call the IP location service (using different providers such as IPInfo, MaxMind, or browser Intl) and dispatch success/failure actions.switchMap, map, catchError, delay, withLatestFrom) to coordinate API calls and state rehydration.createReducer and on functions.ip, city, country, timezone, etc.createSelector to produce memoized slices.Facades provide a simplified API for components to interact with privacy state. The primary facade is ZWPPrivacyFacade.
ipLocationLastUpdated$: Emits the last updated timestamp for IP location.ipLocationIP$, ipLocationHostname$, ipLocationCity$, ipLocationRegion$, ipLocationCountry$, ipLocationLatitude$, ipLocationLongitude$, ipLocationOrg$, ipLocationPostCode$, ipLocationTimezone$: Streams of IP location data.cookieConsentItems$: Emits all cookie consent items.cookieConsentItemsByCategory$: Groups cookie consent items by category.cookieConsentEssentialStatus$, cookieConsentAnalyticsStatus$, cookieConsentPersonalisationStatus$, cookieConsentMarketingStatus$, cookieConsentPerformanceStatus$, cookieConsentFunctionalStatus$, cookieConsentUncategorisedStatus$: Streams for individual category statuses.cookieConsentHasUnset$: Emits a boolean indicating if any cookies have an unset status.Methods:
locateUser(): voidgetCookies(): ZWPCookie[]registerCookie(name: string, category: ZWPCookieCategory, description: string, allowCallback: () => void, denyCallback: () => void): voiddeleteCookie(name: string): voidsetCookieStatus(name: string, consentStatus: ZWPCookieStatus): voidsetMultipleCookieStatus(names: string[], consentStatus: ZWPCookieStatus): voidsetCategoryStatus(category: ZWPCookieCategory, consentStatus: ZWPCookieStatus): voidresetCookieConsent(): voidconfirmCookiePreferences(): voidshowCookieConsentBanner(): voidhideCookieConsentBanner(): voidtriggerCookieConsentCallback(name: string, consentStatus: ZWPCookieStatus): voidmodel/ folder, these include data structures for:
IPInfoStandardResponse, IPInfoFreeResponse).ZWPPrivacyModuleRootConfig (see privacy-module.root.config.ts). It configures options like:
The privacy package provides two primary services:
getIPInfoLocationFromIPInfo(): Observable<IPInfoResponse>getIPInfoLocationFromIPInfoDB(): Observable<IPInfoResponse>getIPInfoLocationFromMaxMind(): Observable<IPInfoResponse>getIPInfoLocationFromBrowserIntlTimezone(): Observable<ZWPIANATimezone>makeIPInfoHeaders(accessToken: string): HttpHeadersgetCookie(init?: string | ZWPCookieStoreGetOptions): ZWPCookie | undefinedgetCookies(init?: string | ZWPCookieStoreGetOptions): ZWPCookie[]setCookie(init: Partial<ZWPCookieListItem> | string, possibleValue?: string): voidZWPCookieSameSite enum).deleteCookie(init: string | ZWPCookieStoreDeleteOptions): voidregisterCookieConsentCallbacks(name: string, allowCallback: () => void, denyCallback: () => void): voidtriggerCookieConsentCallback(name: string, status: ZWPCookieStatus): voidopenBanner(): voidcloseBanner(): voidThe privacy package currently provides the following reusable component:
confirmCookiePreferences(), hideCookieConsentBanner(), toggleCategoriesShown()) based on user interaction.ZWPPrivacyModuleRootConfig for styling and texts.Additional components may be added in the future as needed.
nx build platform.privacy
nx test platform.privacy
import { ZWPPrivacyModule } from '@zwp/platform.privacy';
@NgModule({
imports: [
ZWPPrivacyModule.forRoot({
ipLocationEnabled: true,
ipLocationAPIProvider: /* e.g. ZWPPrivacyModuleIPLocationAPIProvider.IP_INFO */,
ipInfoApiKey: 'your-ipinfo-api-key',
ipInfoDBApiKey: 'your-alt-ipinfo-key',
cookieBanner: {
rejectButtonText: 'Reject All',
saveButtonText: 'Customise',
// other banner configuration options...
}
})
]
})
export class AppModule {}
import { Component } from '@angular/core';
import { ZWPPrivacyFacade } from '@zwp/platform.privacy';
@Component({
selector: 'app-cookie-demo',
template: `<button (click)="acceptAll()">Accept All Cookies</button>`
})
export class CookieDemoComponent {
constructor(private privacyFacade: ZWPPrivacyFacade) {}
acceptAll() {
// Example: set all cookie categories to ALLOW and confirm preferences
this.privacyFacade.setCategoryStatus('ANALYTICS', 'ALLOW');
this.privacyFacade.setCategoryStatus('FUNCTIONAL', 'ALLOW');
this.privacyFacade.confirmCookiePreferences();
this.privacyFacade.hideCookieConsentBanner();
}
}
The platform.privacy package provides a comprehensive solution for managing user privacy and consent in your Angular application. With robust NgRx-powered state management (actions, effects, reducers, selectors, and facades), well-defined models and configuration, versatile services for IP location and cookie handling, and a responsive cookie banner UI component, this package offers a scalable and maintainable architecture for privacy compliance.