2

What i want to do:

  • Save and update facet data repeatedly through WebApi
  • Read the facet data through WebApi

Everything works fine, until the contact stops being updated. It seems as if, once the inital flush to xDB has happened I will forever retrieve old data and it wont update either.

Saving the data, i'm doing this.

    public IHttpActionResult SaveContact([FromBody] dataFromView)
    {
        if (Sitecore.Analytics.Tracker.Current != null && data != null)
        {
            var contact = Sitecore.Analytics.Tracker.Current.Contact;
            if (contact != null)
            {

                ICalculatorData customfacet = Tracker.Current.Contact.GetFacet<ICustomFacet>("NameOfCustomFacet");


                if (customfacet != null)
                {                        
                   customfacet.someproperty = dataFromView.updatedproperty;                      
                };
            }                
            return Ok();
        }

        return BadRequest();
    }

The facet:

[Serializable]
    public class CustomFacet: Facet, ICustomFacet
    {      

        private const string SOMENUMBER= "SomeNumber";

        public decimal SomeNumber
        {
            get { return base.GetAttribute<decimal>(SOMENUMBER); }
            set { base.SetAttribute<decimal>(SOMENUMBER, value); }
        }


        public CustomFacet()
        {

            base.EnsureAttribute<decimal>(SOMENUMBER);
        }

    }

ICustomFacet

public interface ICustomFacet : IFacet
    {
 decimal SomeNumber { get; set; }
    }

I take the same approach when reading the data, only i'm just returning the customFacet instead.

If I clear my cookies and retrieve a new SC_ANALYTICS_GLOBAL_COOKIE id, i'll be able to again save and update the data. Until it keeps returning old data.

Edit: Log Exception: Sitecore.Analytics.Exceptions.ContactLockException Message: Failed to extend contact lease for contact 5d3ad09a-76af-4a3d-98b9-b448d4e3be14

This is happening on my development environment, working as both CM and CD

Anyone who can shed some light onto this issue?

  • Can you tell me if the contact ID is changing from request to request. You can find that in this property. Sitecore.Analytics.Tracker.Current?.Contact?.ContactId. Also are you identifying the user? And one you update facet, is it right. But after you come back again, its old data? – Chris Auer Sep 4 '18 at 6:18
  • That was my first thought aswell. But no it doesn't change. It reads it straight out of the cookie ( as it should ). Yes, the first few times I update it come back right. But then it seems to be stuck with whatever is written in xDB – Kristian Nissen Sep 4 '18 at 6:30
  • Anything in the logs? If you create a controller that executes Session.Abandon(); it will push to xDB right away. Then you can see if there is an error causing the data to not be written back to xDB. Can you post your facet. Are all the classes decorated as [Serializable]? – Chris Auer Sep 4 '18 at 6:33
  • Nothing in the logs of any interest. My facet class is decorated [Serializable] and is just a flat class with a few properties on it. I've updated the answer with the facet – Kristian Nissen Sep 4 '18 at 23:58
  • 1
    Do you have a lot of API calls on your page? I honestly didn't know about this setting, Looking deeper into it. There is a bug fix in 8.2 Update 7 for it. dev.sitecore.net/Downloads/Sitecore%20Experience%20Platform/82/… – Chris Auer Sep 5 '18 at 1:15
2

Now that we know your issue is that you are hitting the MaxPageIndexThreshold limit. And that this is because of the API calls. You can find a nice way to make API calls be ignored from analytics, with a bit of code from Habitat. It's just an attribute for you controller actions. This will kill your API calls from being logged in xDB. When you API calls are in as "page" views, it trashes you path analyzer. Since there are hundreds of calls that do not actually correlate to the user's journey.

[HttpGet]
[SkipAnalyticsTrackingApi]
public ActionResult GetFooter()
{

}

This is the attribute

namespace Sitecore.Foundation.SitecoreExtensions.Attributes
{
    using System.Web.Mvc;
    using Sitecore.Analytics;

    public class SkipAnalyticsAjaxTrackingAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest() && Tracker.IsActive)
            {
                Tracker.Current?.CurrentPage?.Cancel();
            }
        }
    }
}
  • 1
    Ah! I like this solution a lot more than the one I just implemented. Was extending the pipeline and ignoring tracking of urls contained in a customSettings config. – Kristian Nissen Sep 5 '18 at 1:45
1

As i'm using WebApi2 and not the one build into Sitecore, I had to modify Chris Auer's answer a bit to use the using System.Web.Http.Filters; instead of System.Web.Mvc. Here's the result.

using Sitecore.Analytics;
using System.Linq;
using System.Web.Http.Filters;

public class SkipAnalyticsAjaxTrackingAttribute : ActionFilterAttribute
{

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        var isAjax = false;
        if (actionExecutedContext.Request.Headers.Contains("X-Requested-With"))
        {
            var requestedWith = actionExecutedContext.Request.Headers.GetValues("X-Requested-With").FirstOrDefault();
            if(!string.IsNullOrEmpty(requestedWith)){

                isAjax = requestedWith.Equals("XMLHttpRequest");

            }
        }               
        if (isAjax && Tracker.IsActive)
        {
            Tracker.Current?.CurrentPage?.Cancel();
        }
        base.OnActionExecuted(actionExecutedContext);
    }
}

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.