---
title: Improving custom macro performance in scoring and contact groups
related:
  - https://docs.kentico.com/k81/on-line-marketing-features/contact-management/contact-segmentation.md
  - https://docs.kentico.com/k81/on-line-marketing-features/contact-management/scoring-contacts.md
  - https://docs.kentico.com/k81/macro-expressions.md
  - https://docs.kentico.com/k81/macro-expressions/writing-macro-conditions/creating-macro-rules.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

This topic describes two ways in which you can improve [macro](https://docs.kentico.com/k81/macro-expressions.md) performance in scoring and contact groups:

1. [Create a custom macro rule translator](#creating-a-custom-macro-rule-translator) - by default, [custom macro](https://docs.kentico.com/k81/macro-expressions/extending-the-macro-engine/registering-custom-macro-methods.md) rules that you use in scoring and contact group conditions evaluate individually for each contact. You can improve the recalculation performance by implementing and registering translators for these macro rules. Once a macro rule has a translator registered, a database query is constructed that then selects only the contacts that fit the given macro condition.
2. [(Contact groups) Limit unnecessary recalculations](#recalculating-contact-group-rules-only-for-specific-activities) - you can further improve your site performance by making sure your [custom](https://docs.kentico.com/k81/macro-expressions/extending-the-macro-engine/registering-custom-macro-methods.md) contact group macro rules are recalculated only when users perform activity types that are relevant to the macro rule.

> **Info:** **Default macro rules**
>
> Macro rules available in Kentico by default have both these optimizations implemented out of the box.

## Creating a custom macro rule translator

1. Create a class that implements the _IMacroRuleInstanceTranslator_ interface.
2. Implement the logic of the macro rule in a method that returns a query of the _ObjectQuery\<ContactInfo>_ type. The returned object needs to contain the contacts that fulfill the condition. Note that you do not have to handle contextual information, such as site context, in the query.

   ```csharp title="Example of an implemented ContactIsFemaleMacro rule translator"

   using CMS.Base;
   using CMS.DataEngine;
   using CMS.MacroEngine;
   using CMS.Membership;
   using CMS.OnlineMarketing;

   class ContactIsFemaleInstanceTranslator: IMacroRuleInstanceTranslator
   {
       /// <summary>
       /// Translates ContactIsFemaleMacro rule.
       /// </summary>
       public ObjectQuery<ContactInfo> Translate(MacroRuleInstance macroRuleInstance)
       {
           if (macroRuleInstance == null)
           {
               throw new ArgumentNullException("macroRuleInstance");
           }
           if (macroRuleInstance.MacroRuleName != "ContactIsFemaleMacro")
           {
               throw new ArgumentException("[ContactIsFemaleTranslator.Translate]: Only macro rule instances of type ContactIsFemaleMacro can be translated");
           }
           // ruleParameters["_is"] accesses the data contacts enter in macro fields
           string paramIs = ruleParameters["_is"].Value;
           QueryOperator queryOperator = paramIs == "!" ? QueryOperator.NotEquals : QueryOperator.Equals;

           return ContactInfoProvider.GetContacts().Where("ContactGender", queryOperator, (int)UserGenderEnum.Female);
       }
   }

   ```
3. Register an instance of the _MacroRuleMetadat&#x61;_&#x63;lass through the MacroRuleMetadataContainer.RegisterMetadata method. There are two ways in which you can register the class at the beginning of the application's lifecycle:

   - During the initialization process of the application itself — use the **CMSModuleLoader** partial class in the **App\_Code** folder.
   - When initializing [custom modules](https://docs.kentico.com/k81/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md) — override the **OnInit** method of the module class.

```csharp

protected override void OnInit()
{
    ...
    MacroRuleMetadata metadata = new MacroRuleMetadata( "ContactIsFemaleMacro" , new ContactIsFemaleInstanceTranslator());
    MacroRuleMetadataContainer.RegisterMetadata(metadata);
    ...
}

```

4. Save the files.

Now, when you use the translated macro rule in a scoring or contact group condition, the query is only performed once when you run recalculation.

> **Note:** **Compound conditions**
>
> Note that all macro rules used in a contact group or scoring macro conditions need to be translated. Otherwise, the evaluation will be performed in the non-optimized way.

## Recalculating contact group rules only for specific activities

Another way of improving your site performance is making sure that your custom contact group macro rules aren't recalculated when contacts perform activities unrelated to the macro rule.

**Examples:**

- The "ContactIsFemale" macro doesn't need to be rebuilt by any contact activity that the visitor performs on the site.
- A rule that checks whether contact is registered for an event ("CMSContactIsRegisteredForSpecifiedEvent" macro by default) only needs to be rebuilt when a visitor performs an event booking activity on the site ("EVENT\_BOOKING" activity by default).
- A rule that checks whether contact has visited a page ("CMSContactHasVisitedSpecifiedPageInLastXDays" macro by default) only needs to be rebuilt when a visitor performs an event booking activity on the site ("PAGE\_VISIT" activity by default).

**To specify the activities that rebuild your custom macro rule**

1. Create a [custom macro rule translator](#creating-a-custom-macro-rule-translator).
2. Extend the instantiation of metadata by a parameter. The parameter specifies the activity or a set of activities for which you want the macro to rebuild.

   ```csharp title="Rebuild custom macro rule for a single activity"

   protected override void OnInit()
   {
       ...
       MacroRuleMetadata metadata = new MacroRuleMetadata("ContactHasVisitedSpecifiedPageInLastXDays", new ContactHasVisitedSpecifiedPageInLastXDaysTranslator(), PredefinedActivityType.PAGE_VISIT);
       MacroRuleMetadataContainer.RegisterMetadata(metadata);
       ...
   }

   ```

   ```csharp title="Rebuild custom macro rule for a custom activity"

   protected override void OnInit()
   {
       ...
       MacroRuleMetadata metadata = new MacroRuleMetadata("ContactHasVisitedSpecifiedPageInLastXDays", new ContactHasVisitedSpecifiedPageInLastXDaysTranslator(), "custom_activity_codename");
       MacroRuleMetadataContainer.RegisterMetadata(metadata);
       ...
   }

   ```

   ```csharp title="Rebuild custom macro rule for a set of activities"

   protected override void OnInit()
   {
       ...
       MacroRuleMetadata metadata = new MacroRuleMetadata("ContactIsSubscribedToSpecifiedNewsletter", new ContactIsSubscribedToSpecifiedNewsletterTranslator(), new List<string>() {
           PredefinedActivityType.NEWSLETTER_SUBSCRIBING, 
           PredefinedActivityType.NEWSLETTER_UNSUBSCRIBING,
       });
       MacroRuleMetadataContainer.RegisterMetadata(metadata);
       ...
   }

   ```

   ```csharp title="Do not rebuild custom macro rule for any activity"

   protected override void OnInit()
   {
       ...
       MacroRuleMetadata registration = new MacroRuleMetadata("ContactIsFemaleMacro", new ContactIsFemaleInstanceTranslator(), new List<string>(0));
       MacroRuleMetadataContainer.RegisterMetadata(metadata);   
       ...
   }

   ```
3. Save the files.

Now, only the specified contact activities will trigger a recalculation of the custom macro rule.
