Targeting
Targeting allows you to pass key-value pairs to your ad server to help with ad selection and optimization. Moli provides several ways to set targeting values that will be sent with your ad requests.
Overview
Targeting key-values are used by ad servers and demand-side platforms (DSPs) to:
- Match ads to specific audience segments
- Optimize ad performance based on page context
- Enable programmatic buying with specific criteria
- Improve ad relevance and user experience
Setting Targeting Values
Global Targeting
Set targeting values that apply to all ad slots on the page:
// Set targeting before requesting ads
window.moli.setTargeting('key', 'value');
window.moli.setTargeting('category', 'sports');
window.moli.setTargeting('position', 'sidebar');
// Request ads with targeting applied
window.moli.requestAds();
Per-Page Targeting
For single-page applications, set targeting values for each page:
// Set page-specific targeting
window.moli.setTargeting('page_type', 'article');
window.moli.setTargeting('article_category', 'technology');
window.moli.setTargeting('author', 'john_doe');
// Request ads for the new page
window.moli.requestAds();
Ad Slot Specific Targeting
Set targeting values for specific ad slots:
const slot: Moli.AdSlot = {
domId: 'content_1',
adUnitPath: '/1234/content_1',
sizes: [[300, 250], [728, 90]],
position: 'in-page',
behaviour: { loaded: 'eager' },
gpt: {
targeting: {
'slot_position': 'content',
'content_type': 'article'
}
}
};
Targeting Configuration
In MoliConfig
You can set default targeting values in your configuration:
const moliConfig: Moli.MoliConfig = {
slots: [ /* ... */ ],
gpt: {
targeting: {
'site_section': 'news',
'content_language': 'en'
}
}
};
Dynamic Targeting
Use functions to set targeting values dynamically:
const slot: Moli.AdSlot = {
domId: 'content_1',
adUnitPath: '/1234/content_1',
sizes: [[300, 250]],
position: 'in-page',
behaviour: { loaded: 'eager' },
gpt: {
targeting: () => ({
'page_url': window.location.href,
'user_agent': navigator.userAgent,
'timestamp': Date.now().toString()
})
}
};
Common Targeting Keys
Content Targeting
content_category- Article or page categorycontent_type- Type of content (article, video, gallery)content_author- Content authorcontent_tags- Content tags or keywords
User Targeting
user_type- User segment (new, returning, premium)user_location- Geographic locationuser_device- Device type (mobile, desktop, tablet)
Page Targeting
page_type- Page type (home, article, category)page_section- Site sectionpage_position- Ad position on page
Custom Targeting
custom_key- Any custom key-value paircampaign_id- Campaign identifierexperiment_group- A/B test group
Best Practices
Use Descriptive Keys
// Good
window.moli.setTargeting('article_category', 'technology');
// Avoid
window.moli.setTargeting('cat', 'tech');
Consistent Naming
// Use consistent naming across your site
window.moli.setTargeting('content_category', 'sports'); // ✅
window.moli.setTargeting('category', 'sports'); // ❌ Inconsistent
Avoid Sensitive Data
// Don't include personally identifiable information
window.moli.setTargeting('user_id', '12345'); // ❌ Avoid
window.moli.setTargeting('user_segment', 'premium'); // ✅ Safe
Clear Values
// Use clear, standardized values
window.moli.setTargeting('device_type', 'mobile'); // ✅ Clear
window.moli.setTargeting('device_type', 'm'); // ❌ Unclear
Integration with Prebid
When using Prebid.js, targeting values can be passed to bidders:
const slot: Moli.AdSlot = {
domId: 'content_1',
adUnitPath: '/1234/content_1',
sizes: [[300, 250]],
position: 'in-page',
behaviour: { loaded: 'eager' },
prebid: {
adUnit: {
mediaTypes: {
banner: { sizes: [[300, 250]] }
},
bids: [
{
bidder: 'appNexus',
params: {
placementId: 123,
keywords: {
category: 'sports',
position: 'content'
}
}
}
]
}
}
};
Debugging Targeting
Use the debugging console to verify targeting values:
// Enable debugging
localStorage.setItem('moliDebug', 'true');
// Check targeting in browser console
API Reference
Available Methods
setTargeting(key, value)- Set a targeting key-value pair for all ad slots
For detailed API documentation, see the MoliTag API reference.
Example Usage
// Set targeting
window.moli.setTargeting('category', 'sports');