Customizing Sitecore Analytics

Posted 08/08/2016 by Subramanian Ramanathan

In my previous blog post, we were discussing on how to retrieve the existing Sitecore Analytics Interaction Details using the Mongo API or the 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 it will be copied to the Reporting Database. Sitecore has some Aggregation pipelines for this purpose. For storing the custom details along with the Interactions, we can edit these existing Aggregation pipelines or we can even create a new one. Then patch it into the existing configurations.


  
    
      
        
          
            
          
        
      
    
  

And the 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 && 0 < 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 & Views
                    pageViews.Columns.Add("Duration", typeof(Int64));
                    pageViews.Columns.Add("Url", typeof(string));

                    visit.Pages.GroupBy(i => new { i.Item.Id, Url = i.Url.ToString() }).ToList().ForEach(j =>
                    {
                        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 => s.Duration), page.Url.ToString()});
                    });
                }
            }
            catch (Exception ex)
            {
                //Error Logging
            }
        }
    }
}

In order to keep track of the records, I have stored some default Sitecore Analytics Details like Interaction Id, Contact Id, Item Id etc using VisitData and PageData. 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 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. 

Share:

Add your comment

 
 

 

Archive

Syndication