I have a custom xDB/xConnect Interaction Facet that I have created using Sitecore's documentation found here: https://doc.sitecore.net/developers/xp/xconnect/xconnect-model/facets/creating-facets/index.html
I am trying to create a custom rule condition for the purposes of personalization. The rule I am trying to target is to be used during the Current Interaction.
Personalization Rule Requirement
Trying to create:
where SiteIds is equal to [somevalue] in current interaction.
Current Rule Code Created
public bool Evaluate(IRuleExecutionContext context)
{
var contact = context.Fact<Contact>();
var interaction = contact.Interactions.OrderByDescending(sort => sort.StartDateTime).FirstOrDefault(i => !i.GetFacet<SalesforceInteraction>().SiteIds.IsNullOrEmpty());
var sfFacet = interaction?.GetFacet<SalesforceInteraction>();
var kruxInfo = sfFacet?.GetSiteIdsFromInteraction();
return kruxInfo != null && Comparison.Evaluate(kruxInfo.KruxSegment, KruxSegment);
}
However, after looking at this, I fret that this won't pick up the current interaction which is the requirement.
When I look at other OOTB rules that specify current interaction, I get examples like the following:
public string GoalId { get; set; }
private Guid GoalGuid { get; set; }
protected override bool Execute(T ruleContext)
{
Assert.ArgumentNotNull((object) ruleContext, "ruleContext");
Assert.IsNotNull((object) Tracker.Current, "Tracker.Current is not initialized");
Assert.IsNotNull((object) Tracker.Current.Session, "Tracker.Current.Session is not initialized");
Assert.IsNotNull((object) Tracker.Current.Session.Interaction, "Tracker.Current.Session.Interaction is not initialized");
try
{
this.GoalGuid = new Guid(this.GoalId);
}
catch
{
Log.Warn(string.Format("Could not convert value to guid: {0}", (object) this.GoalId), (object) this.GetType());
return false;
}
return ((IEnumerable<Page>) Tracker.Current.Session.Interaction.Pages).Any<Page>((Func<Page, bool>) (page => page.PageEvents.Any<PageEventData>((Func<PageEventData, bool>) (e => e.PageEventDefinitionId == this.GoalGuid))));
}
}
This utilizes Tracker.Current
to get the current interaction. When I explored using that method, I could NOT figure out how to reference the custom facet information in xConnect using the Tracker.Current methodology.
How do I correctly create this rule?
EDIT 1:
I've updated my rule evaluation code to look like this, still not sure if this will work.
public bool Evaluate(IRuleExecutionContext context)
{
var contact = context.Fact<Contact>();
var interaction = Tracker.Current?.Session?.Interaction?.InteractionId == null
? contact.Interactions.OrderByDescending(sort => sort.StartDateTime).FirstOrDefault(i => !i.GetFacet<SalesforceInteraction>().SiteIds.IsNullOrEmpty())
: contact.Interactions.FirstOrDefault(i => i.Id.HasValue && i.Id.Value.Equals(Tracker.Current.Session.Interaction.InteractionId));
var sfFacet = interaction?.GetFacet<SalesforceInteraction>();
var kruxInfo = sfFacet?.GetSiteIdsFromInteraction();
return kruxInfo != null && Comparison.Evaluate(kruxInfo.KruxSegment, KruxSegment);
}