Sitecore Publish Telemetry and Reporting Part – I

Sitecore Azure Publish Tracking

No comments

It is always good to know the contents that are being published especially if there are multiple automated jobs that publish items. It is also helpful to track the publish done by various content authors and it helps to quickly revert changes in case of an accidental publishing.

Sitecore Publish Telemetry
Image 1 : Azure Telemetry Publish Event

Currently, Sitecore does not provide out of the box feature to track publishing but there are various ways to track them using Sitecore’s pipeline.In this article, we shall discuss on how to track Sitecore publishing using Azure telemetry and creating Publishing reports. It is really useful if the Sitecore sites are hosted in Azure.

Azure Application Insights

Azure application insights is a feature in Azure to monitor your application. It is used to collect, store and analyze telemetry data from your Azure or On-premise application.

Image 2 : Azure App Insights : Courtesy Microsoft Azure

Normally, Azure application insights is created automatically in Azure when Sitecore’s ARM templates are used.

Sitecore Publish Telemetry Sitecore ARM
Image 3 : Sitecore Azure ARM template

The Azure Application insights instrument key is required for the application to post data and it is actually stored in App_Config/ConnectionStrings.config

Sitecore App Insights Telemetry Key in ConnectionStrings
Image 4 : Sitecore App Insights Telemetry Key in ConnectionStrings

Tracking Publish using Azure Telemetry Client

Configure Microsoft.ApplicationInsights Nuget package in your project to utilize TelemetryClient. The Telemetry client api helps to track Views, Request, Exception, Metric, Events, Trace, Dependency etc.

In below code, you can notice that Publish end event pipleline in Sitecore is used to gather information regarding the published items and then the compiled information is posted to Azure App Insights as Metric and Event.

 public class ItemPublishTracker

        public void PublishEnd(object sender, EventArgs args)
                Publisher publisher = Event.ExtractParameter(args, 0) as Publisher;
                if (publisher == null)
                    Log.Info("PUBLISH TELEMETRY LOG: Cancel Process: Publisher Data is Null.", this);

                // if publishing is not on Web Target than do nothing
                if (publisher.Options == null ||
                    publisher.Options.TargetDatabase == null ||
                    Log.Info("PUBLISH TELEMETRY LOG: Cancel Process: Publisher.Options Data is Null.", this);

                Item currentItem = publisher.Options.RootItem;
                Job job = JobManager.GetJob(publisher.GetJobName());
                var messages = job.Status.Messages;

                //Record in Azure Telemetry
                TrackPublishMetric(GetUnitsProcessedInfo(messages), publisher.Options.UserName, currentItem.ID.ToString(), publisher.Options.Mode.ToString(), publisher.Options.Deep.ToString(), currentItem.Database.Name, currentItem.Paths.FullPath, publisher.Options.Language.Name, publisher.Options.TargetDatabase.Name);
            catch (Exception e)
                Log.Error("PUBLISH TELEMETRY LOG: Could not correctly retrieve the published end item in Sitecore. " + e.ToString(), this);

        private int GetUnitsProcessedInfo(System.Collections.Specialized.StringCollection Messages)
            int processItems = 0;
            foreach (var msg in Messages)
                var result = Regex.Match(msg, @"\d+$").Value;
                int processedNum = 0;
                if (int.TryParse(result, out processedNum))
                    processItems += processedNum;
            return processItems;

        private void TrackPublishMetric(int value,string userName, string itemId, string publishMode, string subitems, string databaseName, string itemPath, string publishedLanguage, string targetDatabase)
            TelemetryClient client = CustomTelemetryService.Initialize();

            var publishMetric = new MetricTelemetry("sitecore:publishmetric", value);
            publishMetric.Timestamp = DateTime.Now;

            var properties = publishMetric.Context.Properties;
            properties.Add("UserName", userName);
            properties.Add("SitecoreItemID", itemId);
            properties.Add("SitecoreItemPath", itemPath);
            properties.Add("SitecoreItemLanguage", publishedLanguage);
            properties.Add("PublishMode", publishMode);
            properties.Add("SubItemsSelected", subitems);
            properties.Add("DataBase", databaseName);
            properties.Add("TargetDatabase", targetDatabase);
            //Sitecore Instance Name
            properties.Add("InstanceName", Sitecore.Configuration.Settings.InstanceName);
            //Track Metric
            //Track Event
            client.TrackEvent("sitecore:publishevent", properties);


Custom service to initialize Telemetry client using appinsights instrumentation key.

public static class CustomTelemetryService
        public static TelemetryClient Initialize()
            TelemetryConfiguration configuration = TelemetryConfiguration.Active;
            ///Get the instrumentation key from Sitecore Connectionstring settings
            configuration.InstrumentationKey =  ConfigurationManager.ConnectionStrings["appinsights.instrumentationkey"].ConnectionString;
            return new TelemetryClient(configuration);

Also make sure to register the pipeline.

<?xml version="1.0"?>
<configuration xmlns:patch="">
      <event name="publish:end">
        <handler type="RD.PublishTelemetry.Events.ItemPublishTracker, RD.PublishTelemetry" method="PublishEnd" />

Metric contains a value parameter through which we can Track the number of items that are published. You can view the metrics using Metric tab in Azure App insights. Using the Metrics drop down, select the metric name provided in the code [ sitecore:publishmetric ]

Sitecore Publish Metrics
Image 5 : Sitecore Publish Metrics

Event contains a set of properties through which we can track number of publish events that have occurred in Sitecore. We track parameters like Username, Item ID , Item Path , Item Language, Publish Mode, Is Subitems Selected, Database , Target database and Instance Name in order to have complete understanding on publishing event.

 Sitecore Publish Event Telemetry
Image 6 : Sitecore Publish Event Telemetry

Now you can clearly understand publishing activities happening in your Sitecore instance and there will not be any blame-game in case of a wrong publish 😉

In the second part of this article, we shall discuss on how to setup a reporting mechanism in Azure to send information about publishing.

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.