# First-Party Data Collection

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.

## Code Example

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

```javascript
var AUDIENCE_URL = 'https://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', AUDIENCE_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:**

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 requests from non-valid domains will be ignored.**
