I am creating custom facets in Sitecore 10.2. Below steps I have performed:
- Created custom facets.
[FacetKey(DefaultFacetKey)]
[Serializable]
public class CustomFacet : Facet
{
public const string DefaultFacetKey = "CustomFacet";
public string ResidentialStatus { get; set; }
}
- Created custom facet model
public class CustomFacetModel
{
public static XdbModel Model { get; } = null;
static CustomFacetModel()
{
Model = BuildModel();
}
private static XdbModel BuildModel()
{
var builder = new XdbModelBuilder(typeof(CustomFacetModel).FullName, new XdbModelVersion(1, 0));
builder.DefineFacet<Contact, CustomFacet>(CustomFacet.DefaultFacetKey);
return builder.BuildModel();
}
}
- Deployed dll on inetpub/wwwroot/website
- Created Json file and pasted on below two places:
{
"Name": "xx.Foundation.Analytics.Models.CustomFacetModel",
"Version": "1.0",
"References": [
{
"Name": "XConnect",
"Version": "1.0"
}
],
"Types": {
"xx.Foundation.Analytics.Models.CustomFacet": {
"Type": "Facet",
"BaseType": "Sitecore.XConnect.Facet",
"ClrType": "xx.Foundation.Analytics.Models.CustomFacet, xx.Foundation.Analytics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
"Properties": {
"ResidentialStatus": {
"Type": "String"
}
}
}
},
"Facets": [
{
"Target": "Contact",
"Name": "CustomFacet",
"Type": "xx.Foundation.Analytics.Models.CustomFacet"
}
]
}
inetpub\wwwroot\xxxxconnect.dev.local\App_Data\jobs\continuous\IndexWorker\App_Data\Models
inetpub\wwwroot\xxxlocalxconnect.dev.local\App_Data\Models
- Created patch file:
<?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="CustomFacetModel" type="Sitecore.XConnect.Client.Configuration.StaticModelConfiguration,Sitecore.XConnect.Client.Configuration" patch:after="schema[@name='collectionmodel']">
<param desc="modeltype">XX.Foundation.Analytics.Models.CustomFacetModel,XX.Foundation.Analytics</param>
</schema>
</schemas>
</runtime>
</xconnect>
</sitecore>
</configuration>
- Saving facets in xDB
using (var client = SitecoreXConnectClientConfiguration.GetClient())
{
var trackerIdentifier = new IdentifiedContactReference(identifierSource, email);
var contact = client.Get(trackerIdentifier, new ContactExpandOptions(CollectionModel.FacetKeys.PersonalInformation, CollectionModel.FacetKeys.EmailAddressList, CollectionModel.FacetKeys.PhoneNumberList, PhoneNumberList.DefaultFacetKey, CustomFacet.DefaultFacetKey));
if (contact == null)
{
if (Tracker.Current == null && Tracker.Enabled)
{
Tracker.StartTracking();
}
if (!Tracker.Enabled || Tracker.Current == null || !Tracker.Current.IsActive)
{
return false;
}
}
}
Tracker.Current is coming null, Tracker.Enabled is true. In log file below error is coming:
25284 14:17:19 ERROR Cannot create tracker.
Exception: System.ArgumentException
Message: The specified type is not a valid facet type.
Parameter name: facetType
Source: Sitecore.Analytics.Model
at Sitecore.Analytics.Model.Framework.ModelFactory.CreateFacet(Type facetType)
at Sitecore.Analytics.Model.Framework.Faceted.AddFacet(String name, Type type)
at Sitecore.Analytics.Model.ContactFactory.Create(ContactCreationParameters parameters, IReadOnlyDictionary`2 facets)
at Sitecore.Analytics.Data.ContactRepository.CreateContact(ID id)
at Sitecore.Analytics.Tracking.ContactManager.CreateContact(ID id)
at Sitecore.Analytics.Pipelines.EnsureSessionContext.LoadContact.Process(InitializeTrackerArgs args)
at (Object , Object )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Analytics.Pipelines.EnsureSessionContext.EnsureSessionContextPipeline.<>c__DisplayClass4_0.<Run>b__0()
at Sitecore.Analytics.XConnect.Diagnostics.PerformanceCounters.OperationPerformanceMonitorExtensions.<>c__DisplayClass1_0.<Monitor>b__0()
at Sitecore.Analytics.XConnect.Diagnostics.PerformanceCounters.OperationPerformanceMonitorExtensions.Monitor[T](OperationPerformanceMonitorBase monitor, Func`1 operation)
at Sitecore.Analytics.DefaultTracker.EnsureSessionContext()
at Sitecore.Analytics.Pipelines.CreateTracker.GetTracker.Process(CreateTrackerArgs args)
at (Object , Object )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Analytics.Tracker.Initialize()
xDB is enabled and SC_ANALYTICS_GLOBAL_COOKIE
is coming in browser. Please help.