Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions content/collections/experiment-sdks/en/experiment-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
| `exposureTrackingProvider` | Implement and configure this interface to track exposure events through the experiment SDK, either automatically or explicitly. | `null` |
| `instanceName` | Custom instance name for experiment SDK instance. **The value of this field is case-sensitive.** | `null` |
| `initialFlags` | A JSON string representing an initial array of flag configurations to use for local evaluation. | `undefined` |
| `consentOptions` | Configure cookie consent management. Set `status` to `ConsentStatus.GRANTED`, `ConsentStatus.PENDING`, or `ConsentStatus.REJECTED`. Go to [Consent Management](#consent-management) for more details. | `{ status: ConsentStatus.GRANTED }` |
| `httpClient` | (Advanced) Use your own HTTP client implementation to handle network requests made by the SDK. | Default HTTP client |

{{/partial:collapse}}
Expand Down Expand Up @@ -361,6 +362,75 @@

{{/partial:collapse}}

## Consent Management

Check warning on line 365 in content/collections/experiment-sdks/en/experiment-javascript.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Microsoft.Headings] 'Consent Management' should use sentence-style capitalization. Raw Output: {"message": "[Microsoft.Headings] 'Consent Management' should use sentence-style capitalization.", "location": {"path": "content/collections/experiment-sdks/en/experiment-javascript.md", "range": {"start": {"line": 365, "column": 4}}}, "severity": "INFO"}

The Experiment JavaScript SDK supports cookie consent management. Consent status controls data storage persistence and exposure tracking.

### Consent status values

The SDK supports three consent status values:

- **GRANTED (1)**: User has granted consent. Uses browser localStorage and sessionStorage for persistence and tracks exposures immediately.

Check failure on line 373 in content/collections/experiment-sdks/en/experiment-javascript.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'sessionStorage'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'sessionStorage'?", "location": {"path": "content/collections/experiment-sdks/en/experiment-javascript.md", "range": {"start": {"line": 373, "column": 76}}}, "severity": "ERROR"}

Check failure on line 373 in content/collections/experiment-sdks/en/experiment-javascript.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'localStorage'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'localStorage'?", "location": {"path": "content/collections/experiment-sdks/en/experiment-javascript.md", "range": {"start": {"line": 373, "column": 59}}}, "severity": "ERROR"}
- **PENDING (2)**: Waiting for user consent decision. Stores data in-memory only and queues exposures without tracking them. When consent changes to GRANTED, persists in-memory data to browser storage and fires all queued exposures.
- **REJECTED (0)**: User has rejected consent. Doesn't initialize, store data, or track exposures. If transitioning from PENDING, deletes all queued exposures.

### Configure consent on initialization

Set the initial consent status when you initialize the SDK:

```js
import { Experiment, ConsentStatus } from '@amplitude/experiment-js-client';

const experiment = Experiment.initialize('DEPLOYMENT_KEY', {
consentOptions: {
status: ConsentStatus.PENDING
}
});
```

If you set consent status to REJECTED, the SDK doesn't initialize.

### Update consent status

Update the consent status after initialization when users interact with your consent banner:

```js
import { ConsentStatus } from '@amplitude/experiment-js-client';

// When user grants consent
experiment.setConsentStatus(ConsentStatus.GRANTED);

// When user rejects consent
experiment.setConsentStatus(ConsentStatus.REJECTED);
```

When you change consent from PENDING to GRANTED, the SDK persists in-memory data to browser storage and fires all queued exposures. When you change to REJECTED, the SDK stops storing data and deletes any queued exposures.

### Example integration with consent banner

```js
import { Experiment, ConsentStatus } from '@amplitude/experiment-js-client';

// Initialize with PENDING status
const experiment = Experiment.initializeWithAmplitudeAnalytics('DEPLOYMENT_KEY', {
consentOptions: {
status: ConsentStatus.PENDING
}
});

// Start fetching variants
await experiment.fetch();

// Update when user responds to consent banner
window.addEventListener('consentGranted', () => {
experiment.setConsentStatus(ConsentStatus.GRANTED);
});

window.addEventListener('consentRejected', () => {
experiment.setConsentStatus(ConsentStatus.REJECTED);
});
```

## Fetch

Fetches variants for a [user](/docs/feature-experiment/data-model#users) and store the results in the client for fast access. This function [remote evaluates](/docs/feature-experiment/remote-evaluation) the user for flags associated with the deployment used to initialize the SDK client.
Expand Down
Loading