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