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.csfile. -
If your rendering host is located behind a proxy server, in the
ConfigureServicesmethod, configureForwardedHeadersOptionsfor 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
WithTrackingoption to theAddSitecoreRenderingEngineservice 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
AddSitecoreVisitorIdentificationservice to enable support for robot detection:RequestResponsepublic void ConfigureServices(IServiceCollection services) { services.AddSitecoreVisitorIdentification(options => { options.SitecoreInstanceUri = new Uri("https://<sitecore instance>"); }); }The
SitecoreInstanceUrimust point to the Sitecore instance hosting the Sitecore Layout Service. -
In the
Configuremethod, 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.csfile. -
In your layout, add the
sc-visitor-identificationtag 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.VisitorIdentificationtag 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.