Search code examples
c#sitecoresitecore7sitecore-analytics

Counting visits through DMS rules in Sitecore 7 does not work


I am having an issue with a Sitecore 7 solution. I cannot get the DMS rules to work properly.

Some specific content has to be shown when a visitors visit count on the site is greater than 3. I have made a custom DMS rule in order to be able to debug the problem.

var renderingItem = ruleContext.Reference == null ? null : ruleContext.Reference.RenderingItem;

var pageEvents = Tracker.Visitor.DataContext.PageEvents;
        pageEvents =
            pageEvents.Where(y => y.ItemId == renderingItem.ID.ToGuid() && y.Data == PageEventDataName).ToArray();

var count = pageEvents.Count();

int elementVisits = Tracker.Visitor.DataContext.Pages
            .Where(x => x.ItemId == ruleContext.Item.ID.ToGuid())
            .SelectMany(x => x.PageEvents.Where(y => y.ItemId == renderingItem.ID.ToGuid() && y.Data == PageEventDataName))
            .Count();

bool itemHasReachedMaxVisitCount = !Compare(elementVisits);

if (itemHasReachedMaxVisitCount)
            return false;

The elementVisits integer is counted up fine for a short amount of time, but after a while it is reset to 0, even if the Visitor is the same. It looks like the standard DMS rule does not work either.

Sitecore visit no. DMS rule

I can see that the VisitCount column of the Visitors table in the Sitecore_analytics database is not updated.

Does anyone else have had similar problems and knows what goes wrong?


Solution

  • You can try calling the Tracker.Visitor.LoadAll() method to make sure that the visit data is pulled from memory as well as the analytics database.