Analytics Data classes

Version: 10.3

The custom Analytics Data classes expose two methods when data is being serialized into the interaction, and one method to rehydrate the content.

An example of Initialize and Serialize methods in the BaseAnalyticsData class diagram.
An example of the Deserialize method in the BaseAnalyticsData class diagram.

Name

Description

Initialize

In the context of a page event, call this method and pass in the arguments received during the call to the service layer. It is the responsibility of the specific AnalyticsEntity object to know how to parse the Request and Result objects to extract the data to be serialized.

Note

In some implementations, an overloaded initialize has been created to accommodate for different input parameters such as an explicit order not retrieved back from the ECS .

Serialize

Call this method to serialize the content into the given dictionary, which is then written into the interaction’s page event or goal custom values.

Deserialize

Call this method to rehydrate the content of the data class. Currently used by the Connect Experience Profile and Experience Analytics reporting components

When data is being serialized, the following utility methods are available:

An example of the AddEntityToEventDictionary method in the BaseAnalyticsData class diagram.

Name

Description

AddEntityToEventDictionary<T>

This templated method is used when adding Connect entities to the custom values dictionary.

Note

It is very important to use this method instead of adding Connect entities directly.

This method does a few things. First, it saves the base entity values only, stripped out of any extension that might have been made by ECS. This is because in a distributed environment, the ECS specific data types might not be available and would cause problems on the reporting and processing servers during the deserialization phase.

In the case where both these servers are in your own domain and control, having the original “custom” ECS entity might be desirable. In this case, the following setting is available in the Sitecore.Commerce.config file:

RequestResponse
<setting name="Commerce.Analytics.EntitiesIncludedInXDB" value="Base"/>

Valid values are:

  • Base

  • Custom

  • Both

Base - saves the base entity only.

Custom - saves the original given entity.

Both - saves the base entity and original version.

By default, Base is configured.

For more information, see the Experience Profile Commerce tab topic.

When data is being rehydrated, use the following utility methods:

A BaseAnalyticsData class diagram showing utility methods.

The above methods allow you to retrieve a value from the serialized list for any type that you require. The GetMandatory* methods add some extra error handling code in case the mandatory parameter is not found, such as adding proper null checks and error logging.

The Get Optional* method is the same as the GetMandatory* methods except no error logging will occur if the key is not present.

The following shows the Connect implementation of the order goal that is registered when a new visitor order is created:

RequestResponse
public class OrderGoalAnalyticsData : BaseAnalyticsData
{
public string ExternalId { get; set; }
public string ShopName { get; set; }
public Total Total { get; set; }
public Order Order { get; set; }
public bool? IsOfflineOrder { get; set; }
public override void Initialize(Pipelines.ServicePipelineArgs args)
   {
   base.Initialize(args);
       this.Order = this.GetOrderFromArgs();
            
       this.ExternalId = this.Order.ExternalId;
       this.ShopName = this.Order.ShopName;
       this.Total = this.Order.Total.ToBaseType<Total>();
       this.IsOfflineOrder = this.Order.IsOfflineOrder;
   }
public virtual void Initialize(Order order)
   {
   this.ServicePipelineArgs = null;
       this.Order = order;
       this.ExternalId = this.Order.ExternalId;
       this.ShopName = this.Order.ShopName;
       this.Total = this.Order.Total.ToBaseType<Total>();
       this.IsOfflineOrder = this.Order.IsOfflineOrder;
   }
public override void Serialize(IDictionary<string, object> customValues)
   {
   customValues.Add(Constants.KnownPageEventDataNames.ExternalId, this.ExternalId);
       customValues.Add(Constants.KnownPageEventDataNames.ShopName, this.ShopName);
       customValues.Add(Constants.KnownPageEventDataNames.Total, this.Total);
       customValues.Add(Constants.KnownPageEventDataNames.IsOfflineOrder, this.IsOfflineOrder);
       this.AddEntityToEventDictionary<Order>(this.Order, customValues, Constants.KnownPageEventDataNames.Order);
   }
public override void Deserialize(IDictionary<string, object> customValues)
   {
   this.ExternalId = this.GetMandatoryCustomValueAsObject<string>(Constants.KnownPageEventDataNames.ExternalId, customValues);
       this.ShopName = this.GetMandatoryCustomValueAsObject<string>(Constants.KnownPageEventDataNames.ShopName, customValues);
       this.Total = this.GetMandatoryCustomValueAsObject<Total>(Constants.KnownPageEventDataNames.Total, customValues);
       this.Order = this.GetOptionalCustomValueAsObject<Order>(Constants.KnownPageEventDataNames.Order, customValues);
       var isOfflineOrder = this.GetOptionalCustomValueAsObject<bool?>(Constants.KnownPageEventDataNames.IsOfflineOrder, customValues);
       this.IsOfflineOrder = isOfflineOrder ?? false;
       base.Deserialize(customValues);
   }
protected virtual Order GetOrderFromArgs()
   {
   Assert.IsTrue(this.ServicePipelineArgs.Result is SubmitVisitorOrderResult, "args.Result is SubmitVisitorOrderResult");
       return ((SubmitVisitorOrderResult)this.ServicePipelineArgs.Result).Order;
   }
}

There are a few things worth mentioning:

  • Two Initialize methods exist. The first takes the ServicePipelineArgs as it is being called by the trigger page event mechanism. The second one takes an Order as it is used during the registering of the off line orders process.

  • When setting the Total property, the ToBaseType<Total>() is used. This is important as we do not want custom extensions to be saved in the page event as these data types might not be available on the reporting and processing servers (multi-server deployment).

  • The Serialize method makes use of the AddEntityToEventDictionary() template method to add the order to the page event.

The following code snippet demonstrates how you would consume the analytics data:

RequestResponse
var customData = AnalyticsDataInitializerFactory.Create<SearchAnalyticsData>();
customData.Deserialize(pageEventData.CustomValues);

The above assumes you have the page event data from the Sitecore interaction. After calling the Deserialize method, the SearchAnalyticsData object properties will be populated with the information.

Do you have some feedback for us?

If you have suggestions for improving this article,