HaikoFalk
Software Engineer

Sitecore Analytics with Custom Facets


Requirements

Sitecore Version:
  1. 8.1
  2. possibly late 8

Needs Modules:
  1. Analytics
  2. xDB

When tracking a visitor, Sitecore can collect all kinds of information. Some frequently tracked information about the visitor are their email, their first and their last name. The goal for the marketing guys is to know as much as possible about the visitor. Sitecore saves all information available about the visitor in the so called contact.

But this is a developer article!

So if the marketing guys want to save more than the standard you might need to know how to save them at the contact, too. Another use case for custom facets is, to save user preferences in a portal page, preferred language for e-mail notifications, and so on. Don’t save it in cookies, viewstate, custom Userprofiles or local storage – use Sitecore Analytics. This way the solution is cross-device capable.

Reading and Writing Facets

The basics about contacts and facets are described in the sitecore documentation. Don't forget to check the Tracker and the Contact not to be null. If Mongo DB is offline the site shouldn't crash ;)

if ((Tracker.Current != null) && (Tracker.Current.Contact != null))
{
 try
 {
  Contact contact = Tracker.Current.Contact;
  var personal = contact.GetFacet("Personal");
  personal.FirstName = "Haiko";
  personal.Surname = "Falk";
  personal.JobTitle = "Senior Software Engineer";
 }
 catch (FacetNotAvailableException ex)
 {
  Log.Warn("The 'Personal' facet is not available.", ex);
 }
}

Implementing custom Facets

Like almost everything else in Sitecore, facets can be customized. To achieve this, you need to implement several things:

The basic interface using System;
using Sitecore.Analytics.Model.Framework;
namespace MysitecoreProject.Facets
{
 public interface IPreferredLanguage : IFacet
 {
  string PreferredSitecoreLanguage { get; set; }
 }
}

A getter/setter classy

The interface alone does nothing – you also need something to use it. For this, you need to create a class with a getter and setter.

Please note: The class must be serializable!

using System;
using Sitecore.Analytics.Model.Framework;
namespace MysitecoreProject.Facets
{
 [Serializable]
 public class PreferredLanguage : Facet, IPreferredLanguage
 {
  private const string PREFERRED_LANGAUGE = "FacetFieldPreferredLanguage";
  public PreferredLanguage()
  {
   EnsureAttribute(PREFERRED_LANGAUGE);
  }
  public string PreferredSitecoreLanguage
  {
   get
   {
    return GetAttribute(PREFERRED_LANGAUGE);
   }
   set
   {
    SetAttribute(PREFERRED_LANGAUGE, value);
   }
  }
 }
}

Config File

I prefer to use a standalone config file in the INCLUDE folder. There, you can patch the model/elements and model/entities/contact/facets in:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
  <model>
   <elements>
    <element interface="MysitecoreProject.Facets.IPreferredLanguage, MysitecoreProject.WebLogic" implementation="MysitecoreProject.Facets.PreferredLanguage, MysitecoreProject"> </element>
   </elements>
   <entities>
    <contact>
     <facets>
      <facet name="PreferredLanguage" contract="MysitecoreProject.Facets.IPreferredLanguage, MysitecoreProject">
     </facets>
    </contact>
   </entities>
  </model>
 </sitecore>
</configuration>

Using the Custom Facet

Now you are ready to use your new custom facet:

private static void SetPreferredLanguage(Sitecore.Analytics.Tracking.Contact contact)
{
 var preferredLang = contact.GetFacet("PreferredLanguage");
 if (string.IsNullOrEmpty(preferredLang.PreferredSitecoreLanguage))
 {
  preferredLang.PreferredSitecoreLanguage = Sitecore.Context.Language.ToString();
 }
}

Where to find the facet in the mongo DB?

You can find the facet in the analytics database in the mongo DB.

Sitecore Analytics Facets

 

 

Kommentare
Es wurden noch keine Kommentare zu diesem Eintrag geschrieben.
Kommentar hinzufügen
Vor und Zuname
E-Mail
E-Mail bei weiteren Kommentaren
Mein Kommentar