Opt-In / Opt-Out

Your first-party data collection respects each user's opt-in/out status. The Velocidi CDP 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 = 'http://match.domain.com';
var BUTTON_PARENT_ID = "body";
// Change this to true if the system's default configuration is opted-in
var USERS_ARE_OPTED_IN_BY_DEFAULT = false;
// -----------------------------------------------------
function req(path, cb) {
var request = new XMLHttpRequest();
request.open('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));
}
};
request.send();
}
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) {
if (USERS_ARE_OPTED_IN_BY_DEFAULT) {
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"; };
setText(b);
btn.onclick = function() { b = !b; setText(b); b ? optOut(t) : optIn(t); };
document.querySelector(BUTTON_PARENT_ID).append(btn);
});
}
onDOMReady(createButton);

Note that there are two parameters, the CDP_URL where Velocidi 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.

info

This code should be deployed in a validated domain. Velocidi 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.

Last updated on