Enable rendering host tracking, analytics, and personalization

Current version: 20.x

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:

  1. Make sure that the Sitecore instance analytics cookie domain matches the rendering host domain, or is set to empty.

  2. In Visual Studio, in your rendering host project, double-click the Startup.cs file.

  3. If your rendering host is located behind a proxy server, in the ConfigureServices method, configure ForwardedHeadersOptions for visitor IP address forwarding:

    RequestResponse
    public void ConfigureServices(IServiceCollection services)
    {
      services.Configure<ForwardedHeadersOptions>(options =>
        {
          options.ForwardedHeaders = ForwardedHeaders.XForwardedFor;
        });
    }
    Warning
  4. Add the WithTracking option to the AddSitecoreRenderingEngine service to enable tracking in Sitecore Rendering Engine:

    RequestResponse
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddSitecoreRenderingEngine(options =>
        {
          //Register your components here using options parameter
        }).WithTracking();
    }
  5. Add the AddSitecoreVisitorIdentification service to enable support for robot detection:

    RequestResponse
    public 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.

  6. In the Configure method, add the following:

    RequestResponse
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      app.UseForwardedHeaders();                 // enables visitor IP address forwarding
      app.UseSitecoreVisitorIdentification();     // enables robot detection
    });
  7. Save the Startup.cs file.

  8. 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.

  9. In your views, add the Sitecore.AspNet.Tracking.VisitorIdentification tag helper:

    RequestResponse
    @addTagHelper *, Sitecore.AspNet.Tracking.VisitorIdentification
Tip

Refer to the Getting Started template for a working example of the various APIs available in the Sitecore Rendering Engine SDK.

Do you have some feedback for us?

If you have suggestions for improving this article,