1

I have a requirement to create a personalization rule to change the rendering display based on whether the user is First Time user or New User.

The first-time user will be decided if the users last login or contact. InterAction is null.

The New User user will be decided if the users last login or contact. InterAction date is less than 30 days.

I want the Last Logged in date from the Sitecore contact. InterAction. How do I do GetContact and what do I need to pass as parameter. How do I get the contact. InterAction value. using xconnect.

1
  • What are you using for authentication? I would have thought the last login details would be stored on the user record vs the Contact
    – Richard Seal
    Commented Sep 7, 2022 at 14:36

1 Answer 1

1

Sitecore provides a flag to identify new anonymous users on the tracker: Tracker.Current.Contact.IsNew

You can use this in a condition as below:

public class IsFirstTimeUserCondition<T> : WhenCondition<T> where T : RuleContext
{
    protected override bool Execute(T ruleContext)
    {
        if (Tracker.Current == null || !Tracker.Current.IsActive) return false;

        if (Tracker.Current.Contact.IsNew) return true;

        return false;
    }
}

To check a contacts previous interactions, you need to call XConnect directly. A condition as below should satisfy your requirements:

public class IsNewUserCondition<T> : WhenCondition<T> where T : RuleContext
{
    protected override bool Execute(T ruleContext)
    {
        if (Tracker.Current == null || !Tracker.Current.IsActive) return false;

        using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
        {
            var references = new List<Sitecore.XConnect.IEntityReference<Sitecore.XConnect.Contact>>()
                {
                    new IdentifiedContactReference(Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource, Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N"))
                };

            var contacts = client.Get<Sitecore.XConnect.Contact>(references, new ContactExecutionOptions(new Sitecore.XConnect.ContactExpandOptions(PersonalInformation.DefaultFacetKey)
            {
                Interactions = new Sitecore.XConnect.RelatedInteractionsExpandOptions(LocaleInfo.DefaultFacetKey)
                {
                    StartDateTime = DateTime.MinValue,
                    EndDateTime = DateTime.UtcNow.AddDays(-30),
                    Limit = 1
                }
            }));
            if (!contacts.FirstOrDefault().Entity.Interactions.Any()) return true;
            return false;
        }
    }
}

These conditions have been tested and work on Sitecore 10.1 XP.

References:

https://doc.sitecore.com/xp/en/developers/101/sitecore-experience-platform/set-contact-facets-in-session.html

https://doc.sitecore.com/xp/en/developers/101/sitecore-experience-platform/get-contact-with-interactions.html

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.