Dmgdotnet's Blog

Sitecore and stuff

Aggregated Personalisation – Part 1

Posted by dmgdotnet on October 30, 2012

A feature of Sitecore’s DMS that is often misunderstood is the personalisation rules around the visitors session. It is often assumed that the default behaviour is for DMS to accumulate scores for all visits and not just for the current one, this is simply not the case.  If you visit a site running personalisation and you exhibit behaviour as, for example, a landscape photographer, the site may respond by giving you personalised content targeted at your demographic. If you close the browser and return to the site, despite being recognised as a returning visitor, your personalisation experience will start again as though it is your first visit.

I am hoping to put together a series of posts, with hopefully some community help (:, outlining some approaches that can be taken to aggregate different kinds of personalisation.

Profiling

For the first post we will look into aggregating profiling scores.  Some quick disclaimers, at the time of writing:

  1. THIS HAS NOT BEEN LOAD TESTED
    Obviously the major concern with these customisations is the affect on performance. I have not had a chance to load test this AT ALL so please approach this with caution if you plan to use any aggregation approaches in your solution. I will hopefully have a chance to set up a testing environment in the near future to provide a better understanding of the impact of these changes.
  2. These code samples have been put together quickly so if you have a better idea or can see any issues, please point them out and I will update.

Onto the example; for this I have used Sitecore 6.5 which has a different API to the older OMS version meaning this will need modification to run on the OMS. My first step was to use dotPeek to take a look at the existing profile condition “Sitecore.Analytics.Rules.Conditions.ProfileCondition”.   The method we want to change is “GetProfileKeyValue” which is private so we will also need to override the “Execute” method in this case. I’ve added an additional property called “Visits” that will allow the author/developer to set how many visits we wish to include where zero means all visits (CAUTION!!).

        private double GetProfileKeyValue()
        {
            if (string.IsNullOrEmpty(ProfileKeyId))
                return 0.0;
            var obj = Tracker.DefinitionDatabase.GetItem(ProfileKeyId);
            if (obj == null)
                return 0.0;
            var parent = obj.Parent;
            if (parent == null)
                return 0.0;
            var name1 = obj.Name;
            var visits = int.Parse(Visits);

            var index = Tracker.Visitor.GetOrCreateCurrentVisit().VisitorVisitIndex;
            Tracker.Visitor.Load(new VisitorLoadOptions
                {
                    Start = visits == 0 || index - visits < 0 ? 0 : index - visits,
                    Count = visits == 0 || index + 1 < visits ? index + 1 : visits,
                    VisitLoadOptions = VisitLoadOptions.Profiles,
                    Options = VisitorOptions.None                 
                }
             );             
            return Tracker.CurrentVisit.Profiles.Sum(v => v.GetValue(name1));
        }

To get this working was a very small change in the end.  Firstly I had to modify the VisitorLoadOptions to return more than a single record.  We set ‘Start’ to the appropriate index based on the condition configuration and how many times the current user has visited the site.  We set the ‘Count’ to the number of rows to return and the rest of the options remain the same.  From here just need to sum the values for the given profile to get our aggregated profile score.

The only thing that is left is to create the content item that will reference our new class. It’s easiest just to copy the existing Profile Condition from ‘/sitecore/system/Settings/Rules/Conditional Renderings/Conditions/Profiles and Patterns/Profile Condition’ and update the Type field to your new class.

sitecore content tree

Location of new aggregated profile condition in the content tree

We also need to add in a token to allow users to set the value of our ‘Visits’ property in our class (outlined in red).

aggregated profile condition

With this in place we can set a personalisation rule that will aggregate profile scores across the specified number of visits for our current user.

rule editor

Leave a comment