Enable rendering host tracking, analytics, and personalization
If you integrated your ASP.NET Core app before September 2024, it's using the legacy ASP.NET Core Rendering SDK, version 22 or earlier. This SDK is no longer receiving updates, so we recommend that you upgrade to the latest version of the new ASP.NET Core SDK.
To enable rendering host tracking, analytics, and personalization, you must configure the rendering host requests to the Sitecore Layout Service to include the visitor IP address as an X-Forwarded-For HTTP header.
If you are using proxy servers or load balancers, see the Microsoft documentation on configuring ASP.NET Core to work with proxy servers and load balancers for additional instructions.
To enable tracking and visitor identification:
-
Make sure that the Sitecore instance analytics cookie domain matches the rendering host domain, or is set to empty.
-
In Visual Studio, in your rendering host project, double-click the
Startup.cs
file. -
If your rendering host is located behind a proxy server, in the
ConfigureServices
method, configureForwardedHeadersOptions
for visitor IP address forwarding:RequestResponsepublic void ConfigureServices(IServiceCollection services) { services.Configure<ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor; }); }
WarningBeware of spoofing vulnerabilities when forwarding the visitor IP address. The express project has a debate about handling the X-Forwarded-For header, and the Gorouter project discusses spoofing the visitor IP address.
-
Add the
WithTracking
option to theAddSitecoreRenderingEngine
service to enable tracking in Sitecore Rendering Engine:RequestResponsepublic void ConfigureServices(IServiceCollection services) { services.AddSitecoreRenderingEngine(options => { //Register your components here using options parameter }).WithTracking(); }
-
Add the
AddSitecoreVisitorIdentification
service to enable support for robot detection:RequestResponsepublic void ConfigureServices(IServiceCollection services) { services.AddSitecoreVisitorIdentification(options => { options.SitecoreInstanceUri = new Uri("https://<sitecore instance>"); }); }
The
SitecoreInstanceUri
must point to the Sitecore instance hosting the Sitecore Layout Service. -
In the
Configure
method, add the following:RequestResponsepublic void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseForwardedHeaders(); // enables visitor IP address forwarding app.UseSitecoreVisitorIdentification(); // enables robot detection });
-
Save the
Startup.cs
file. -
In your layout, add the
sc-visitor-identification
tag helper to avoid the session and its requests being classified as a robot visitor:RequestResponse<!DOCTYPE html> <html> <head> <sc-visitor-identification /> </head> <body> </body> </html>
This tag helper renders a JavaScript script tag to include
/layouts/system/VisitorIdentification.js
, which performs the necessary contact identification requests. -
In your views, add the
Sitecore.AspNet.Tracking.VisitorIdentification
tag helper:RequestResponse@addTagHelper *, Sitecore.AspNet.Tracking.VisitorIdentification
Refer to the Getting Started template for a working example of the various APIs available in the Sitecore Rendering Engine SDK.