After configuring the persona tool to handle the way profile data is handled by Mongo and creating and populating a mock collection of visit data using .NET driver for Mongo, I saw that the continued development of the Persona Tool required using MongoDB Query to get Sitecore profile data in .NET. The excellent blog post MongoDB C# Driver Cheat Sheet by my esteemed colleague Derek Hunziker was my “Go to” when working with Mongo Query for .NET.
MongoDB.Driver.Builders namespace offers a lot of functionality to work with Mongo data. The one we are particularly interested in here is a class called Query. When opening the Object Browser we see a plethora of operators.
In the code snippet below I am using .And (I am combining all filters within that And), .GTE (“>=”), .LTE (“<=”), and .Exists.
“Profiles.Audience Segment”. This is quite intuitive and would not intimidate somebody familiar with SQL and/or Linq. However, I ran into some difficulties querying Sitecore xDB profile data. It just so happens that Sitecore’s schema for profiles is set up like this: Profile is not a json array of objects but rather an object in itself, as shown I the image below. I erroneously assumed that I could query for Audience Segment by doing something like “Profiles” or “Profiles[“Audience Segment”]” but neither of those worked. In order to query successfully I needed “Profiles.Audience Segment”.
I passed a profile name (i.e. “Audience Segment”) along with a date range to my method that reads visit data for this profile. Once I got the collection I built the string mentioned above – currentProfileString = “Profiles.” + profileName which comes out as “Profiles.Audience Segment”.
Then I constructed the query object. I filtered by StartDateTime and EndDateTime.
‘Query.Exists(currentProfileString)’ is equivalent to ‘.Where(x => x.Profiles != null && x.Profiles.ContainsKey(profileName))’ in Linq.
Human translation: I asked the database to return visit data for documents where profile node “Audience Segment” existed AND where visit start date was within a certain range.
It’s very informative to look inside the VisitData class offered by Sitecore.Analytics. From there this can be turned into a custom object, serialized or used as is. Below I have highlighted Profiles (essentially the profile data) and Value (which is actually an Engagement Value.)
In my next blog post I’ll discuss how exceptional, extraordinary and essential Engagement Value is, and how Persona Tool analyzes this important piece of information from each visit. If you’d like more information about persona development and marketing strategy, please reach out to Hedgehog Digital Marketing Innovation Team