diff --git a/cmp/ketch.js b/cmp/ketch.js new file mode 100644 index 0000000..c3471f3 --- /dev/null +++ b/cmp/ketch.js @@ -0,0 +1,12 @@ +// MIT License +// Copyright (c) 2026 Open2b +// See the LICENSE file for full text. + +function setupKetch(onConsent) { + const k = window.ketch + const cb = (config) => onConsent(config.purposes) + k('on', 'consent', cb) + k('on', 'userConsentUpdated', cb) +} + +export default setupKetch; diff --git a/krenalis.js b/krenalis.js index cc9d25c..a09331f 100644 --- a/krenalis.js +++ b/krenalis.js @@ -11,6 +11,7 @@ import Sender from './sender.js' import Session from './session.js' import Storage from './storage.js' import User from './user.js' +import setupKetch from './cmp/ketch.js' const version = '0.0.0' const none = () => {} @@ -50,6 +51,7 @@ class Krenalis { #isLeader = false #onFollowerQueue = null // is null when it is not the leader #debug + #consent // constructor returns a new Krenalis instance. writeKey is the write key, // endpoint denotes the endpoint URL, and options is an object containing @@ -85,6 +87,8 @@ class Krenalis { this.#user = new User(this.#storage) this.#group = new Group(this.#storage) + this.#setupConsentManagement() + // Participate in leader elections. const electionsState = new ElectionsState(this.#keysPrefix) this.#elections = new Elections(this.#id, electionsState, this.#onElection.bind(this)) @@ -537,8 +541,12 @@ class Krenalis { } } + if (this.#consent != null) { + event.context.consent = this.#consent; + } + // Apply user-provided context last so it can override automatic - // enrichments (campaign, timezone, session, etc). + // enrichments (campaign, timezone, session, consent, etc). if ('context' in options) { this.#mergeContext(event.context, options.context) } @@ -781,6 +789,15 @@ class Krenalis { return options } + #setupConsentManagement() { + const onConsent = (consent) => { + this.#consent = consent + } + if (window.ketch != null) { + setupKetch(onConsent) + } + } + // mergeTraits merges traits into the current user's or group's traits, // stores them, and returns them. If traits is undefined, it only returns // the current traits. k must be either '#user' or '#group'.