Customizing Sitecore Analytics

In my previous blog (https://subbu.ca/blogs/retrieving-interactions-data-from-mongodb-using-sitecore-analytics-api/), we were discussing on how to retrieve the existing Sitecore Analytics Interaction Details using Mongo API or Sitecore Analytics API.

But what if we are looking out for details that will not be stored in Analytics Database by default (eg: User Details like User ID, First Name, Last Name, Organization, Business Unit, Region etc.)

We know that once the session ends, Sitecore will flush the details to MongoDB and then will copy it to the Reporting Database. Sitecore has some Aggregation pipelines for this purpose. For storing the custom details along with the Interactions, you can edit these existing Aggregation pipelines or you can even create a new one. Then patch it into the existing configurations,

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <group groupName="analytics.aggregation">
        <pipelines>
          <interactions>
            <processor type="SitecoreWebsite.SCExtensions.Analytics.CustomAnalyticsAggregationProcessor, SitecoreWebsite.SCExtensions" />
          </interactions>
        </pipelines>
      </group>
    </pipelines>
  </sitecore>
</configuration>

And your processor class would be something like this,

namespace SitecoreWebsite.SCExtensions.Analytics
{
    public class CustomAnalyticsAggregationProcessor : AggregationProcessor
    {
        public CustomAnalyticsAggregationProcessor()
        {
        }

        protected override void OnProcess(AggregationPipelineArgs args)
        {
            try
            {
  //Get the Current Visit
                VisitData visit = args.Context.Visit;
                if (visit.Pages != null &amp;&amp; 0 &lt; visit.Pages.Count) { //Create a DataTable to store the Interaction Details DataTable pageViews = new DataTable(); pageViews.Columns.Add("Date", typeof(DateTime)); pageViews.Columns.Add("InteractionId", typeof(Guid)); pageViews.Columns.Add("ItemId", typeof(Guid)); pageViews.Columns.Add("ContactId", typeof(Guid)); pageViews.Columns.Add("UserId", typeof(string)); pageViews.Columns.Add("Actions", typeof(Int64)); //Postbacks &amp; Views pageViews.Columns.Add("Duration", typeof(Int64)); pageViews.Columns.Add("Url", typeof(string)); visit.Pages.GroupBy(i =&gt; new { i.Item.Id, Url = i.Url.ToString() }).ToList().ForEach(j =&gt;
                    {
                        PageData page = j.First(); 
                        pageViews.Rows.Add(new object[]{ page.DateTime, args.Context.Visit.InteractionId, page.Item.Id, args.Context.Visit.ContactId, args.Context.Contact.Identifiers.Identifier,
                        j.Count() , j.Sum(s =&gt; s.Duration), page.Url.ToString()});
                    });
                }
            }
            catch (Exception ex)
            {
                //Error Logging
            }
        }
    }
}

In order to keep track of the records, I would also store some default Sitecore Analytics Details like Interaction Id, Contact Id, Item Id etc. I have used ‘User Id’ as an identifier while Analytics Tracking is started, hence I am getting the ‘User Id’ using ‘args.Context.Contact.Identifiers.Identifier’

Once the required data is gathered, I would choose to copy them to the custom database using SqlBulkCopy API.  You can also use your own custom storage mechanism (like Mongo DB etc.).

And you can customize the Sitecore Analytics in the way you want it. Have Fun!!

Originally Posted at:  http://www.nttdatasitecore.com/Blog/2016/August/Customizing-Sitecore-Analytics

Leave a Reply

Your email address will not be published. Required fields are marked *