Personalization does not work properly on the first visitor request

While using personalization or conditional renderings with rules based on the geographical information of the visitor (such as where the country is equal to specific country), the personalized content might not been presented to the visitor correctly on the first request.

Sitecore is designed not to wait until the GeoIP information has been resolved from a GeoIP lookup provider. This is implemented to keep response times for the website visitors low in situations when the GeoIP resolution process is taking a long time.

To make Sitecore wait for the GeoIP information even on the first request from a visitor, proceed as follows:

Step 1: Create a custom processor for the createVisits pipeline:

namespace Sitecore.Module.Foundation.Personalization.Pipelines.Processor
    {
         using Sitecore.Analytics.Pipelines.CreateVisits;
         using Sitecore.Configuration;
         using Sitecore.Diagnostics;
         using System;
    
          public class UpdateGeoIpData : CreateVisitProcessor
           {
             public override void Process(CreateVisitArgs args)
              {
                Assert.ArgumentNotNull(args, "args");
                Assert.ArgumentNotNull(args, "args");
                int intSetting = Settings.GetIntSetting("Analytics.PerformLookup.CreateVisitInterval", 5); // retrieve the delay value from the setting.
                args.Interaction.UpdateGeoIpData(TimeSpan.FromSeconds((double) intSetting)); // wait for the time specified in the Analytics.PerformLookup.CreateVisitInterval setting.
               }
            }
     } 

Step 2: Create a Patch file to override the existing processor i.e. ‘Sitecore.Analytics.Pipelines.CreateVisits.UpdateGeoIpData

<configuration xmlns:patch=http://www.sitecore.net/xmlconfig/ xmlns:set=http://www.sitecore.net/xmlconfig/set/ xmlns:role=http://www.sitecore.net/xmlconfig/role/>
  <sitecore role:require="Standalone or ContentDelivery or ContentManagement">
    <pipelines>
      <createVisit>
        <processor patch:instead="processor[@type='Sitecore.Analytics.Pipelines.CreateVisits.UpdateGeoIpData']" type="Sitecore.Module.Foundation.Personalization.Processor.UpdateGeoIpData, Sitecore.Module.Foundation.Personalization" />
      </createVisit>
    </pipelines>
  </sitecore>
</configuration>

The approach overrides the logic of the createVisit pipeline and makes Sitecore wait a certain period of time for the GeoIp information to be resolved. The delay time is configured using the Analytics.PerformLookup.CreateVisitInterval setting. The setting defines the maximum delay to resolve the geographical data from the lookup provider allowed during the first page-request of a new visit.