Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmp/ketch.js
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 18 additions & 1 deletion krenalis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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'.
Expand Down