Category Archives: Sitecore

How to: Collect Custom Tracking Data using a Custom Facet in Sitecore 8

Out of the box, Sitecore provides a number of attributes you can collect about your website visitors in order to provide a personalized experience and produce meaningful reporting. In this tutorial, we’ll look at how to collect your own custom attributes based associated with users that may not exist in Sitecore. For this tutorial, we will assume your customer just made a purchase or payment of some sort to your business and we want to track their last payment amount and date.

Sitecore Versions:

  • Sitecore 8.x

Note: The implementation for how to work with Facets changes slightly in Sitecore 9.x. I’ll be producing another post in the future on how to do this in 9.x.

Tutorial:

Step 1: Creating the Custom Facet

The first step we need to take to collect our custom user attributes is to create a new Interface and Class. Since our example is associated with a visitor’s last payment amount and date, we’ll name our Interface and Class ICustomerPaymentFacet and CustomerPaymentFacet respectively.

There are a couple of items worth noting in the above snippet. First, the implementation class must be decorated with the [Serializable] attribute. Since we will be storing this class into the xDB database, being able to serialize and deserialize the information is going to be important.

The next thing to notice is that the interface inherits from the Sitecore.Analytics.Model.Framework.IFacet interface while our class inherits from Sitecore.Analytics.Model.Framework.Facet and implements our own interface. This will be required in order to attach this information to the user’s tracking contact.

Lastly, the implementation of the properties is a bit abnormal compared to declaring properties in a normal C# object. The helper methods of EnsureAttribute<T>(“name”), GetAttribute<T>(“name”), and SetAttribute<T>(“name”) are to ensure our interface members are properly registered with the tracking contact model.

Step 2: Registering the Facet

Now that we the model defined via our interface and our facet implemented, it’s time we tell SItecore that it is ready to use. In order to do this, we need to write a configuration patch file in order to patch the Sitecore.Analytics.Model.config.

Save the above snippet in your App_Config folder to patch it into the Sitecore’s configuration. There are 3 section to the above snippet so let’s walk through them now.

The first section defines where our new field definitions are for the tracking model. This is defined in the <elements> section of the snippet.

The second section defines how to expose our model properties by way of the <contact> section. In this instance, we’re appending a new facet to the existing collection. Sitecore will take our model definition and populate the contact using the facet we provided.

The third section patches the Sitecore.SessionSerialization.config file which is used when running a multiple CD servers in your environment. This section simply registers our type to be serialized into session state.

Step 3: Reading and Writing

Now that the facet is defined and registered, we can write code to help us read and write the information we wish to track of our users.

In both reading and writing scenarios, you will need to ensure that the user’s tracking contact is available. If VIsitor Tracking is not enabled on your site, none of the following code act properly.

In this snippet, we define a repository class that will be used to set the properties we defined in our facet earlier. The SetFacentInformation function defines 3 parameters. The first is used to identify the user. If the user is not identified, the facet settings will exist for the user for the session; however, it will not be persisted to the xDB database for future use. The second and third parameters are the values we wish to track.

The first thing we do inside of the SetFacentInformation function is ensure visitor tracking is enabled on this user’s session. If it is not, we there is no object to append our data to.

Next, we identify the user if they have not been identified so far this session. Again, it is very important to identify your users so that the tracking information is stored to the xDB database. Unidentified (Anonymous) users are not stored in the xDB database for performance reasons and their return visits will be reset.

Lastly, we retrieve our user’s Payment facet which we defined in our configuration patch file. Once this facet has been retrieved, we can set the properties of the facet by reference with no explicit call to save the information. In Sitecore 8.x, the values we set are persisted for the session and, if the user is identified, will be persisted to the xDB database at the end of the current user session.

By retrieving our user’s Payment facet we also have the option to read any existing values that are saved to the xDB database from a previous session or earlier in the current session. If we wish to create a custom personalization rule around these values, we would simply retrieve the facet as we did in this example and read the properties. But that is the topic of a latter post.

Additional Resources

How To: Change the Sitecore Admin Login Background

This tutorial will provide you the necessary steps in order to change the Background Image of the SItecore Admin Login Screen. Changing the background image for the admin login screen is a great way to personalize an experience for the content authors, provide agency or corporate branding, or provide additional information about the site (by embedding the information into the image).

Sitecore Versions:

  • Sitecore 8.x
  • Sitecore 9.x

Tutorial:

Step 1: Create / Select a New Background Image

Create a new image to use as the login screen background image.  To accommodate a variety of screen sizes, it is recommended the image is 2560x1600px in dimensions.  Once you have obtained the image, add it to the project so that it will be deployed with the rest of the website’s assets.

Note: When the background image is presented on the screen, it is set via the following CSS rules:

Step 2: Create a Patch File

To set the new image, you need to create a new configuration patch file within the App_Config project folder.  The patch file should contains the following code.

Step 3: Deploy and Test

With all files saved and added to the project, simply deploy the code and visit the login screen in order to verify everything is configured correctly.

Talking about Sitecore

For the last 5 years, I’ve been focusing a lot of my professional time around designing experiences around Sitecore.  For the last 3 of those years, I’ve been doing a large amount of development in the platform as well.  After that time, I still find the platform has much to learn and with every new release, another body’s worth of features and potential comes with it.

Sitecore, as a platform, is unlike any CMS platform I’ve worked with up to this point.  There are competing platforms for an enterprise level CMS; however, with so much to offer and my primary languages of choice being .Net and Node, Sitecore is an amazing platform to work within.  From a User Experience perspective, no other platform that I’ve used has the capabilities for personalization, analytics, and optimization built into it out of the box.  In many ways, it’s an ideal platform for someone like myself who has experiences with Marketing Technologies, Design Research, and Development.

As I continue to work within the platform from the perspective of each of these 3 areas, I plan to document a variety of tutorials and strategies here.  This is for my own reference; however, like any public blog – I also hope someone will discover such and find value from such as well.