Configure Recommendation Widgets
Why widgets matter for recommendations
Section titled βWhy widgets matter for recommendationsβA recommendation model running in the background produces nothing visible to visitors. Widgets are the delivery mechanism β they render recommended content on your pages where visitors can see and interact with the suggestions. The widget placement, style, and behavior directly affect whether visitors engage with recommendations.
What you will do
Section titled βWhat you will doβ- Choose a widget type for your use case
- Add a widget to a page
- Configure display options
- Handle empty and fallback states
- Style the widget to match your design
Choose a widget type
Section titled βChoose a widget typeβContent Recommendations supports several widget formats. Choose based on placement and content type.
| Widget type | Layout | Best for |
|---|---|---|
| Carousel | Horizontally scrollable cards | Article sidebars, homepage sections |
| Grid | Fixed grid of cards (2x2, 3x1, etc.) | Blog footers, category pages |
| List | Vertical list with thumbnails | Sidebar widgets, mobile layouts |
| Inline | Single recommendation embedded in text | Mid-article suggestions |
| Overlay | Modal or slide-in panel | Exit intent, engagement triggers |
Add a widget to a page
Section titled βAdd a widget to a pageβJavaScript embed method
Section titled βJavaScript embed methodβAdd the widget code where you want recommendations to appear.
<!-- Container for the widget -->
<div id="content-recommendations-widget"></div>
<script>
window.optimizelyContentRecs = window.optimizelyContentRecs || [];
window.optimizelyContentRecs.push({
action: 'renderWidget',
selector: '#content-recommendations-widget',
modelId: 'YOUR_MODEL_ID',
widgetType: 'grid',
options: {
count: 4,
showImage: true,
showDescription: true,
maxDescriptionLength: 120,
openInNewTab: false,
},
});
</script> Replace YOUR_MODEL_ID with the ID from your recommendation model (found in Content Recommendations > Models > [Your Model] > Settings).
CMS block method
Section titled βCMS block methodβFor CMS PaaS, the recommendation widget can be implemented as a custom block type that wraps the JavaScript embed code. This gives editors control over placement while developers control the rendering logic.
Configure display options
Section titled βConfigure display optionsβContent card settings
Section titled βContent card settingsβControl what information each recommended item displays:
| Option | Type | Default | Description |
|---|---|---|---|
count | integer | 4 | Number of recommendations to show |
showImage | boolean | true | Display the featured image |
showDescription | boolean | true | Display the content description |
maxDescriptionLength | integer | 150 | Truncate description at this character count |
showCategory | boolean | false | Display the content category label |
showDate | boolean | false | Display the publication date |
openInNewTab | boolean | false | Open recommendation links in a new browser tab |
imageAspectRatio | string | β16:9β | Image crop ratio: β16:9β, β4:3β, β1:1β |
Filtering options
Section titled βFiltering optionsβNarrow what the widget can display beyond the modelβs own rules:
window.optimizelyContentRecs.push({
action: 'renderWidget',
selector: '#sidebar-recommendations',
modelId: 'YOUR_MODEL_ID',
widgetType: 'list',
options: {
count: 5,
showImage: true,
showDescription: false,
},
filters: {
excludeCurrentPage: true,
contentTypes: ['blog-post', 'guide'],
categories: ['marketing'],
maxAge: 90, // only content published within last 90 days
},
}); | Filter | Type | Description |
|---|---|---|
excludeCurrentPage | boolean | Exclude the page the visitor is currently viewing |
contentTypes | array | Only show specific content types |
categories | array | Only show content in these categories |
maxAge | integer | Maximum content age in days |
language | string | Only show content in this language |
Handle empty and fallback states
Section titled βHandle empty and fallback statesβSometimes the recommendation engine cannot fill all slots β a new visitor with no history, insufficient data, or aggressive filters. Configure what happens in these cases.
window.optimizelyContentRecs.push({
action: 'renderWidget',
selector: '#recommendations',
modelId: 'YOUR_MODEL_ID',
widgetType: 'grid',
options: { count: 4 },
fallback: {
strategy: 'trending', // 'trending', 'editorial', or 'hide'
trendingWindow: '7d', // for trending strategy: '24h', '7d', '30d'
editorialItems: [ // for editorial strategy
{ url: '/blog/getting-started/', title: 'Getting Started Guide' },
{ url: '/blog/best-practices/', title: 'Best Practices' },
],
minItems: 2, // hide widget if fewer than this many items
},
}); | Strategy | Behavior |
|---|---|
| trending | Fill empty slots with trending content from the same catalog |
| editorial | Fill empty slots with a manually curated list |
| hide | Hide the widget entirely if not enough recommendations |
Style the widget
Section titled βStyle the widgetβWidgets render with default CSS classes you can override. The widget generates markup with these classes:
| Class | Element |
|---|---|
.opti-recs-widget | Widget container |
.opti-recs-item | Individual recommendation card |
.opti-recs-image | Card image container |
.opti-recs-title | Card title |
.opti-recs-description | Card description text |
.opti-recs-category | Category label |
.opti-recs-date | Publication date |
To customize styles, add CSS rules targeting these classes. The widget does not use shadow DOM, so your site styles apply normally.
For complete visual control, use the templateOverride option:
window.optimizelyContentRecs.push({
action: 'renderWidget',
selector: '#custom-recommendations',
modelId: 'YOUR_MODEL_ID',
widgetType: 'grid',
options: { count: 3 },
templateOverride: function(items) {
return items.map(item =>
'<a href="' + item.url + '" class="my-rec-card">' +
'<img src="' + item.image + '" alt="' + item.title + '" />' +
'<h3>' + item.title + '</h3>' +
'</a>'
).join('');
},
}); Verify widget behavior
Section titled βVerify widget behaviorβ- Deploy the widget to a staging environment
- Browse several pages to build a visitor profile
- Navigate to a page with the widget and verify recommendations appear
- Check that recommendations change based on browsing context
- Test the fallback by opening the page in an incognito window (no visitor history)
- Verify that the widget hides or shows fallback content as configured