Create and configure XDB Custom Facet

In this post I am describing the required steps to create a custom facet on XDB and how to use it to personalize the experience…

  • Create your Interface extending IFacet
using System;
using System.Linq;
using Sitecore.Analytics.Model.Framework;

namespace Testing.ContactFacets.Model
{
    public interface ICRMSegment : IFacet
    {
        string CRMSegmentId { get; set; }
    }
}
  • Implement your Custom Facet
namespace Testing.ContactFacets.Model
{
    public class CrmSegment : Facet, ICRMSegment
    {
        private const string FIELD_CRMSegmentId = "CRMSegmentId";
        public CrmSegment()
        {
            base.EnsureAttribute< string>(FIELD_EMPLOYEE_ID);
        }
        public string CRMSegmentId
        {
            get { return base.GetAttribute< string>(FIELD_CRMSegmentId ); }
            set { base.SetAttribute< string>(FIELD_CRMSegmentId , value); }
        }
    }
}
  • Register your Facet via config file
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <model>
      <elements>
        <element interface="Testing.ContactFacets.Model.ICRMSegment,XXX.AssemblyName " implementation="Testing.ContactFacets.Model.CrmSegment, XXX.AssemblyName" />
      </elements>
      <entities>
        <contact>
          <facets>
            <facet name="CRMSegment" contract="Testing.ContactFacets.Model.ICRMSegment, XXX.AssemblyName" />
          </facets>
        </contact>
      </entities>
    </model>
  </sitecore>
</configuration>

  • Create a new XDB Contact
@using Sitecore.Web;
@using Testing.ContactFacets.Model;
@using Sitecore.Analytics.Model.Entities;
@{
 Sitecore.Analytics.Tracker.Current.Session.Identify("stelio.dibello@xxxxx.com");
 var contact =  Sitecore.Analytics.Tracker.Current.Contact;
 var data = contact.GetFacet<ICRMSegment>("CRMSegment");
 data.CRMSegmentId = "BargainHunter";

var personalInfo = Sitecore.Analytics.Tracker.Current.Contact.GetFacet<Sitecore.Analytics.Model.Entities.IContactPersonalInfo>("Personal");

personalInfo.FirstName = "Stelio";
personalInfo.Surname = "Di Bello";
personalInfo.Gender="M";

var contactEmail =  Sitecore.Analytics.Tracker.Current.Contact.GetFacet<IContactEmailAddresses>("Emails");

            // this email to show in the Experience Profiles backend.
 if (!contactEmail.Entries.Contains("Home"))
    {
      contactEmail.Entries.Create("Home");
    }
            // set the email
  var email = contactEmail.Entries["Home"];
  email.SmtpAddress = "stelio.dibello@xxxxx.com";
 contactEmail.Preferred = "Home";
}

CRM data contact facet updated.

Contact ID: @contact.ContactId.ToString() /b>

Employee #: <b> @data.CRMSegmentId </b>

  • Personalize based on CRMSegmentId rule

 

  • crmsegmentpersonalisation

Sitecore 8 Personalisation rules for beginners

Probably most of you are already aware of what it means personalisation in Sitecore 8 but in case you have not heard about it, this post will give you a better idea of how Sitecore has implemented it…

Within Sitecore the easier way to deliver a Personalised experience is through Personalisation Rules at Rendering level.

The starting point is defining a page with one rendering and click on the Personalise button

personalisebutton

From here you will be able to select your rule:

rulesselector

Once that you have selected your rule, you will than be able to select the actions associated to the conditional rendering that could be setting a different datasource, or Hiding the rendering or displaying a different rendering

personalisecomponent

Sitecore 8 allows the Developer to customise Rules but OOTB it comes with the following rules to personalise your content:

Sitecore Rules are grouped in the following logical categories:

Campaigns: Condition based on the campaign defined, newsletter, landing page, etc
Channel: Condition based on the channel, web, mobile etc
Date: Condition based on the date & time of the visit
Device: Condition based on Device capability (it rely on NetBiscuit service charged separately)
GeoIp: Condition based on user geolocation (rely on Sitecore service based on MaxMind, charged separately
Outcomes: Condition based on the engagement plan status and outcomes triggered
Security: Condition based on user security context, eg.  is anonymous etc
Social : Conditions based on Social network information, in the case the user has a federated identity via social network can be used to personalised on the profile information
Visit: Condition based on the current visit (eg. goal / event, campaign, previous interactions, engagement value, search keyword etc)
Visitor: Condition based on previous interactions (require analytics to be enabled)

 

 

Trigger an Outcome Programmatically

As you probably already know, the main difference between an outcome and a goal, is the possibility to assign a monetary value to an outcome so that you can use it to get better reporting within your analytics and eventually to understand which paths/content converted better.

Outcomes can only be assigned programmatically, this snippet simulate a controller rendering that trigger the ProductPurchase Outcome.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sitecore.Mvc;
using System.Web.Mvc;
using System.Web;
using Sitecore.Analytics.Outcome.Extensions;
using Sitecore.Analytics.Outcome.Model; 

public ActionResult PurchaseOutcome()
    {
        // Register an outcome
        //It will only be saved to the database on session end

        var id = Sitecore.Data.ID.NewID;
        var interactionId = Sitecore.Data.ID.NewID;
        var contactId = Sitecore.Data.ID.NewID;

       // definition item for Purchase Outcome
      var definitionId = new Sitecore.Data.ID("{9016E456-95CB-42E9-AD58-997D6D77AE83}");

        var outcome = new ContactOutcome(id, definitionId, contactId);
        outcome.MonetaryValue = 153;
        Sitecore.Analytics.Tracker.Current.RegisterContactOutcome(outcome);
        return Content("Outcome purchase 153");
    }

These are the references required:

referencesoutcome

And this is the Outcome definition:

outcome

XDB Contact Rules to personalize based on the facet value

As you are probably familiar, Sitecore 8 architecture is all around XDB and the Contacts.

Arguably Sitecore forgot to ship OOTB personalisation rules based on the Contact facet value of the contacts stored in XDB

Luckily Adam Conn has created two projects

https://github.com/adamconn/sitecore-adaptive-rules

https://github.com/adamconn/sitecore-contact-utilities

to deliver almost OOTB personalisation based on Contact Facet conditions

Once that you have installed Adaptive rules and Contact Utilities you will be able to personalise based on Contact Facets…. In this example I personalise the content based on the Gender of the Contacts

genderpersonalisation

Personal Facets

personalfacet

 

Sitecore CMS only mode on 8.1

As you probably now, Sitecore 8.1 can run in CMS only mode that means that your Sitecore instance can live also without MONGODB architecture…

CMS only mode, disable all the features that require MONGODB like Analytics, behaviour personalisation, MVT but it still allow you to run inSession personalisation rules or execute personalisation rules that do not require MongoDB (eg. geoLocation personalisation)

In order to run Sitecore in CMS only mode you need to set the following parameters:

  1. Xdb.Enabled
  2. Xdb.Tracking.Enabled
  3. Xdb.Configuration.XdbSettings

Further documentation on CMS only mode can be found here:

https://doc.sitecore.net/sitecore_experience_platform/setting_up__maintaining/experience_management/experience_management_an_overview