Sitecore SXA Personalization based on current environment

How many times you need a different content on different environment? For example, GTM script with different params on PROD and non-PROD environment? Why not leverage SXA Multisite Environment variable and Sitecore rules?

Perso

There are 3 steps.

First. In your web.config define local environment variable

<add key=”localenv:define” value=”local” /> i.e. different value for each environment : local, dev, qa, etc.

Second.  Set SXA setting XA.Foundation.Multisite.Environment on each environment

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:localenv="http://www.sitecore.net/xmlconfig/localenv/">
<sitecore >
<settings>
<setting name="XA.Foundation.Multisite.Environment" value="LOCAL" />
<setting name="XA.Foundation.Multisite.Environment" role:require="ContentManagement" localenv:require="development">
<patch:attribute name="value">DEV-CM</patch:attribute >
</setting>
<setting name="XA.Foundation.Multisite.Environment" role:require="ContentDelivery" localenv:require="development">
<patch:attribute name="value">DEV-CD</patch:attribute>
</setting>
<setting name="XA.Foundation.Multisite.Environment" role:require="ContentManagement" localenv:require="qa">
<patch:attribute name="value">QA-CM</patch:attribute>
</setting>
<setting name="XA.Foundation.Multisite.Environment" role:require="ContentDelivery" localenv:require="qa">
<patch:attribute name="value">QA-CD</patch:attribute>
</setting>
</settings>
</sitecore>
</configuration>

 

Third. Create a rule

using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
using Sitecore.Diagnostics;
using Sitecore.Rules;
using Sitecore.Rules.Conditions;
using Sitecore.XA.Foundation.Abstractions.Configuration;
using Sitecore.XA.Foundation.Multisite;

namespace OneWeb.Feature.Multisite.Conditions
{
 public class SxaEnvironmentRule<T>:StringOperatorCondition<T> where T : RuleContext
 {
  public string querystringvalue { get; set; }

  protected override bool Execute(T ruleContext)
  {
   Assert.ArgumentNotNull((object)ruleContext, nameof(ruleContext));
   if (string.IsNullOrEmpty(this.querystringvalue))
   {
    return false;
   }

   var multisiteConfigurationReader = ServiceLocator.ServiceProvider.GetService<IConfiguration<MultisiteConfiguration>>();
   var multisiteConfiguration = multisiteConfigurationReader?.GetConfiguration();

   if (multisiteConfiguration == null)
   {
    return false;
   }
   return this.Compare(multisiteConfiguration.MultisiteEnvironment, this.querystringvalue);
  }
 }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s