Skip to content

Set Up Recommendation Tracking

⏱ 20 minutes intermediate

Why tracking is the foundation of recommendations

Section titled “Why tracking is the foundation of recommendations”

Content Recommendations learns what to suggest by observing what visitors read and how content relates to other content. Without tracking, the recommendation engine has no behavioral data and no content metadata to work with. The result is an empty or random set of suggestions.

Tracking collects two things: visitor behavior (which pages they view, how long they stay) and content properties (what each page is about). Together, these power the algorithms that match visitors to relevant content.

  1. Add the tracking script to your site
  2. Configure content properties for your pages
  3. Send page view events
  4. Verify data collection

The Content Recommendations tracking script must load on every page where you want to track behavior or display recommendations.

Add the tracking script
html
<!-- Place in the <head> of your site -->
<script>
(function(d, s) {
  var f = d.getElementsByTagName(s)[0],
      j = d.createElement(s);
  j.async = true;
  j.src = 'https://codegen.optimizer.net/YOUR_ACCOUNT_ID/loader.js';
  f.parentNode.insertBefore(j, f);
})(document, 'script');
</script>

Replace YOUR_ACCOUNT_ID with your Content Recommendations account identifier. You can find this in your Optimizely dashboard under Content Recommendations > Settings > Account.

Placement matters. Add the script to the <head> section so it loads before the page content renders. This ensures tracking captures the full page view and content properties are available when the recommendation engine processes the page.

Content properties tell the recommendation engine what each page is about. The engine uses these properties to build content profiles and calculate similarity between pages.

Every tracked page must send these properties:

PropertyDescriptionExample value
titleThe page title”How to Optimize Landing Pages”
typeContent category or type”blog-post”, “product-page”, “guide”
urlCanonical URLhttps://example.com/blog/optimize-landing-pages

Additional properties improve recommendation quality:

PropertyDescriptionExample value
categoriesTopic categories[“marketing”, “conversion”]
tagsContent tags[“landing-pages”, “optimization”, “CRO”]
authorContent author”Jane Smith”
publishDatePublication date”2026-03-15”
languageContent language”en”
imageFeatured image URLhttps://example.com/images/hero.jpg
Configure content properties
javascript
// Set content properties before sending the page view
window.optimizelyContentRecs = window.optimizelyContentRecs || [];

window.optimizelyContentRecs.push({
action: 'setContentProperties',
properties: {
  title: document.title,
  type: 'blog-post',
  url: window.location.href,
  categories: ['marketing', 'conversion'],
  tags: ['landing-pages', 'optimization'],
  author: 'Jane Smith',
  publishDate: '2026-03-15',
  language: 'en',
  image: 'https://example.com/images/hero.jpg',
},
});

If your site renders content from CMS, extract properties from the rendered page data rather than hardcoding them:

Dynamic content properties from CMS data
javascript
// Example: extract properties from page metadata
function getContentProperties() {
const meta = (name) => {
  const el = document.querySelector(`meta[name="${name}"]`);
  return el ? el.content : null;
};

return {
  title: document.title,
  type: meta('content-type') || 'page',
  url: document.querySelector('link[rel="canonical"]')?.href
       || window.location.href,
  categories: meta('categories')?.split(',').map(c => c.trim()) || [],
  tags: meta('keywords')?.split(',').map(t => t.trim()) || [],
  author: meta('author'),
  publishDate: meta('publish-date'),
  language: document.documentElement.lang || 'en',
};
}

window.optimizelyContentRecs = window.optimizelyContentRecs || [];
window.optimizelyContentRecs.push({
action: 'setContentProperties',
properties: getContentProperties(),
});

After setting content properties, send a page view event to record the visit.

Send a page view event
javascript
window.optimizelyContentRecs.push({
action: 'trackPageView',
});

For single-page applications (SPAs) that do not reload the page on navigation, send a page view event on each route change:

Track page views in a SPA
javascript
// Call on each route change
function trackSpaPageView(contentProperties) {
window.optimizelyContentRecs = window.optimizelyContentRecs || [];

// Update content properties for the new page
window.optimizelyContentRecs.push({
  action: 'setContentProperties',
  properties: contentProperties,
});

// Send the page view
window.optimizelyContentRecs.push({
  action: 'trackPageView',
});
}

After deploying tracking code, confirm that data is flowing correctly.

  1. Open your site in a browser with developer tools open
  2. Navigate to a tracked page
  3. In the Network tab, filter for requests to optimizer.net
  4. Verify that you see a tracking request with a 200 response
  5. Inspect the request payload to confirm content properties are present
  1. Navigate to Content Recommendations > Content
  2. Wait 15-30 minutes for initial data processing
  3. Verify that tracked pages appear in the content list
  4. Click a page to confirm its properties match what you configured
IssueCauseFix
No tracking requests in Network tabScript not loading or blocked by ad blockerVerify script placement; test with ad blocker disabled
Tracking request returns 403Invalid account IDCheck the account ID in the loader URL
Pages not appearing in dashboardData processing delay or missing required propertiesWait 30 minutes; verify title, type, and URL are set
Properties showing as emptyProperties set after page view eventSet content properties before calling trackPageView
SPA page views not trackingPage view not fired on route changeAdd route change listener that calls trackPageView