2

Certain actions the contact does trigger goals. The triggering of the goals should be visible in the public site via profile completion.

However, we're not seeing the triggered goals in the KeyBehaviorCache. We would have to Abandon the session for it to refresh.

After triggering the goal we call 'FlushContactToXdb2'.

How can we refresh the KeyBehaviorCache without abandoning session?

Sitecore 8.2

2 Answers 2

5

You can test if a goal has been triggered with this code. It looks in KBC and the current session.

public bool IfGoalAlreadyExists(ID goalId)
{

    // this will get all the goals that have ever happened in the past for a user. Basically all history.   
    var behaviorCache = Tracker.Current.Session.Contact.GetKeyBehaviorCache().Goals.Any(x => x.Id == goalId.ToGuid());

    // this is all the goals that have happened on all the current pages, in the current session. So basically KBC + current pages = every goal that has been fired right now, for this user.
    var goalsTriggered = Tracker.Current.Session.Interaction.Pages.SelectMany(x => x.PageEvents).Where(x => x.IsGoal).ToList();
    var pages = goalsTriggered.Any(x => x.PageEventDefinitionId == goalId.ToGuid());

    return behaviorCache || pages;
}
0
2

Key behavior cache (KBC) is read-only and is loaded when a session starts. KBC is updated when session is submitted and is not meant to store live data by design. You could try combining it with current session and check goals in both KBC and Tracker.Current.Session.

2
  • So you're saying this can't be done using the KBC on its own? In that case do you mind elaborating on how to use the Tracker.Current.Session for this?
    – TamerM
    Commented Jul 18, 2018 at 15:34
  • 1
    Yes, please see Chris' answer.
    – grg
    Commented Jul 19, 2018 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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