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="">
     <setting name="DEV_SERVER_NAME" value="" />
     <setting name="QA_SERVER_NAME" value="" />
     <setting name="PROD_SERVER_NAME" value=", " />

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;
    scriptsettingitem = "DEV";
if (qaservername.Intersect(hostname).Any())
    scriptsettingitem = "QA";
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;

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: Logo

You are commenting using your 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