Create a custom page view event for interactions
Once the user visit with the website, Sitecore creates new contact into "[website_Xdb.Collection.Shard0].[xdb_collection].[Contacts]" or "[website_Xdb.Collection.Shard1].[xdb_collection].[Contacts]". Contact could be known or unknown.
For each interaction, it is mandatory to have one event. All events you can see in the "Events" field inside the interaction table in JSON format.
In the event JSON, out of the box, the page view event looks like the below JSON:
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 ModelCreate 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 XDBIn the console application, generate a JSON file of your model and paste it to the below two locations:
<x-connect root path>\App_data\Models
<x-connectroot path>\App_data\jobs\continuous\IndexWorker\App_data\Models
class Program { static void Main(string[] args) { var serlizableModel = XdbModelWriter.Serialize(CustomPageViewEventModel.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 EventCreate 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 EventNow 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 will 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",
}
Comments