Is this really the version you are looking for? Kentico Xperience 12 Service Pack Documentation is no longer updated. Use the switcher in the header to choose your product version.
Kentico EMS required
Features described on this page require the Kentico EMS license.
This page describes two ways in which you can improve macro performance in scoring and contact groups:
Default macro rules
Macro rules available in Kentico by default have both these optimizations implemented out of the box.
MVC sites
For sites built using the MVC development model, add the customizations to your Kentico administration project (not the MVC live site project). The custom classes are used when recalculating scores and contact groups, which is done by the Kentico application.
Create a class that implements the IMacroRuleInstanceTranslator interface.
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.
using System;
using CMS.DataEngine;
using CMS.Membership;
using CMS.ContactManagement;
internal 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");
}
var ruleParameters = macroRuleInstance.Parameters;
// ruleParameters contains the data users 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);
}
}Register an instance of the MacroRuleMetadata class by calling the MacroRuleMetadataContainer.RegisterMetadata method.
To register the class at the beginning of the application's lifecycle, create a custom module class and override the OnInit method.
Add the module class into a custom Class Library project within the Kentico solution.
using System.Collections.Generic;
using CMS;
using CMS.Base;
using CMS.DataEngine;
using CMS.ContactManagement;
// Registers the custom module into the system
[assembly: RegisterModule(typeof(CustomInitializationModule))]
public class CustomInitializationModule : Module
{
// Module class constructor, the system registers the module under the name "CustomInit"
public CustomInitializationModule()
: base("CustomInit")
{
}
// Contains initialization code that is executed when the application starts
protected override void OnInit()
{
base.OnInit();
MacroRuleMetadata metadata = new MacroRuleMetadata(
"ContactIsFemaleMacro",
new ContactIsFemaleInstanceTranslator(),
affectingActivities: new List<string>(0),
affectingAttributes: new List<string>(0));
MacroRuleMetadataContainer.RegisterMetadata(metadata);
}
}Save the files and rebuild your solution.
Important: Note that this way, the recalculation is not performed for any contact activity or attribute change. You can make the rule recalculate for a specific set of activities and attribute changes.
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.
Compound conditions
All macro rules used in a contact group or scoring macro conditions need to be translated. Otherwise, the evaluation will be performed in a non-optimized way.
Another way of improving your site performance is by making sure that contact groups and scores are recalculated only when necessary. You can make sure your custom contact group and scoring macro rules are only recalculated for specified activities or when specific contact attributes change.
Examples:
To specify the activities and attribute changes that rebuild your custom macro rule:
Create a custom macro rule translator.
Extend the metadata instantiation by the following parameters:
IList<string> affectingActivities – a list of activities for which you want the macro to rebuild.
IList<string> affectingAttributes – a list of attributes for which you want the macro to rebuild.
protected override void OnInit()
{
...
MacroRuleMetadata metadata = new MacroRuleMetadata(
"CustomMacroRule",
new CustomMacroRuleTranslator(),
affectingActivities: new List<string>
{
// Recalculate for a predefined activity
PredefinedActivityType.PAGE_VISIT,
// Recalculate for a custom activity
"custom_activity_codename"
}
affectingAttributes: new List<string>
{
// Recalculate when specific attributes change
"contactgender",
"contactcountryid"
});
MacroRuleMetadataContainer.RegisterMetadata(metadata);
...
}protected override void OnInit()
{
...
var noNeedToRecalculate = new List<string>(0);
MacroRuleMetadata metadata = new MacroRuleMetadata(
"CustomMacroRule",
new CustomMacroRuleTranslator(),
affectingActivities : noNeedToRecalculate,
affectingAttributes : noNeedToRecalculate
);
MacroRuleMetadataContainer.RegisterMetadata(metadata);
...
}protected override void OnInit()
{
...
MacroRuleMetadata metadata = new MacroRuleMetadata(
"CustomMacroRule",
new CustomMacroRuleTranslator(),
affectingActivities: new List<string>
{
MacroRuleMetadata.ALL_ACTIVITIES
},
affectingAttributes: new List<string>
{
MacroRuleMetadata.ALL_ATTRIBUTES
});
MacroRuleMetadataContainer.RegisterMetadata(metadata);
...
}Note: You can also combine the parameters as you see fit.
Save the files and rebuild your solution.
Now, only the specified contact activities or changes in contact attributes will trigger a recalculation of the custom macro rule.