Optimizely + DataGrail Consent
Optimizely Web Experimentation loads synchronously in the page <head> and sets cookies immediately by default. This guide shows you how to gate measurement behind consent while keeping experiments running to avoid page flicker.
Recommended Approach
The best pattern for Optimizely + DataGrail is holdEvents + sendEvents:
- Hold events before the Optimizely snippet loads
- Experiments run immediately (no flicker)
- Measurement data queues in localStorage
- On consent, flush the queue to Optimizely
This approach balances user experience (no flicker) with compliance (measurement gated behind consent).
Implementation
Add this code before your Optimizely snippet:
<script>
// Initialize Optimizely queue and hold events
window["optimizely"] = window["optimizely"] || [];
window["optimizely"].push({ "type": "holdEvents" });
</script>
<!-- Your Optimizely snippet goes here -->
<script src="https://cdn.optimizely.com/js/YOUR_PROJECT_ID.js"></script>
Then register DataGrail callbacks to send events when Performance consent is granted:
<script>
window.dgEvent = window.dgEvent || [];
function handleOptimizelyConsent(preferences) {
if (window.DG_BANNER_API.categoryEnabled('performance') && window.optimizely) {
window.optimizely.push({ "type": "sendEvents" });
}
}
window.dgEvent.push({
event: "initial_preference_callback",
params: handleOptimizelyConsent
});
window.dgEvent.push({
event: "preference_callback",
params: handleOptimizelyConsent
});
</script>
How The Callbacks Work
The initial_preference_callback fires on every page load with any existing consent preferences — this handles returning visitors who already granted consent. The preference_callback fires when a visitor makes a new consent choice via the banner.
By registering both callbacks, Optimizely events are sent immediately for returning visitors with cached consent, and on first interaction for new visitors. See the Callbacks documentation for more details.
Cookie and Storage Patterns
Add these patterns as cookie rules in your DataGrail Consent dashboard under Cookie Management. For each pattern, select Add Rule, enter the match criteria, set the Category to Performance, and configure the appropriate retention:
| Match Criteria | Category | Retention | Purpose |
|---|---|---|---|
optimizelyEndUserId | Performance | 6 months | Unique visitor ID for bucketing |
optimizelySession | Performance | Session | Session tracking |
optimizelyRedirectData | Performance | Session | Redirect experiment data (5s duration) |
The holdEvents approach does set the optimizelyEndUserId cookie before consent. This cookie is necessary for consistent variant assignment. If your compliance requirements prohibit any cookies before consent, you'll need to fully block the Optimizely snippet via DataGrail's script blocking (which requires a page reload after consent) or consider server-side testing.
Integration Comparison
Here's how different integration approaches compare:
| Approach | Flicker? | Cookies Before Consent? | Measurement | User Experience |
|---|---|---|---|---|
| holdEvents + sendEvents (recommended) | No | Yes (optimizelyEndUserId) | Queued until consent | Best — smooth experience |
| Full blocking (disable API) | Yes | No | After consent + reload | Poor — visible flicker + reload |
| No consent gating | No | Yes (all) | Immediate | Best UX, legally risky |
| Server-side testing | No | No | Server-controlled | Best compliance, requires dev work |
Troubleshooting
Events aren't being sent after consent is granted
Check that:
- Your
preference_callbackis firing (add aconsole.logto verify) - The Performance category is enabled in the user's consent preferences
window.optimizelyexists (Optimizely snippet loaded successfully)- You're using
sendEvents(notactivate, which is for a different use case)
Page still flickers when Optimizely loads
Flicker occurs when Optimizely loads too late. Ensure:
- You're using the
holdEventsapproach (experiments run immediately) - Optimizely snippet is in
<head>, not loaded via GTM - You're not using full blocking (disable/optOut API)
Returning visitors see flicker on first page load
If returning visitors have cached consent, check:
- You've registered an
initial_preference_callback(fires on page load with cached consent) - The Optimizely snippet loads in the
<head>before the callback fires
Related Resources
- Optimizely Consent Documentation
- DataGrail Consent Banner Documentation
- For questions, contact support@datagrail.io
Disclaimer: The information contained in this message does not constitute as legal advice. We would advise seeking professional counsel before acting on or interpreting any material.