# Consent and GDPR
> Documentation for Consent and GDPR in the web tracker.
> Source: https://docs.snowplow.io/docs/sources/web-trackers/previous-versions/web-trackers-v3/tracking-events/consent-gdpr/
> Documentation index: https://docs.snowplow.io/llms.txt
> Status: Previous version
> Current version: https://docs.snowplow.io/docs/sources/web-trackers/

Track user consent preferences selection events using the Enhanced Consent plugin. Additionally, add a context entity containing GDPR details to every event using the [original Consent plugin](/docs/sources/web-trackers/previous-versions/web-trackers-v3/tracking-events/consent-gdpr/original/).

Enhanced consent events must be **manually tracked**. The GDPR context entity will be **automatically tracked** with all events if configured.

## Install plugin

**JavaScript (tag):**

| Tracker Distribution | Included |
| -------------------- | -------- |
| `sp.js`              | ❌        |
| `sp.lite.js`         | ❌        |

**Download:**

|                                             |                                                                                                                     |
| ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| Download from GitHub Releases (Recommended) | [Github Releases (plugins.umd.zip)](https://github.com/snowplow/snowplow-javascript-tracker/releases)               |
| Available on jsDelivr                       | [jsDelivr](https://cdn.jsdelivr.net/npm/@snowplow/browser-plugin-enhanced-consent@3/dist/index.umd.min.js) (latest) |
| Available on unpkg                          | [unpkg](https://unpkg.com/@snowplow/browser-plugin-enhanced-consent@3/dist/index.umd.min.js) (latest)               |

```javascript
window.snowplow(
    'addPlugin',
    'https://cdn.jsdelivr.net/npm/@snowplow/browser-plugin-enhanced-consent@3/dist/index.umd.min.js',
    ['snowplowEnhancedConsentTracking', 'EnhancedConsentPlugin']
);
```

**Browser (npm):**

- `npm install @snowplow/browser-plugin-enhanced-consent@3`
- `yarn add @snowplow/browser-plugin-enhanced-consent@3`
- `pnpm add @snowplow/browser-plugin-enhanced-consent@3`

```javascript
import { newTracker } from '@snowplow/browser-tracker';
import { EnhancedConsentPlugin } from '@snowplow/browser-plugin-enhanced-consent';

newTracker('sp1', '{{collector_url}}', {
   appId: 'my-app-id',
   plugins: [ EnhancedConsentPlugin() ],
});
```

***

> **Note:** The plugin is available since version 3.8 of the tracker.

## Events

| API                     | Used for:                                               |
| ----------------------- | ------------------------------------------------------- |
| `trackConsentAllow`     | Track an acceptance of user consent.                    |
| `trackConsentSelected`  | Track a specific selection of consented scopes.         |
| `trackConsentPending`   | Track the unconfirmed selection about user consent.     |
| `trackConsentImplicit`  | Track the implicit consent on user consent preferences. |
| `trackConsentDeny`      | Track a denial of user consent.                         |
| `trackConsentExpired`   | Track the expiration of a consent selection.            |
| `trackConsentWithdrawn` | Track the withdrawal of user consent.                   |
| `trackCmpVisible`       | Track the render time of a CMP banner.                  |

With the exception of the CMP visible event, these methods use the same [`consent_preferences`](https://github.com/snowplow/iglu-central/blob/master/schemas/com.snowplowanalytics.snowplow/consent_preferences/jsonschema/1-0-0) event schema. It has the following properties:

| Attribute          | Type       | Description                                                                                                                            | Required |
| ------------------ | ---------- | -------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| basisForProcessing | `string`   | GDPR lawful basis for data collection & processing.                                                                                    | ✅        |
| consentUrl         | `string`   | URI of the privacy policy related document.                                                                                            | ✅        |
| consentVersion     | `string`   | Version of the privacy policy related document.                                                                                        | ✅        |
| consentScopes      | `string[]` | The scopes allowed after the user finalized their selection of consent preferences. E.g \['analytics', 'functional', 'advertisement']. | ✅        |
| domainsApplied     | `string[]` | The domains for which this consent allows these preferences to persist to.                                                             | ✅        |
| gdprApplies        | `boolean`  | Determine if GDPR applies based on the user's geo-location.                                                                            | ✘        |

### Consent allow

To track the complete acceptance of a user consent prompt, you can use the `trackConsentAllow` method with the following attributes:

**JavaScript (tag):**

```js
window.snowplow("trackConsentAllow:{trackerName}", {
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

**Browser (npm):**

```js
import { trackConsentAllow } from "@snowplow/browser-plugin-enhanced-consent";

trackConsentAllow({
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

***

### Consent selected

To track the specific selection of scopes of a user consent preferences, you can use the `trackConsentSelected` method with the following attributes:

**JavaScript (tag):**

```js
window.snowplow("trackConsentSelected:{trackerName}", {
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

**Browser (npm):**

```js
import { trackConsentSelected } from "@snowplow/browser-plugin-enhanced-consent";

trackConsentSelected({
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

***

### Consent pending

Some consent management platform installations, allow the user to take website actions or/and navigating to other pages without accepting the consent prompt. To track the unconfirmed selection of user consent, you can use the `trackConsentPending` method with the following attributes:

**JavaScript (tag):**

```js
window.snowplow("trackConsentPending:{trackerName}", {
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

**Browser (npm):**

```js
import { trackConsentPending } from "@snowplow/browser-plugin-enhanced-consent";

trackConsentPending({
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

***

### Consent implicit

Some consent management platforms have a configuration which allows the setting of consent implicitly after a set of user interactions like clicks, scroll etc. To track the implicit selection of a user consent, you can use the `trackConsentImplicit` method with the following attributes:

**JavaScript (tag):**

```js
window.snowplow("trackConsentImplicit:{trackerName}", {
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

**Browser (npm):**

```js
import { trackConsentImplicit } from "@snowplow/browser-plugin-enhanced-consent";

trackConsentImplicit({
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

***

### Consent deny

To track the complete denial of a user consent, you can use the `trackConsentDeny` method with the following attributes:

**JavaScript (tag):**

```js
window.snowplow("trackConsentDeny:{trackerName}", {
    consentScopes: ["necessary"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

**Browser (npm):**

```js
import { trackConsentDeny } from "@snowplow/browser-plugin-enhanced-consent";

trackConsentDeny({
    consentScopes: ["necessary"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

***

### Consent expired

To track the expiration of a user consent, you can use the `trackConsentExpired` method with the following attributes:

**JavaScript (tag):**

```js
window.snowplow("trackConsentExpired:{trackerName}", {
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

**Browser (npm):**

```js
import { trackConsentExpired } from "@snowplow/browser-plugin-enhanced-consent";

trackConsentExpired({
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

***

### Consent withdrawn

To track the withdrawal of a user consent, you can use the `trackConsentWithdrawn` method with the following attributes:

**JavaScript (tag):**

```js
window.snowplow("trackConsentWithdrawn:{trackerName}", {
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

**Browser (npm):**

```js
import { trackConsentWithdrawn } from "@snowplow/browser-plugin-enhanced-consent";

trackConsentWithdrawn({
    consentScopes: ["necessary", "marketing", "personalization"],
    basisForProcessing: "consent",
    consentUrl: "https://www.example.com/",
    consentVersion: "1.0",
    domainsApplied: ["https://www.example.com/"],
    gdprApplies: true
});
```

***

### CMP visible

Consent management platform banners are an important part of a website’s first impression and performance. Snowplow provides a way to track what we call `elapsedTime`, which is the timestamp of the consent management platform banner becoming visible on the user’s screen.

**JavaScript (tag):**

```js
window.snowplow("trackCmpVisible:{trackerName}", {
    /* Using the performance.now API to retrieve the elapsed time from the page navigation. */
    elapsedTime: performance.now(),
});
```

**Browser (npm):**

```js
import { trackCmpVisible } from "@snowplow/browser-plugin-enhanced-consent";

trackCmpVisible({
    /* Using the performance.now API to retrieve the elapsed time from the page navigation. */
    elapsedTime: performance.now(),
});
```

***

The CMP visible event uses [this](https://github.com/snowplow/iglu-central/blob/master/schemas/com.snowplowanalytics.snowplow/cmp_visible/jsonschema/1-0-0) schema.
