Add Needed JavaScript Code to Any Page in #Sitecore

Many Sitecore sites need different tracking scripts on their website, but for different environments. For instance you might have multiple Azure sites hosting the same site, but in different environments that could be tracked (Dev, QA, Stage, Prod for instance). The proper script should generate for each site instance. This is one way to solve the issue, but I am sure there are other solutions.

In Sitecore I created a Script Settings Folder template. Each folder item contains a Script Setting item. You can have multiple.

I have a configuration filed that contains the settings of the host names for each environment.

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
     <sitecore>
     <settings>
     <setting name="DEV_SERVER_NAME" value="QA.Server.com" />
     <setting name="QA_SERVER_NAME" value="DEV.Server.com" />
     <setting name="PROD_SERVER_NAME" value="Prod1.Server.com,Prod2.Server.com " />
     </settings>
    </configuration>

In the code below in a view (default in this case) the following code is added. The code will compare the current host to what is in the settings and render the appropriate scripts.

string headscripts = "";
string bodyscripts = "";

//Get Parent Item of Script Folder
var scriptdir = Sitecore.Context.Item.Database.GetItem(8DD8956D-7477-41A8-A879-8D3003995F7E);

//Get Hostname from Sitecore to Match the Current Enviroment
string[] hostname = Sitecore.Context.Site.HostName.Split(new char[] { '|' });

//The reason for a string array is because you may have multiple servers like prod that use the same scripts
string[] devservername = Sitecore.Configuration.Settings.GetSetting("DEV_SERVER_NAME").Split(new char[] { ',' });
string[] qaservername = Sitecore.Configuration.Settings.GetSetting("QA_SERVER_NAME").Split(new char[] { ',' });
string[] prodservername = Sitecore.Configuration.Settings.GetSetting("PROD_SERVER_NAME").Split(new char[] { ',' });
string scriptsettingitem = string.Empty;
if(devservername.Intersect(hostname).Any())
{
    scriptsettingitem = "DEV";
} 
else
if (qaservername.Intersect(hostname).Any())
{
    scriptsettingitem = "QA";
}
else
if (uatcdservername.Intersect(hostname).Any())
{
    scriptsettingitem = "PROD";
}
//Synthesis was used in the syntax below, but use whatever works for you. 
if (!string.IsNullOrEmpty(scriptsettingitem))
{
foreach (var childfFolderItem in scriptdir.InnerItem.Children.AsStronglyTypedCollectionOf<IScriptSettingsFolderItem>())
{
   if (childfFolderItem.Name == scriptsettingitem)
   {
    foreach (var scriptitem in childfFolderItem.InnerItem.Children.InnerChildren.AsStronglyTypedCollectionOf<Foundation.Interfaces.Models.I_ScriptSettingBaseItem>())
    {
       headscripts = headscripts + scriptitem.HeadTag;
       bodyscripts = bodyscripts + scriptitem.BodyTag;
     }
break;
   }
}
}

So simple for the most part. What are some other ways to get this done? I would like to learn some other techniques for this.

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