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.

Dusting Things Off

It’s been a long while since I last blogged on the site.  A lot has happened in my life and my career changed in order to focus more on the human side of technology.  However, those aren’t excuses for the absence I have had from this blog.  If I ever had an excuse was simply writer’s block as nothing has inspired me to write. But that’s changing.  Old habits are returning and that means more blog posts and more content.

To start – my 4 main / current NPM packages have been updated in order to get them up to speed with Node v0.12 as well as to make them compatible with Browserify.  I may add Bower support later but right now, it is what it is.  Along with these changes, I updated the interface making a couple of them 2.0 releases.  You can find more information about the packages at the links below.

luhn – https://www.npmjs.com/package/luhn

requiredir – https://www.npmjs.com/package/requiredir

datevalidator – https://www.npmjs.com/package/datevalidator

abavalidator – https://www.npmjs.com/package/abavalidator

In addition to updating those packages, I’m also resurrecting some old Node.js projects that I had worked on over the last 2 years and begin to add them to Github.  Lastly, you can expect more Node.js and iOS development posts on here with a sprinkling of UX.  I believe in the importance of transparency in learning and I can practice my development skills a lot easier than my User Experience skills.

More to come soon.

Redesigning the Save Button Is the Wrong Approach

Every once in a while, I come across an article about someone talking about how antiquated the floppy disk icon of Save has become.  They talk about how many office documents of today would not have fit onto the 1.44mb capacity of a floppy disk and as new generations of computer users arrive onto the scene, many have never used a floppy disk.  Obviously the icon needs redesigned these individuals discuss; however, I say it doesn’t need redesigned – the button itself needs to be removed altogether.

Continue reading Redesigning the Save Button Is the Wrong Approach

Giving an e-Gift could be Better

It’s October and that means many retailers are beginning to finalize their Holiday plans and queuing everything up for release in the next month or so. While some retailers have their Christmas and other holiday decorations up already, most e-commerce websites won’t be rolling that out for another month.  It’s also around this time of year that I remember how terrible of an experience gifting digital downloads remains.

Continue reading Giving an e-Gift could be Better

Get A Job!

Back in April of 2012, I did a presentation at the Kansas City Developers Conference on tips and tricks to finding and landing your dream job. Having been through a number of interviews as both an interviewer as well as someone trying to get hired over the last handful of years, I have had the opportunity to see what things are important to both sides of the table. In the end, both parties need to make sure that the other side is right for them and it’s not just a one-way relationship.

Continue reading Get A Job!

Using NPM Scripts for Client-Side Module Installation

One of the largest things that I have loved about working in Node.js is the ability to create utilities that work on both the server and within the browsers.  Being able to create packages for NPM and then reuse them in both environments has lead to a lot of shared and unified code.  However, the general experience involving installing a package and moving it to the client left a lot to be desired.  Thankfully, NPM offers a possible solution through it’s use of scripts.

Continue reading Using NPM Scripts for Client-Side Module Installation

Easier module imports with Requiredir

The more that I use Node.js, the more I enjoy it; however, there I have seen an issue in a couple of projects.  In these projects, some modules import in a large number of other modules that reside in single directory.  While it could be argued that the code could be refactored differently, scenarios exist where you may need to import in a number of modules that all exist in the same directory.

Continue reading Easier module imports with Requiredir