Optimizely Web (snippet-based) patterns
This skill covers Optimizely Web (snippet-based) patterns for client-side experimentation.
<!-- Add before closing </head> tag -->
<script src="https://cdn.optimizely.com/js/PROJECT_ID.js"></script>
<script async src="https://cdn.optimizely.com/js/PROJECT_ID.js"></script>
<style>
/* Hide content until Optimizely activates */
.optimizely-pending {
opacity: 0 !important;
}
</style>
<script>
document.documentElement.classList.add('optimizely-pending');
window.optimizely = window.optimizely || [];
window.optimizely.push({
type: 'addListener',
filter: { type: 'lifecycle', name: 'activated' },
handler: function() {
document.documentElement.classList.remove('optimizely-pending');
}
});
// Fallback timeout
setTimeout(function() {
document.documentElement.classList.remove('optimizely-pending');
}, 3000);
</script>
window.optimizely = window.optimizely || [];
window.optimizely.push(['activate', 'experiment_id']);
window.optimizely.push({
type: 'event',
eventName: 'purchase_completed',
tags: {
revenue: 9900,
currency: 'USD'
}
});
var state = window.optimizely.get('state');
var experimentId = 'EXPERIMENT_ID';
var variationId = state.getVariationMap()[experimentId];
var state = window.optimizely.get('state');
var isEnabled = state.getDecisionString({
campaignId: 'CAMPAIGN_ID',
experimentId: 'EXPERIMENT_ID'
});
// Custom activation for SPA
window.optimizely.push({
type: 'activate'
});
// After route change
function onRouteChange(newPath) {
window.optimizely.push({
type: 'page',
pageName: newPath
});
}
window.optimizely.push({
type: 'user',
attributes: {
membershipTier: 'premium',
country: 'US',
isLoggedIn: true
}
});