JavaScript API
Open the preferences panel, read the visitor's choice and react when it changes, in the browser or on your server.
The SDK creates a global window.PrivacyQuick object as soon as it runs. Because the snippet loads with async, your own inline scripts may run first, so check that it exists before you call it, or listen for the pq:consent event on document, which works before the SDK has loaded.
Methods
| Member | Returns | What it does |
|---|---|---|
open() | nothing | Opens the preferences panel. |
acceptAll() | nothing | Allows every category your banner offers. |
rejectAll() | nothing | Keeps only strictly necessary. |
has(category) | boolean | Whether a category is allowed right now. |
consent() | object or null | The visitor's stored choice. |
on('change', fn) | function | Calls fn after each choice. Returns an unsubscribe function. |
destroy() | nothing | Removes the banner and stops the SDK on this page. |
version | string | "1" |
open()
Shows the preferences panel with a switch for each category, already set to the visitor's current choice. Use it for a "Cookie settings" link in your footer, which lets visitors withdraw consent as easily as they gave it:
<a href="#" onclick="window.PrivacyQuick && PrivacyQuick.open(); return false;">Cookie settings</a>
If you call open() before your site settings have loaded, the panel opens as soon as they arrive. If the settings never load, for example because the site ID is wrong, nothing happens. The same applies to acceptAll() and rejectAll().
acceptAll() and rejectAll()
These record a choice exactly as if the visitor had clicked the matching button. The SDK writes the consent cookie, logs the choice, sends a Consent Mode update, fires the change event and releases or holds back tagged scripts. Call them only in response to a clear action by the visitor, such as your own "Accept" button. Never call them automatically.
If rejectAll() switches off a category whose scripts already ran on the current page, the page reloads so that those scripts stop.
has(category)
if (window.PrivacyQuick && PrivacyQuick.has('analytics')) {
// safe to load analytics
}
has('necessary') is always true. Other categories return true only if the visitor allowed them and your banner offers them.
Until your site settings arrive, has() reflects the stored choice, or only necessary if there isn't one. For a visitor in an opt-out or notice region who hasn't chosen yet, categories switch on when the settings load, and no change event fires for that. For code that should run whenever a category is available, a tagged inline script (type="text/plain" data-category="…") is the most reliable option, because the SDK releases it at the right moment. See script blocking.
consent()
Returns null if the visitor hasn't made a choice on this site yet. Otherwise it returns:
{
id: "2f6c1d0e-8a4b-4c1f-9e3a-5b7d2c9f0a14", // anonymous consent ID
v: 3, // banner version the choice was made on
categories: ["necessary", "analytics"],
action: "custom", // accept_all | reject_all | custom | gpc
time: 1793973600 // Unix time, seconds
}
The consent ID is a random UUID created in the visitor's browser. It isn't linked to any personal data. If a visitor asks you to prove what they chose, they can give you this ID and you can look it up in the consent log in your dashboard. To show it on a privacy page:
<p>Your consent ID: <code id="pq-id">none yet</code></p>
<script>
window.addEventListener('load', function () {
var c = window.PrivacyQuick && PrivacyQuick.consent();
if (c) document.getElementById('pq-id').textContent = c.id;
});
</script>
The consent cookie doesn't store the action. For a choice made on an earlier page, action is worked out from the categories: reject_all if only necessary is allowed, accept_all if every category the banner offers is allowed, and custom otherwise. When you publish a new banner version, consent() keeps returning the old choice, which stays in effect until the visitor chooses again. Compare v if you need to know which version it was.
on('change', fn)
var off = PrivacyQuick.on('change', function (detail) {
// detail = { categories: ["necessary", "functional"], action: "custom" }
if (detail.categories.indexOf('functional') >= 0) loadChatWidget();
});
// later: off();
The listener runs each time a choice is made on the current page, including the automatic opt-out when a browser sends Global Privacy Control. It doesn't run on page load for a choice made on an earlier page. Use consent() or has() for that. Errors thrown in your listener are reported in the console and don't affect the SDK.
destroy()
Removes the banner and stops the SDK on the current page, for example in tests or when a single-page app tears down a view. Scripts that were already released keep running. To start the SDK again, reload the page.
The pq:consent event
Each choice also fires a pq:consent DOM event on document, with the same detail object as the change listener. You can add the listener before the SDK loads:
document.addEventListener('pq:consent', function (e) {
console.log(e.detail.action, e.detail.categories);
});
Within a single choice, things happen in this order:
- The consent cookie is written and the choice is logged.
- The Google Consent Mode update is sent.
- The
pq:consentevent fires, thenon('change')listeners run. - Tagged elements are released, or the page reloads if a category whose scripts already ran was switched off.
action | Meaning |
|---|---|
accept_all | Accept all / Accept / OK, or acceptAll() |
reject_all | Reject all, or rejectAll() |
custom | Save choices in the preferences panel |
gpc | Automatic opt-out of marketing from the browser's Global Privacy Control signal (opt-out mode only) |
The pq_consent cookie
The SDK stores the choice in a first-party cookie called pq_consent, so your server can read it too. The value is URL-encoded. Decoded, it's a query string:
id=2f6c1d0e-8a4b-4c1f-9e3a-5b7d2c9f0a14&v=3&c=necessary.analytics&t=1793973600&m=optin
| Field | Meaning |
|---|---|
id | Anonymous consent ID (random UUID v4) |
v | Banner version the choice was made on |
c | Allowed categories, separated by dots. Always starts with necessary. |
t | Time of the choice, Unix seconds |
m | Banner mode at the time: optin, optout, notice or none |
Cookie attributes: Path=/, SameSite=Lax, Secure on HTTPS pages, and Max-Age set to your consent lifetime (365 days by default, adjustable from 30 to 395 days). The cookie has no Domain attribute, so it belongs to the exact host that set it. www.example.com and example.com each keep their own choice.
No cookie means the visitor hasn't chosen yet. Unless you know the visitor is in an opt-out or notice region, treat that as "necessary only".
Reading it in PHP
<?php
// PHP URL-decodes cookie values for you
parse_str($_COOKIE['pq_consent'] ?? '', $pq);
$categories = isset($pq['c']) ? explode('.', $pq['c']) : ['necessary'];
$analytics = in_array('analytics', $categories, true);
Reading it in Node.js
function pqConsent(cookieHeader) {
const m = /(?:^|;\s*)pq_consent=([^;]*)/.exec(cookieHeader || '');
if (!m) return null;
const p = new URLSearchParams(decodeURIComponent(m[1]));
return {
id: p.get('id'),
version: Number(p.get('v')),
categories: (p.get('c') || '').split('.').filter(Boolean),
time: Number(p.get('t')),
mode: p.get('m')
};
}
// const c = pqConsent(req.headers.cookie);
// if (c && c.categories.includes('marketing')) { ... }
In other frameworks, check whether the cookie value you get is already URL-decoded before you parse it. Visitors can edit their own cookies, so use this value to respect their choice, not as proof of consent. The consent log in your dashboard is the record.
Choosing the banner language: data-lang
Add data-lang to the snippet to request a language:
<script src="https://cdn.privacyquick.com/v1/privacy.js" data-site-id="pq_xxxxxxxxxx" data-lang="fr" async></script>
Supported languages: en, fr, de, es, it, nl, pt, pl, sv, da, fi, ja, ko, zh-CN and zh-TW. Regional codes are matched to the nearest supported language. For example, de-AT becomes de, and zh-HK becomes zh-TW.
- Pro: any supported language is used. Without
data-lang, the banner follows the visitor's browser language, unless you turn automatic switching off. - Free and Starter: the banner uses the one language set in your dashboard.
data-langhas no effect unless it matches that language.
On a multilingual site, set data-lang from your page language, for example data-lang="<?= $lang ?>", so the banner matches the page rather than the browser.
Single-page apps
Load the snippet once in your HTML shell, not on each route change. The SDK guards against being loaded twice. To show embeds conditionally in React, Vue and similar frameworks, read PrivacyQuick.has() when the component mounts and subscribe with on('change') to update it. Remember to call the function that on() returns when the component unmounts.