Skip to main content

Opt-In / Opt-Out

Your first-party data collection respects each user's opt-in/out status. Kevel Audience can be configured to have all users opted-in or opted-out by default. When the default is to have the users opted-in, data is automatically captured from the user, but they can opt-out at any time. When disabled, the user needs to explicitly opt-in for data to start being collected.

Despite the default configuration and a user's current opt-in status, users can decide at any time to opt-in or out of tracking. To provide the user the ability to do so, there should be an opt-out button user can click to be excluded from data collection.

The following code could be used to manage the opting in and out of data collection.

var CDP_URL = '';
var BUTTON_PARENT_ID = "body";
// Change this to true if the system's default configuration is opted-in

// -----------------------------------------------------

function req(path, cb) {
var request = new XMLHttpRequest();'GET', CDP_URL + path, true);
request.withCredentials = true;
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
if (cb) cb(JSON.parse(request.responseText));

function optIn(token) { req("/privacy/opt-in?token=" + token) }
function optOut(token) { req("/privacy/opt-out?token=" + token) }
function isOptedOut(cb) { req("/privacy/status", function (json) {
cb(json.status == "optedout", json.token);
} else {
cb(json.status !== "optedin", json.token);

// -----------------------------------------------------

function onDOMReady(fn) {
if (document.readyState != 'loading') fn();
else document.addEventListener('DOMContentLoaded', fn);

function createButton() {
isOptedOut(function (b, t) {
var btn = document.createElement("button");
var setText = function(b) { btn.innerText = b ? "opt-in" : "opt-out"; };
btn.onclick = function() { b = !b; setText(b); b ? optOut(t) : optIn(t); };


Note that there are two parameters, the CDP_URL where Kevel Audience is deployed and BUTTON_PARENT_ID which is the ID of an HTML element which will hold the button.

The first part of the tag above provides the JavaScript methods to optIn, optOut and check a user's status with ifOptedOut. The second part of the tag inserts a button and toggles the user being opted in or out. The second part can be adapted to better integrate with the site where it is available.


This code should be deployed in a validated domain. Kevel Audience should be informed about the list of domains where code will be used, in order to whitelist them.

All opt-out request from non-valid domains will be ignored.