THIS IS ARCHIVED DOCUMENTATION

Changing the Events Data send to Sitecore Analytics

By default, the analytics component only tracks a limited amount of information on each search event. However, by using the Coveo analytics pipeline, custom processing can be done to change the format in which the information is stored or to include custom metadata. The current tutorial will show you how to create a custom analytics processor in order to change the way the results URIs are stored in the database, and to include the device which triggered the search event.

Steps

  1. Create a new C# class following the code below. You need to reference the Coveo.SearchProviderBase and Coveo.Framework dlls, as well as Sitecore.Kernel and Sitecore.Analytics. This is your custom processor for the Coveo analytics pipeline.

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text.RegularExpressions;
     using Coveo.SearchProvider.Analytics;
     using Coveo.SearchProvider.Pipelines;
    
     namespace CustomAnalyticsProcessing
     {
         public class CustomAnalyticsEventProcessor
         {
             /// <summary>
             /// A <see cref="CoveoAnalyticsPipelineArgs"/> processor. This method is responsible for changing the way
             /// the results are stored and for logging the current device.
             /// </summary>
             /// <param name="p_Args">The <see cref="CoveoAnalyticsPipelineArgs"/>.</param>
             public void Process(CoveoAnalyticsPipelineArgs p_Args)
             {
                 SearchEvent searchEvent = p_Args.AnalyticsData as SearchEvent;
                 if (searchEvent != null) {
                     Regex guidRegex = new Regex(@"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b", RegexOptions.IgnoreCase);
                     // Modify the way the results are stored only for the search box event.
                     if (searchEvent.Type == "search box") {
                         // Find all the GUIDs in the EventData Text parameter. This field typically contains the result URIs.
                         IEnumerable<string> searchResultsIds = guidRegex.Matches(p_Args.EventData.Text)
                                                                         .Cast<Match>()
                                                                         .Select(match => match.Value)
                                                                         .ToList();
                         // Replace the results URIs with their Sitecore item ID, and keep track of the device (for example, Chrome, Firefox)
                         // that generated the event.
                         p_Args.EventData.Text = String.Join("|", searchResultsIds);
                         p_Args.EventData.DataKey = String.Format("Device: {0}", searchEvent.Device);
                     }
                 }
             }
         }
     }
    
    • This method simply detects when a query event is triggered and replaces the content of the Text property with a pipe-separated list of result IDs. Furthermore, it keeps track of the current device and stores this information in the DataKey property.
  2. Compile the class and copy its assembly to the bin directory of your Sitecore website.
  3. Create a Coveo.UI.Controls.Custom.config file, which is a patch of the Coveo.UI.Controls.config and add the <coveoAnalyticsPipeline> element in which you will place your processor.

     <coveoAnalyticsPipeline>
       <processor mode="on" type="Coveo.SearchProvider.Processors.HandleSearchEventProcessor, Coveo.SearchProviderBase" />
       <processor mode="on" type="Coveo.SearchProvider.Processors.HandleDocumentViewEventProcessor, Coveo.SearchProviderBase" />
    
       <!-- Our custom analytics processor is defined here -->
       <processor mode="on" type="CustomAnalyticsProcessing.CustomAnalyticsEventProcessor, CustomAnalyticsProcessing" />
    
       <processor mode="on" type="Coveo.SearchProvider.Processors.TrackAnalyticsEventProcessor, Coveo.SearchProviderBase" />
     </coveoAnalyticsPipeline>
    

    There are many other ways to define processors in configuration files. For example, you can define this processor in your own configuration file, as long as it’s located under the App_Config/Include directory of your website. See the All About Web.config Include Files with the Sitecore ASP.NET CMS article for more information.

  4. Browse to your Coveo-powered search page and perform a new query.
  5. Validate that the processor was executed correctly by querying the PageEvents table in your analytics database. You should see that the DataKey and Text data are now logged differently than your previous queries.
    1. DMS results:

    2. xDB results: