﻿#### 

Sitecore has a large number of personalization and automation [conditions](https://doc.sitecore.com/users/92/sitecore-experience-platform/en/the-personalization-conditions.html "Sitecore automation conditions"). Understanding how conditions work and the data behind each condition is essential for successful experience optimization. 

Contact profile values are stored in multiple locations in the Collections database. Historical data is stored in a ProfileScores facet for each interaction. It is also cached in a contact InteractionsCache facet. Final contact behaviour profile is stored in a contact Behavior profile facet. 

![Contact Behaviour Profile scores](https://www.brimit.com/-/media/images/blog/profileconditions1.png)

What important is that next to each Profile Score, Sitecore stores matching profile pattern.

Once any interaction matches profile pattern such pattern will be listed in the ProfileScores facet.

![Contact Behaviour Profile patterns](https://www.brimit.com/-/media/images/blog/profileconditions2.png)

During one of our projects, we found some conditions don't behave according to our expectations. 

We used the following out of the box conditions:

- /sitecore/system/Settings/Rules/Definitions/Elements/XConnect - Visit Conditions/**Where Contact Has Matched Profile Key Value in Profile**
- /sitecore/system/Settings/Rules/Definitions/Elements/XConnect - Visit Search Queries/**Where Contact Has Matched Pattern Card In Profile**

Sitecore evaluates an entire interactions history for the above conditions.

Doing some tests we found that both conditions are evaluated against all interactions available in Sitecore Collection DB. So if a contact has ever matched a pattern in the past, the above mentioned conditions will be evaluated to True. While this is reasonable in some profiling and personalization scenarios it did not quite match our project needs. Instead, we needed a condition like **Where Contact Matches Pattern Card In Profile Now**. i.e. instead of checking all historical data, we wanted to evaluate the current state (or final state of the contact).

Consider the following sample scenario where Product Profile score is collected as a summary between interactions.

| Product Profile score | Interaction 1 | Interaction 2 | Interaction 3 | Interaction 4 | Interaction 5 |
| --- | --- | --- | --- | --- | --- |
| Product A | 10 | 15 | 15 | 20 | 20 |
| Product B | 16 | 36 | 63 | 84 | 96 |
| Product C | 2 | 3 | 3 | 4 | 4 |
| Product D | 18 | 27 | 27 | 36 | 36 |

Notice that for product B contact is progressing much better collecting higher profile score. According to project marketing definitions, this contact matched **Awareness Stage** pattern after 2 interactions, **Evaluation Stage** pattern after 4 interactions, **Action Stage** pattern after Interaction 5. 

![Sitecore Pattern Match Scenario](https://www.brimit.com/-/media/images/blog/patternmatchcondition.png?la=en&amp;hash=76184C07BE5882F7A8F7469B4968A1BD)

If you were to use out of the box **Where Contact Has Matched Pattern Card** condition for segmentation to show different message for a contact matching Product B **Awareness Stage** you condition will always be True even when contact progressed to the **Action Stage** pattern match. Same issue will apply if you try to evaluate pattern match in the Marketing Automation plan definition (entry condition or custom listener).

So we get to a point where a custom condition has to be created. 

Instead of checking historical data we can check final state data available in a ContactBehaviourProfile facet.

Implementation is fairly easy and this post is not intended to go into implementation details. You can find the source code and configuration files on [GitHub](https://github.com/avershalovich/Sitecore9.ConditionsKit "Sitecore9 ConditionsKit"). Once you get to the code you will find two sets of conditions:

- a traditional ComparisonCondition for RulesContext which has to be used for in-session personalization.
- an ICondition + IContactSearchQuery implementation which can be used for List Segmentation and Marketing Automation conditions.

Be sure to follow [Sitecore documentation](https://doc.sitecore.com/developers/90/sitecore-experience-platform/en/create-a-custom-predicate.html "Sitecore documentation") for rules definition and deployment.

###### Author

[!\[Alexei Vershalovich\](https://www.brimit.com/-/jssmedia/feature/blogs/authors/alexei-vershalovich-brimit---500.png?h=1098&amp;iar=0&amp;w=1042&amp;hash=7551A887E43E4DDE95E9C95102DBDF1B)
Alexei Vershalovich
Principal Consultant, digital experience and e-commerce](https://www.brimit.com/blog/author?authors=Alexei%20Vershalovich)

#### More on Sitecore

[!\[How Vercel Will Help You Save Effort When Deploying Sophisticated Sitecore Projects\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2024/vercel_cover-image.png)
#Guides#How-toDXPE-commerce
##### How Vercel Will Help You Save Effort When Deploying Sophisticated Sitecore Projects
Optimize and accelerate the development and deployment of complex multisite Sitecore projects.
Alexei Vershalovich on July 17, 2024](https://www.brimit.com/blog/how-vercel-will-help-you-save-effort-when-deploying-sophisticated-sitecore-projects)

[!\[Training Up Tomorrow's Sitecore MVPs: a Mentoring Success Story\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2023/sitecore-mentoring---cover-image.png)
#How-toDXP
##### Training Up Tomorrow's Sitecore MVPs: a Mentoring Success Story
How to participate in the Sitecore Mentor program and help younger colleagues jump-start a career in Sitecore development.
Sergey Baranov on October 2, 2023](https://www.brimit.com/blog/training-up-tomorrows-sitecore-mvps)

[!\[Going Headless. Part 2: When a Headless CMS Is Your Best Bet (if you have Sitecore)\](https://www.brimit.com/-/jssmedia/project/brimit/blog/2022/headless/adobestock_456986731.jpg)
#How-toDXPE-commerce
##### Going Headless. Part 2: When a Headless CMS Is Your Best Bet (if you have Sitecore)
Discover how a headless CMS can benefit organizations that use Sitecore.
Daniil Raschupkin, Palina Trokhautsava on September 15, 2022](https://www.brimit.com/blog/going-headless-part-2-when-a-headless-cms-is-your-best-bet-if-you-have-sitecore)

![](https://bat.bing.net/action/0?ti=187017043&amp;tm=gtm002&amp;Ver=2&amp;mid=581756ab-d6be-43dc-9422-890df3541aa7&amp;bo=2&amp;gtm_tag_source=1&amp;pi=0&amp;lg=en-US&amp;sw=800&amp;sh=600&amp;sc=24&amp;nwd=1&amp;tl=Custom%20Sitecore%20Contact%20Behaviour%20Profile%20Conditions&amp;kw=Sitecore,%209,%20Custom,%20Marketing,%20%20Automation,%20Action,%20xconnect,%20conditions&amp;p=https%3A%2F%2Fwww.brimit.com%2Fblog%2Fcustom-sitecore-contact-behaviour-profile-conditions&amp;r=&amp;lt=283&amp;evt=pageLoad&amp;sv=2&amp;asc=D&amp;cdb=AQAY&amp;rn=211608)