Wednesday 30 August 2017

Sitecore FXM, Profile and Pattern Matching

I was playing around with FXM and I was able to create placeholders in the non-Sitecore site and put Sitecore content into those placeholders, which is cool!

However it was not so lucky when setting up profiles at the beginning. For some reason (still unknown) the profile (and its keys, profile/pattern cards) I set up the first time did not with with the page filters and nothing got recorded in MongoDB's intractions collection, and of course in Experience Analytics only the visits are shown.

I then wrote a custom pipeline to loop through the profile cards associated with the matched filter and manually score them. This worked, the profile and the profile keys/values were recorded against the interaction however no pattern was recorded.

After studying the code, I realised that what I did in my custom pipeline was already catered for out-of-the-box so I was confused and consulted Sitecore support and they also could not figure out why so I was suggested to create another profile from scratch and remove the custom pipeline.

Surprisingly this worked! I could see profile values as well as pattern card recorded under the interaction and the matched pattern shows in Experience Analytics.

However soon I noticed that there is nothing displayed in Experience Profile. I used to see bunch anonymous records there in another version of Sitecore 8 but I cannot see anything here. Why?

I am using Sitecore 8.2 Update 4 and if you are not as careless as me and are paying attention to the release notes, you would notice that in the release notes of Sitecore 8.2 Update 3 there is a change to how the anonymous contacts are indexed:

    
<settings>

  
  <setting name="ContentSearch.Analytics.IndexAnonymousContacts" value="false"/>
</settings>
Note the comments and the default value was changed from true (prior to 8.2.3) to false. For sites that do not have WFFM (that identifies the contact when needed) nor custom contact identification process you might prefer this to be turned on in order to display the behaviour of the anonymous users. However if you do have the mechanism to identify the contact then this is suggested to be turned off to reduce impact on performance.

So by changing the value to true and make some interactions with the site, you will see the anonymous contacts listed in Experience Profile. Note that this only works for new intractions; if you want to see old data (before this was set to true) you will need to rebuild your reporting database.

I thought I would just blog this in case someone was struggling with this too!

Wednesday 16 August 2017

Sitecore Tracking Field Parsing - the Native Way

When we assign profile cards, goals or attributes (outcomes, events, etc) to a page content item, Sitecore stores the information in the Tracking (actual name __tracking) field under the Advanced section, as XML which looks like below (I am using the Sitecore Helix Utilities demo site for example):


Now if we switch to the raw value, we will see it is stored in XML, which I have formatted it for ease of viewing:
  
<tracking>
   <profile id="{D016ADEF-7737-4289-A85F-FE0055F49C8E}" name="Utilities Persona" presets="evan the eco-owner|100||">
      <key name="Sign-up for service" value="1" />
      <key name="Solar_Enviro Programs" value="5" />
      <key name="Budget_Savings Offers" value="4" />
      <key name="H of H_Community" value="3" />
   </profile>
</tracking>

There might be some cases where you want to parse this and get the information out of it and I have seen many developers parsing it by loading the value into XmlDocument and then read its nodes. This works fine however we would imagine Sitecore should have already had something doing this?

Answer is yes of course :) So, we can use the following code to get a strongly-typed object that represents this information.

// using Sitecore.Analytics.Data;

var pageItem = Sitecore.Context.Item;
var trackingField = new TrackingField(pageItem.Fields["__Tracking"]);

// Loop through all profiles (if multiple profile cards are assigned)
foreach (var profile in trackingField.Profiles)
{
    // Loop through all profile keys for current profile.
    // Here we want only the ones that has value greater than 0, for example.
    foreach (var k in profile.Keys.Where(o=> o.Value > 0))
    {
        // Do something
    }
}

The above example shows how to read profiles. If you have stuff like campaigns, goals, events that you need, in the TrackingField class there are collections for them as well:


Note that goals are same as events so you will find it under the Events IEnumerable.

Hope this helps!