---
title: "Personalize a page using affinities"
slug: "personalize-a-page-using-affinities"
abstract: "Use affinity scores to deliver personalized page variants based on what visitors are most interested in."
---
# Personalize a page using affinities

> [!important]
> Feature availability is part of a phased rollout. Your organization may not see this functionality yet. It will become available when your environment is included in the rollout.

[Affinities](https://doc.sitecore.com/sai/en/users/sitecoreai/audience-and-insights/affinities.html "Affinities") let you customize page experiences based on what visitors are most interested in.

As visitors view pages with assigned affinities, SitecoreAI calculates affinity scores for the affinity values associated with the attributes you defined. These scores are updated in near real time and can be used as conditions to determine which page variant a visitor sees.

For example, on a site where visitors browse different models and features, affinity scores can indicate which values, such as *electric*, *suv*, or *performance*, best reflect a visitor's current interests.

> [!note]
> **Before you begin**
> Make sure you have already [set up affinities for your site](https://doc.sitecore.com/sai/en/users/sitecoreai/audience-and-insights/affinities/set-up-affinities-for-a-site.html "Set up affinities for a site") so affinity scores can be calculated and used for personalization.

You can use affinity scores to [personalize a page](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize/create-a-page-variant.html "Create a page variant") in two ways:

- Use the [Top Affinity condition](https://doc.sitecore.com/sai/en/users/sitecoreai/audience-and-insights/affinities/personalize-a-page-using-affinities.html#use-the-top-affinity-condition) to target visitors based on their highest affinity score.
- [Create a custom condition](https://doc.sitecore.com/sai/en/users/sitecoreai/audience-and-insights/affinities/personalize-a-page-using-affinities.html#create-a-custom-condition-with-affinities "Create a custom condition with affinities") to combine affinity scores with other rules and target matching visitors.

## Use the Top Affinity condition

Use this option to personalize a page based on a visitor's highest affinity score for a specific affinity value.

To use the Top Affinity condition:

1. In the Page builder in **Editor** mode, in the site tree, select the page you want to personalize.
2. On the **Personalize** tab, in the left-hand side panel, click **Create variant**.
3. To create the audience intended for that page variant, in the **Create new page variant** dialog, enter a name for the variant, then click **Next**.
4. In the **Create your audience** dialog, in **Build** mode, enter the audience name and click **Add first condition**.
5. In the search field in the **Conditions** right-hand pane, search for *Top Affinity*.

    The **Top Affinity** condition targets visitors whose highest affinity score matches the criteria you define.

    ![Screenshot of the Top Affinity condition.](https://doc.sitecore.com/api/markdown-image/sai/en/users/sitecoreai/images/NewTopAffinityConditionUI.png)

6. Click :mdiPlusCircle: to add the condition.
7. Configure the condition:
    - **Affinity value** - enter the specific value you want the condition to match, for example, *electric*, *suv*, or *performance*. The condition is met when this value has the visitor's highest affinity score within the specified affinity name.

        You can enter multiple affinity values if needed. Press Enter after each one.

    - **Affinity name (optional)** - enter the attribute you want to evaluate, for example, *product_type*, *model*, or *focus*. Use this field to limit the condition to values associated with a specific affinity name.
        
    > [!important]
    > Affinity names and values must exactly match those defined during [setup](https://doc.sitecore.com/sai/en/users/sitecoreai/audience-and-insights/affinities/set-up-affinities-for-a-site.html "Set up affinities for a site").

    ### Example 1: Single affinity value

    If you configure the condition with:
    - **Affinity value**: *electric* and **Affinity name**: *model*

        ![Condition using a single affinity value](https://doc.sitecore.com/api/markdown-image/sai/en/users/sitecoreai/images/affinities-electric.png)

        SitecoreAI evaluates the visitor's affinity scores for values associated with the *model* affinity name. If the visitor has the highest affinity value for *electric*, the visitor matches the condition and is shown the corresponding page variant.

    ### Example 2: Multiple affinity values 

    If you configure the condition with:
   - **Affinity values**: *electric*, *hybrid* and **Affinity name**: *model*

        ![Condition using multiple affinity values](https://doc.sitecore.com/api/markdown-image/sai/en/users/sitecoreai/images/affinities-electric-or-suv.png)

        SitecoreAI evaluates the visitor's affinity scores for values associated with the *model* affinity name. If the visitor has the highest affinity value for either *electric* or *hybrid*, the visitor is shown the corresponding page variant.

        The affinity values are evaluated using OR logic, meaning only one of the specified values needs to have the highest affinity score for the condition to be met.

    
8. Optionally, if needed, you can also:
    - Add additional conditions and logical operators between them.
    - [Group multiple conditions together](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize/work-with-condition-groups.html "Work with condition groups").
    - Reorder conditions or groups using the up :mdiArrowUp: or down :mdiArrowDown: arrows.
9. Click **Save**.
10. Your new page variant is now available in the list of variants in the left pane. You can now [edit the variant](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize/edit-a-page-variant.html "Edit a page variant"). When editing is complete, [publish](https://doc.sitecore.com/sai/en/users/sitecoreai/build-pages/publishing-pages-and-referenced-items/move-an-item-through-the-workflow.html "Move an item through the workflow") the variant.

SitecoreAI shows the variant to visitors whose highest affinity score matches the configured affinity value.

## Create a custom condition with affinities

Use this option when you want to combine affinity scores with other visitor data or business rules to personalize a page.

To create a custom condition with affinities:

1. In the Page builder in **Editor** mode, in the site tree, select the page you want to personalize.
2. On the **Personalize** tab, in the left-hand side panel, click **Create variant**.
3. To create the audience intended for that page variant, in the **Create new page variant** dialog, enter the page variant name and click **Next**.
4. In the **Create your audience** dialog, in **Build** mode, enter the audience name and click **Add first condition**.
5. In the **Conditions** right-hand panel, click **Custom**, then click **Create**.

    ![Screenshot of the audience builder with the custom condition option.](https://doc.sitecore.com/api/markdown-image/sai/en/users/sitecoreai/images/CreateCustomconditionAffinities.png)

6. Enter a name for the new condition.
7. In the code editor, choose one of the following options:
    - Use JavaScript to write the logic for your [custom condition](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize/create-a-custom-condition.html "Create a custom condition").

        **Example logic**

        This example checks whether a visitor has a high interest in electric vehicles and has viewed the Vehicle comparison page:

        ```csharp
        (function () {
          const requiredScore = 0.5;
          const pageName = "Vehicle comparison";

          const affinityScore =
            profile?.traits?.affinities?.product_type?.Electric?.score ?? 0;

          const hasViewedPage =
            profile?.sessions?.some(session =>
              session?.events?.some(event => event?.customData?.page === pageName)
            ) ?? false;

          return affinityScore > requiredScore && hasViewedPage;
        })();
        ```

    - Use the [Code Assistant](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize/create-a-custom-condition/create-a-condition-with-the-code-assistant.html "Create a condition with the Code Assistant") to describe the condition logic in natural language.

        **Example prompt**

        *Write a condition to display content for visitors whose affinity score for the *electric* value of *product_type* affinity name is greater than 50% and who have viewed the *Vehicle comparison* page*.

8. Click **Save**. After saving your custom condition, make sure to define the condition output using the appropriate parameter names.
9. Your new page variant is now available in the list of variants in the left pane. You can now [edit the variant](https://doc.sitecore.com/sai/en/users/sitecoreai/personalize/edit-a-page-variant.html "Edit a page variant"). When editing is complete, [publish](https://doc.sitecore.com/sai/en/users/sitecoreai/build-pages/publishing-pages-and-referenced-items/move-an-item-through-the-workflow.html "Move an item through the workflow") the variant.

SitecoreAI shows the variant to visitors who match the custom condition.