During the training session, we had a chat about Profile card, Profile keys and how we are assigning those to a content item. The example given there was for a Cycling Shop. We could create some profile keys for "Beginner", "Amateur" and "Professional". Obviously, you will have different product that will be for Professional: Helmets, Bikes and even GoPro (why not...). Well that gave me an idea for this blog posts and the following on:
- How to access those keys and Profile card when we access the item.
- How to retrieve Related items based on profile keys - we will continue with that on a later post...
So first let see how the data are stored in the sitecore item:
When you assign the Profile card and keys in Sitecore you will click on the "Edit Profile Card associated with the item":
Once you have done that, you will notice that those data are stored on the Content Item. The field you are looking for is (enable the standard fields): Tracking ( - or __tracking)
I have pasted the formated XML here for readability:
Since those data are in a field, that means we can access them throuh code... Here are a few methods to show you how you can retrieve the ContentProfiles or just the ContentProfiles name or if you prefer to get the profile Keys... The important thing here is to get the Field "__tracking" and cast it to a Sitecore.Analytics.Data.TrackingField. This field will give you all the required information you need for Campaigns, events, and Profiles... From this field, you should now be able to extract the different profiles associated with the item and then from each profile you can access every keys...< profile id="{1E05D43D-BF07-403D-893D-552FCFBF2BD0}" name="MyTest" presets="general|100||"> < key name="Digital" value="2" /> < key name="Traditional" value="2" /> < /profile>
////// Return the list of all profiles for the content Item /// /// ///public static IEnumerable GetProfiles(Item item) { if (!AnalyticsSettings.Enabled) { return new List (); } Field field = item.Fields["__tracking"]; if (field == null) return new List (); ContentProfile[] profiles = (new TrackingField(field)).Profiles; if (profiles == null) return new List (); return profiles.ToList(); } /// /// Return the list of all profile names associated with the content Item /// Where Keys are defined. This is to make sure we are not returning the profiles that are not assigned /// /// ///public static IEnumerable GetProfileNames(Item item) { if (!AnalyticsSettings.Enabled) { return new List (); } Field field = item.Fields["__tracking"]; if (field == null) return new List (); ContentProfile[] profiles = (new TrackingField(field)).Profiles; if (profiles == null) return new List (); return profiles.Where(p => GetProfileKeys(p,1).Count()>0).Select(i => i.Name).ToList(); } /// /// Get the keys contains in the Content Profile with a minimum score /// /// /// ///public static IEnumerable GetProfileKeys( ContentProfile profile, int minimumScore) { var matchedKeys = new List (); foreach (ContentProfileKeyData profileKey in profile.Keys) { if (profileKey.Value >= minimumScore) { matchedKeys.Add(profileKey); } } return matchedKeys; } /// /// Get the key names contains in the Content Profile with a minimum score /// /// /// ///public static IEnumerable GetProfileKeyNames(ContentProfile profile, int minimumScore) { var matchedKeys = new List (); foreach (ContentProfileKeyData profileKey in profile.Keys) { if (profileKey.Value >= minimumScore) { matchedKeys.Add(profileKey.Name); } } return matchedKeys; } /// /// Get the Key Names that are define in the Item profiles with a minimum score /// /// /// ///public static IEnumerable GetProfileKeyNames(Item item, int minimumScore) { List keyNames = new List (); IEnumerable profiles = GetProfiles(item); foreach (var profile in profiles) { keyNames.AddRange(GetProfileKeyNames(profile, minimumScore)); } return keyNames.Distinct(); }
Now on the next post, we will see how we could use that to get the related content items based on Profile Keys...
No comments:
Post a Comment