Single node Facets in the xDB database

Single node Facets in the xDB database

xDB Factets

The xDB Mongo database is intended to collect all the data you need for each client that visits your Sitecore site. Out of the box it collects a lot of data about what pages the user clicked on, how long they were on the page, who they are, etc...

But the true power of the xDB is collecting data that is important to your organization. What videos did a user view, how long did they watch the video, what colors did the user visit the most in leather couches.

This data is stored in xDB as facets. And the facets can be shaped in several ways depending on what data you are trying to track. Below I will show you have to create a single node facet. This would be data that is only assigned once to a contact. Like the contacts name, or their business, etc...

Single node

To create a single node facet it takes a few simple steps.

  • Create an interface of the facet
  • Create a concrete implementation of the interface you created
  • Register your facet with Sitecore via a config entry
  • Add an entry to your contact's record.

For our example we are going to use an example of capturing company contact information we are getting from a customer.

The interface

The interface is a simple interface, just make sure it inherits from IFacet.

The concrete model

Next create a serializable concrete model of your interface inheriting from Facet and your interface.

The config

Finally we tie it all together with some config entries. In this config we are defining our element CompanyLookupModel and defining it as a facet called CompanyData

The code

At this point Sitecore knows our facet called CompanyData. And it knows it is based on our interface ICustomerLookup. All we need to do is get the facet from the user's tracker and push data into it. Thats it.

Reading the facet

Reading the data is as simple as putting the data in. Just get the facet off the Tracker by name/Interface. This is persistent data for the contact. As soon as the user's session ends, the data is pushed into xDB. When the user visits the website again, the data is pulled from xDB and pushed into the users Tracker again. So its always available.