Saturday, March 11, 2017

Sitecore accessing custom contact facets

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


Setting the facet data

For this example, a test aspx web page has been created which identifies a contact, and enters data into each of the fields on the newly created contact facets.
using ContactFacet.Custom;
using Sitecore.Analytics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ContactFacet
{
    public partial class GetFacet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var identifyUser = IdentifyUser(); // Identify the user

            if (identifyUser)
            {
                var contact = Sitecore.Analytics.Tracker.Current.Contact;
                ICustomDataFacet facet = contact.GetFacet<ICustomDataFacet>(CustomDataFacet.FacetName); // Get the facet

                if (facet != null)
                {
                    facet.ActiveCustomer = true;
                    facet.CrmId = "1234567A";

                    // Product Purchases
                    var productPurchasesField = facet.ProductPurchases.Create();
                    productPurchasesField.ProductId = "PID1";
                    productPurchasesField.PurchaseDate = DateTime.Now;

                    var productPurchasesField2 = facet.ProductPurchases.Create();
                    productPurchasesField2.ProductId = "PID3";
                    productPurchasesField2.PurchaseDate = DateTime.Now;
                }
            }

            Session.Abandon(); // Push data through to MongoDB
        }

        private bool IdentifyUser()
        {
            var identifiers = Sitecore.Analytics.Tracker.Current.Contact.Identifiers;

            if (identifiers.IdentificationLevel != ContactIdentificationLevel.Known)
            {
                Sitecore.Analytics.Tracker.Current.Session.Identify("test@email.com"); // Statically bound email :)
                return true;
            }

            return false;
        }
    }
}
It's relatively simple, the flow of the code is as follows:
  1. Identify the user by an email address - this data can be saved to anonymous contacts who can be identified at a later stage.
  2. Get the custom contact facet
  3. Populate the simple fields on the facet (active customer and CRM ID).
  4. Each of the product purchase elements are instantiated via the create method - remember that this type only had a get and not set inside the interface.
  5. The session is abandoned to push the data through to MongoDB - this is for testing purposes and in the real world would happen 20 minutes after the users last interaction with the Sitecore website. 
Now we can use a tool such as Robomongo to query MongoDB and ensure that our data has been saved. The query that can be used is: db.getCollection('Contacts').find({"Identifiers.Identifier":"test@email.com"}).

Robomongo showing the custom contact facet data


Conclusion

In the example above we show how to set the data for a custom contact facet in Sitecore. Accessing the data is covered in part 3, where the Experience Profile is updated to display the custom facet data.

No comments:

Post a Comment