4

I'm implementing a scheduled task that loops through contacts that have recently visited the site and sends over some analytics information to another service we have. The relevant parts of the code is

var contactRepository = new ContactRepository();
var contact = contactRepository.LoadContactReadOnly(contactId);
var keyBehaviorCache = contact.GetKeyBehaviorCache();

I'm getting an exception on the call to GetKeyBehaviorCache() that says contact.Attachments should contain key KeyBehaviorCache. Looking at the decompiled code, it's checking the contact for an attachment called KeyBehaviorCache that indeed doesn't exist on the contact.

Assert.IsTrue((contact.Attachments.ContainsKey("KeyBehaviorCache") ? 1 : 0) != 0, "contact.Attachments should contain key {0}", (object) "KeyBehaviorCache");

Looking at sitecore's documentation, I see that the key behavior cache isn't accessible during aggregating and reporting but only on a content delivery instance. That seems a little big vague to me. I'm currently running a single instance on my local machine.

Is this something that isn't possible to do from a scheduled task or has anyone done this successfully?

We're currently running version 8.2 (rev. 170728).

  • What type of data you need from the contact? – Hishaam Namooya Sep 20 '18 at 7:39
  • I'm trying to get the completed goals, triggered campaigns, engagement value, and any profile cards associated with the contact. – Drew Sep 20 '18 at 14:25
  • Please see answer below – Hishaam Namooya Sep 20 '18 at 20:07
3

Since the user is not in a session, you will need to load the contact behavior cache using the extension LoadKeyBehaviorCache. A code snippet is provided below.

public void GetContactInfo(string contactId)
{
    var contactGuid = Guid.Parse(contactId);

    var contactManager = Sitecore.Configuration.Factory.CreateObject("tracking/contactManager", true) as ContactManager;

    var contact = contactManager.LoadContactReadOnly(contactGuid);

    contact.LoadKeyBehaviorCache();

    var behaviorCacheResults = contact.GetKeyBehaviorCache();

    //------------------------------------------------------//

    var historicalDataResults = contact.LoadHistorycalData(1).ToList();
}

Based on your comment on the question, you want to access

  • completed goals
  • triggered campaigns
  • engagement value
  • Any profile cards associated with the contact.

You will need to use both the Historical and Behavior Cache. Below is a classification of where you can obtain the data you require.

Behavior Cache

  • completed goals
  • triggered campaigns

Historical Data

  • engagement value
  • Any profile cards associated with the contact

Additional Information

In the Behavior Cache, you can retrieve all the below data

Channels, Campaigns, Custom Values, Goals, Outcomes, Page Events, Venues

In the historical data, you can retrieve more information such as

SiteName, Pages, Referrer etc

You can dotpeek the Sitecore.Analytics.dll and navigate to the namespace Sitecore.Analytics.Tracking.IInteractionData. You will see all the properties that you can use to get additional information from the contact.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.