6

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);


    }
1
  • Edit: Ignore me - you said current interaction, not contact. I will get back to you. :)
    – mhwelander
    Commented Oct 30, 2018 at 13:17

1 Answer 1

7

In the context of the tracker, interaction facets do not exist. However, you can set custom values on an interaction:

Sitecore.Analytics.Tracker.Current.Interaction.CustomValues["myvalue"] = "blah";

Before 9.X, these custom values were persisted in MongoDB. In 9.X+, you have to map custom values to an interaction facet in xConnect. That facet will never be loaded back into the tracker because there is no such concept as continuing an existing interaction.

So in your case, I suspect you will want to:

  1. Set custom values on the current interaction.
  2. Whilst your data is in the format of a custom value, use that for personalization.
  3. On session end, map the custom values to an interaction facet in xConnect - assuming you want to save this data and not use it exclusively in session and then get rid of it. https://doc.sitecore.net/developers/xp/tracking-and-session/tracker/interaction-facets.html

PS - Great docs feedback, adding to my list! :)

1
  • BOOM! Thats what I needed! This makes a lot of sense. I am also realizing that interaction facets are only useful in xConnect and MA and not really personalization. But storing data in the Custom Data and then doing something with it in SessionEnd was the magic sauce! Thanks Martina! Commented Oct 30, 2018 at 21:42

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.