Episerver, now rebranded as Optimizely, is a powerful .NET-based content management and digital commerce platform. This guide covers integrating analytics and marketing tools with both Episerver CMS and Commerce products.
About Episerver/Optimizely
Note: Episerver rebranded to Optimizely in 2020, but many developers still refer to it as Episerver. This documentation uses both names interchangeably.
Platform Overview
- Technology Stack: Built on .NET (ASP.NET Core for CMS 12+)
- Products: Episerver CMS and Episerver Commerce
- Architecture: Block and page type architecture with Razor view templates
- Multi-tenancy: Built-in multi-site and multi-language support
- Headless Options: Content Delivery API for headless implementations
Key Features for Analytics Integration
- Client Resources System: Centralized script and stylesheet management
- Razor Templates: Server-side rendering with .NET
- Content Types: Blocks, pages, and commerce-specific content
- Built-in Tracking: Episerver Tracking for analytics
- Optimizely Integration: Native integration with Optimizely Web Experimentation
Available Integration Guides
Google Analytics 4
- GA4 Setup on Episerver - Initial configuration and installation
- Event Tracking - Custom event implementation
- Ecommerce Tracking - Episerver Commerce integration
Google Tag Manager
- GTM Setup - Container installation and configuration
- Data Layer Implementation - Data layer structure for Episerver
Meta Pixel
- Meta Pixel Setup - Base pixel installation
- Event Tracking - Custom and standard events
Integration Approaches
1. Template-Based Integration (Recommended)
Add tracking code directly to Razor templates for maximum control and server-side data access.
Pros:
- Access to server-side data (content properties, user info, commerce data)
- Full control over rendering logic
- Strong typing with C# models
- Easy to maintain and version control
Example:
@model MyPageType
<script>
// Access server-side data
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'pageType': '@Model.ContentType.Name',
'pageId': '@Model.ContentLink.ID',
'language': '@Model.Language.Name'
});
</script>
2. Client Resources
Use Episerver's Client Resources system to register scripts globally or per-site.
Pros:
- Centralized script management
- Automatic versioning and bundling
- Dependency management
Example:
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ClientResourceInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
context.Locate.Advanced.GetInstance<IClientResourceService>()
.RegisterScript("analytics", "/Scripts/analytics.js");
}
}
3. Blocks for Tracking Scripts
Create reusable blocks for tracking code that editors can add to pages.
Pros:
- Editor-friendly
- Reusable across pages
- Can be added to shared layouts
4. Initialization Modules
Use initialization modules to inject tracking code programmatically.
Pros:
- Runs on application startup
- Can add scripts conditionally
- Integration with dependency injection
Best Practices
Performance Considerations
- Async Loading: Always load tracking scripts asynchronously
- Script Placement: Add scripts at the end of
<body>when possible - Bundling: Use Episerver's bundling for custom tracking scripts
- CDN: Leverage Episerver's CDN support for static resources
Content Delivery API (Headless)
For headless implementations:
- Return tracking data via API responses
- Implement tracking client-side in your frontend framework
- Use content properties to store tracking IDs
- Consider server-side tracking for sensitive data
Multi-site Support
When managing multiple sites:
- Use site-specific configuration files or database settings
- Access current site via
ISiteDefinitionRepository - Store tracking IDs as site-level settings
- Create reusable components that adapt to current site
var currentSite = _siteDefinitionRepository.Get(SiteDefinition.Current.Id);
var gaTrackingId = currentSite.GetProperty("GATrackingId")?.ToString();
Multi-language Support
- Track language in data layer:
@Model.Language.Name - Use content URLs that include language:
@Url.ContentUrl(Model.ContentLink) - Track language switches as events
- Consider language-specific views and conversion goals
Commerce-Specific Considerations
For Episerver Commerce implementations:
- Product Data: Access rich product information from catalog
- Cart Data: Track cart modifications and abandonments
- Order Data: Implement purchase tracking with full order details
- User Segments: Integrate customer segmentation data
- Promotions: Track promotion usage and effectiveness
Security and Privacy
GDPR Compliance
- Consent Management: Implement cookie consent before loading trackers
- Data Minimization: Only collect necessary data
- User Rights: Provide mechanisms for data deletion
- Privacy Policy: Link to privacy policy in tracking implementations
Content Security Policy (CSP)
If using CSP headers:
- Allow tracking domains in
script-src - Allow connection to analytics endpoints in
connect-src - Consider using nonces for inline scripts
- Test CSP rules in report-only mode first
Testing Your Integration
- Local Development: Test in CMS edit mode and preview mode
- Browser DevTools: Verify requests in Network tab
- Tag Assistants: Use Google Tag Assistant or Meta Pixel Helper
- Real-Time Reports: Monitor real-time analytics dashboards
- Multi-site Testing: Test each site configuration separately
- Language Testing: Verify tracking across all languages
Common Challenges
- Edit Mode vs. View Mode: Prevent tracking in edit mode
- Preview Mode: Decide whether to track preview traffic
- Scheduled Content: Handle content that's not yet published
- Personalization: Track personalized content variations
- A/B Testing: Integrate with Optimizely Web Experimentation
Support Resources
Next Steps
Choose an integration guide based on your analytics platform:
- Start with GTM Setup for flexible tag management
- Or go directly to GA4 Setup for Google Analytics
- Add Meta Pixel for Facebook advertising
For issues, see our Troubleshooting Guide.