1

We are pushing the customer details into xDB when they login. The problem is we may have times where more than 5k customers could be accessing the site at the same time, So I don't want updating xDB causing performance hits and we tried to update xDB asynchronously and Tracker is always NULL if we do it asynchronously. I'm loosing content if I'm on async mode. My Implementation looks like this:

public void CreateUpdatexDBProfile(CustomerDataWrapper customerData, int lastPurchaseAmount)
{
   Customer customer;
   if (customerData.Customer != null)
   {
       customer = customerData.Customer;

       //Save profile information into xDB 
       _trackerRepositoryClient.Identify(customer.CustomerId);

       ICustomerMaketingData customerMData = new CustomerMaketingData()
       {
           EmailId = customer.Email,
           FirstName = customer.FirstName,
           LastName = customer.LastName,
           CustomerId = customer.CustomerId,
           LastPurchaseAmount = lastPurchaseAmount
        };


        if (customer.DateOfBirth != null && customer.DateOfBirth.HasValue)
        {
            customerMData.Age = (DateTime.Today.Year - customer.DateOfBirth.Value.Year);
        }

        customerMData.CustomerType = customerData.customerType;                    

        //add customer profile data into xdb
         _trackerRepositoryClient.AddCustomerMarketingData(customerMData);
}
public void AddCustomerMarketingData(ICustomerMaketingData customerMData)
{
    try
    {
        AddCustomerMarketingData(customerMData);

        IContactEmailAddresses emailFacet = Tracker.Current.Contact.GetFacet<IContactEmailAddresses>(MarketingCustom.Facets.Constants.EmailFacetName);
        if (!emailFacet.Entries.Contains(MarketingCustom.Facets.Constants.PersonalEmailFacetName))
        {
            IEmailAddress email = emailFacet.Entries.Create(MarketingCustom.Facets.Constants.PersonalEmailFacetName);
            email.SmtpAddress = customerMData.EmailId;
            emailFacet.Preferred = MarketingCustom.Facets.Constants.PersonalEmailFacetName;
        }

        IContactPersonalInfo personalInfo = Tracker.Current.Contact.GetFacet<IContactPersonalInfo>(MarketingCustom.Facets.Constants.PersonalFacetName);
        personalInfo.FirstName = customerMData.FirstName;
        personalInfo.Surname = customerMData.LastName;
    }
    catch (Exception ex)
    {
        _logger.Error(ex.Message, ex);
    }
}
public void AddCustomerMarketingData(ICustomerMaketingData customerMarketingData)
{
    if(customerMarketingData != null)
    {
        if (!string.IsNullOrEmpty(customerMarketingData.CustomerId))
        {
            if (Tracker.Current == null)
            {
                Tracker.StartTracking();
            }

          Tracker.Current.Session.Identify(customerMarketingData.CustomerId);

            ICustomerMaketingData customerMData = Tracker.Current.Contact.GetFacet<ICustomerMaketingData>(MF.Constants.CustomFacetName);

            if (!string.IsNullOrEmpty(customerMarketingData.EmailId))
                customerMData.EmailId = customerMarketingData.EmailId;

            if (!string.IsNullOrEmpty(customerMarketingData.FirstName))
                customerMData.FirstName = customerMarketingData.FirstName;

            if (!string.IsNullOrEmpty(customerMarketingData.LastName))
                customerMData.LastName = customerMarketingData.LastName;

            if (!string.IsNullOrEmpty(customerMarketingData.CustomerId))
                customerMData.CustomerId = customerMarketingData.CustomerId;

            if (customerMarketingData.CustomerType != CustomerType.NoValue)
                customerMData.CustomerType = customerMarketingData.CustomerType;                    

            customerMData.LastOrderAmount = customerMarketingData.LastPurchaseAmount;

            if (customerMarketingData.Age > 0)
                customerMData.Age = customerMarketingData.Age;

        }
    }    
}

How can I update this asynchronously to xDB? Thanks!

1 Answer 1

1

I presume with async you mean you are pushing data from your frontend through a web api. Web api is by default not Sitecore context aware nor session aware. So you have to make sure you make them session aware and start the tracker manually (this is possible as webapi HTTP call should contain Sitecore cookies.

Check blogs like this to help you with snippets: xdb tracking the untrackable part 1

To start tracker manually:

if (!Tracker.IsActive)
{
    Tracker.StartTracking();
}

Use IRequiresSessionState on your web api controller handler (make it inherit from it)

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.