1

I have one requirement where when users interact with the page I need to make interaction with some custom facets, start and end times. Suppose the user was on the home page for 5 mins then StartDateTime will have the start time when the user land on a page and on EndDateTime will be the time when the user redirect to another page (+5 mins from StartDateTime in my scenario).

I have added interactions with custom facets on InteractionFacets table as mentioned here now I am not sure how EndDateTime will calculate because in Database when I check Interactions table StartDateTime and EndDateTime have few seconds different.

2 Answers 2

1

EndDateTime of interaction is calculated as the date and time of the last event in the interaction + duration of the last event (for page view events, for example, duration is the server response time for the visited page).

public DateTime EndDateTime
{
      get => this.Events.Count != 0 ? this.Events.Max<Event, DateTime>((Func<Event, DateTime>) (ev => ev.Timestamp + ev.Duration)) : new DateTime();
}

So, if you see just a second or couple of seconds difference between StartDateTime and EndDateTime of an interaction, please check how many events were recorded for this interaction in the Shards databases:

SELECT TOP (100)
    [InteractionId],
    [StartDateTime],
    [EndDateTime],
    [Events]
FROM [xdb_collection].[Interactions]
ORDER BY StartDateTime DESC

If there is just one event in the interaction, the time gap between StartDateTime and EndDateTime will be minimal because it will be equal to the server page load time. Interactions with multiple events usually have a longer delay between StartDateTime and EndDateTime:

Interactions example

1

To achieve this functionality I created a custom page view event for interactions and below are the steps:

Create a Custom Facet Model Create a class CustomPageViewEvent. I have given DefaultFacetKey as " CustomPageViewEvent" and EventDefinitionId id you can give either the existing or create new event. I have added a new field like the Number field.

 [Serializable]  
   [FacetKey(DefaultFacetKey)]  
   public class CustomPageViewEvent : Event  
   {  
     public CustomPageViewEvent(DateTime timestamp, Guid itemId, int itemVersion, string itemLanguage, string dataKey, string data, string number)  
      : base(EventDefinitionId, timestamp)  
     {  
       this.ItemId = itemId;  
       this.ItemVersion = itemVersion;  
       this.ItemLanguage = itemLanguage;  
       this.DataKey = dataKey;  
       this.Data = data;  
       this.Number = number;        
     }  
     public const string DefaultFacetKey = "CustomPageViewEvent";  
     public static Guid EventDefinitionId { get; } = new Guid("CD52B756-21B4-4028-8BA5-E981B8A96F95");  
     public string ItemLanguage { get; set; }  
     public int ItemVersion { get; set; }  
     public string Url { get; set; }  
     public string Number { get; set; }  
   }  

Register the Custom Facet Model Create a CustomPageViewEventModel class to register your custom facet model.

  public static class CustomPageViewEventModel  
   {  
     public static XdbModel Model { get; } = BuildModel();  
     private static XdbModel BuildModel()  
     {  
       var modelBuilder = new XdbModelBuilder("CustomPageViewEventModel", new XdbModelVersion(1, 0));  
       modelBuilder.DefineEventType<CustomPageViewEvent>(true);  
       return modelBuilder.BuildModel();  
     }  
   }  

Deploy custom Facet model to XDB

In the console application, generate a JSON file of your model and paste it to the below two locations:

\App_data\Models \App_data\jobs\continuous\IndexWorker\App_data\Models

 class Program  
   {  
     static void Main(string[] args)  
     {  
       var serlizableModel = XdbModelWriter.Serialize(CustomPageViewEventCollectionModel.Model);  
       File.WriteAllText(CustomPageViewEventCollectionModel.Model.FullName + ".json", serlizableModel);  
     }  
   }  

Add Custom Facet Model to Configuration Now create a patch file and paste the below code:

 <?xml version="1.0"?>  
 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">  
  <sitecore>  
   <xconnect>  
    <runtime type="Sitecore.XConnect.Client.Configuration.RuntimeModelConfiguration,Sitecore.XConnect.Client.Configuration">  
     <schemas hint="list:AddModelConfiguration">  
      <schema name="customageviewomodel" type="Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration" patch:after="schema[@name='collectionmodel']">  
       <param desc="modeltype">Website.CustomPageViewEventModel, Website</param>  
      </schema>  
     </schemas>  
    </runtime>  
   </xconnect>  
  </sitecore>  
 </configuration>  

Create a Custom Event Create a new class ConvertPageEventDataToCustomPageViewEvent and override ConvertPageEventDataToEventBase class.

  public class ConvertPageEventDataToCustomPageViewEvent : ConvertPageEventDataToEventBase  
   {  
     protected override bool CanProcessPageEventData(Sitecore.Analytics.Model.PageEventData pageEventData)  
     {  
       if (pageEventData.PageEventDefinitionId == Guid.Parse("{CD52B756-21B4-4028-8BA5-E981B8A96F95}"))  
       {  
         return true;  
       }  
       return false;  
     }  
     protected override Event CreateEvent(Sitecore.Analytics.Model.PageEventData pageEventData)  
     {  
       var number = pageEventData.CustomValues["Number"]?.ToString();  
       CustomPageViewEvent ev = new CustomPageViewEvent(pageEventData.DateTime, pageEventData.ItemId, 1, "en", pageEventData.DataKey, pageEventData.Data, number);  
       return ev;  
     }  
   }  

Create a Patch File

<?xml version="1.0"?>  
 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/"  
   xmlns:env="http://www.sitecore.net/xmlconfig/env/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">  
      <sitecore>  
           <pipelines>  
                <convertToXConnectEvent>  
                     <processor patch:after="processor[@type='Sitecore.Analytics.XConnect.DataAccess.Pipelines.ConvertToXConnectEventPipeline.ConvertPageEventDataToGoal, Sitecore.Analytics.XConnect']" type="Website.ConvertPageEventDataToCustomPageViewEvent, Website"/>  
                </convertToXConnectEvent>  
           </pipelines>  
      </sitecore>  
 </configuration>  

Register Event Now you can register your event wherever you want and you can pass CustomValues as I pass Number.

var ev = Tracker.MarketingDefinitions.PageEvents[new Guid("{CD52B756-21B4-4028-8BA5-E981B8A96F95}")];  
         if (ev != null && !string.IsNullOrEmpty(model.Number))  
         {  
           var pageData = new Sitecore.Analytics.Data.PageEventData(ev.Alias, ev.Id);  
           pageData.CustomValues.Add("Number", model.Number);  
           Tracker.Current.CurrentPage.Register(pageData);  
         }

Now once you hit the page or click any button where you have registered for this event, it will store in the Interactions table in the Events field just like below:

{  
      "@odata.type": "#Website.CustomPageViewEvent",  
      "CustomValues": [],  
      "DefinitionId": "cd52b756-21b4-4028-8ba5-e981b8a96f95",  
      "ItemId": "2a00b93f-dab9-4536-b1d2-8ee3e832c7da",  
      "Id": "a7816c4f-b7b5-4fac-aca2-08ac17b26cc4",  
      "ParentEventId": "c6b1b479-8378-4311-b4b2-4231937399cc",  
      "Timestamp": "2023-06-08T13:38:04.9040878Z",  
      "ItemLanguage": "en",  
      "ItemVersion": 1,  
      "Number": "6OEVv0sFCjZ0OASiQcpvuDYg18o9lH94CQOWgmyGkeahLxXzlfckSwMXrT999tr4zrHN3a9r91BUFNEnQpQ2",       
 }  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.