Saturday, March 11, 2017

Sitecore creating custom contact facets

This post is part 1 of a series, the introduction is available here and includes a table of contents, along with the full source code.


The data structure


The facet will be named CustomData.
  1. CRM ID - string
  2. Active Customer - boolean
  3. Product Purchases - List of custom object
    1. Product ID - string
    2. Purchase Date - datetime

The interface

The first step is to create the interface for the custom contact facet. The primary object is the facet and this can contain many elements.
using Sitecore.Analytics.Model.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContactFacet.Custom
{
    public interface ICustomDataFacet : IFacet
    {
        string CrmId { get; set; }
        bool ActiveCustomer { get; set; }
        IElementCollection<IProductPurchaseElement> ProductPurchases { get; }
    }

    public interface IProductPurchaseElement : IElement
    {
        string ProductId { get; set; }
        DateTime PurchaseDate { get; set; }
    }
}
Any facet field which is a list of an object (even a list of single strings) must be based on IElementCollection with an IElement.

The implementation

The next step is to create the implementation of the interface. You will notice that the classes are all serializable.
using Sitecore.Analytics.Model.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContactFacet.Custom
{
    [Serializable]
    public class CustomDataFacet : Facet, ICustomDataFacet
    {
        public static readonly string FacetName = "CustomData";
        private const string _PRODUCT_PURCHASES_NAME = "ProductPurchases";
        private const string _CRM_ID_NAME = "CrmId";
        private const string _ACTIVE_CUSTOMER_NAME = "ActiveCustomer";

        public CustomDataFacet()
        {
            EnsureCollection<IProductPurchaseElement>(_PRODUCT_PURCHASES_NAME);
        }

        public IElementCollection<IProductPurchaseElement> ProductPurchases
        {
            get
            {
                return GetCollection<IProductPurchaseElement>(_PRODUCT_PURCHASES_NAME);
            }
        }

        public bool ActiveCustomer
        {
            get
            {
                return GetAttribute<bool>(_ACTIVE_CUSTOMER_NAME);
            }
            set
            {
                SetAttribute(_ACTIVE_CUSTOMER_NAME, value);
            }
        }

        public string CrmId
        {
            get
            {
                return GetAttribute<string>(_CRM_ID_NAME);
            }
            set
            {
                SetAttribute(_CRM_ID_NAME, value);
            }
        }
    }

    [Serializable]
    public class ProductPurchaseElement : Element, IProductPurchaseElement
    {
        private const string _PRODUCT_ID = "ProductId";
        private const string _PURCHASE_DATE = "PurchaseDate";

        public ProductPurchaseElement()
        {
            EnsureAttribute<string>(_PRODUCT_ID);
            EnsureAttribute<DateTime>(_PURCHASE_DATE);
        }

        public string ProductId
        {
            get
            {
                return GetAttribute<string>(_PRODUCT_ID);
            }
            set
            {
                SetAttribute(_PRODUCT_ID, value);
            }
        }

        public DateTime PurchaseDate
        {
            get
            {
                return GetAttribute<DateTime>(_PURCHASE_DATE);
            }
            set
            {
                SetAttribute(_PURCHASE_DATE, value);
            }
        }
    }
}

Registering the contact facet in Sitecore

Now that the facet has been defined, we need to register it inside the Sitecore.Analytics.Model.config file. The following XML file will achieve this.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <model>
      <elements>
        <element interface="ContactFacet.Custom.IProductPurchaseElement, ContactFacet" implementation="ContactFacet.Custom.ProductPurchaseElement, ContactFacet" />
        <element interface="ContactFacet.Custom.ICustomDataFacet, ContactFacet" implementation="ContactFacet.Custom.CustomDataFacet, ContactFacet" />
      </elements>
      <entities>
        <contact>
          <facets>
            <facet name="CustomData" contract="ContactFacet.Custom.ICustomDataFacet, ContactFacet" />
          </facets>
        </contact>
      </entities>
    </model>
  </sitecore>
</configuration>

Conclusion

That's it, the custom contact facet is now available to be accessed for a given site browser (identified or anonymous contact).Part two: Sitecore accessing custom contact facets will go over the steps to access this facet at a contact level.

No comments:

Post a Comment