Get a key metric for an individual page in Sitecore

I have a question regarding Sitecore Analytics and user profile keys. I need to get a profile rating for a single page. For example, if I have a profile called "traveler" that can have a value of 1-10 on this page, I need to be able to get the value for this key that was assigned by the content author. I found that using the following:

Sitecore.Analytics.AnalyticsTracker.Current.Data.Profiles.GetProfile("Profile Name").GetProfileKeyValue("traveler")

I can get the total score that the user accumulated throughout the session, but I can’t find a way to get the rating for the current page only.

Any understanding that anyone could offer would be very helpful. Thank.

+3
source share
2 answers

I know this post is pretty old, but much has changed in Sitecore for future links. I don't know if this was possible in 2010, but at least in 2013 there are API methods for retrieving page tracking values.

I would never recommend manually analyzing the raw data in the __Tracking field.

Here's how to read tracking data for a Persona profile using the Sitecore Analytics API:

public static string ProfileValues(this Item item)
{
        StringBuilder sb = new StringBuilder();

        TrackingField trackingField = new TrackingField(item.Fields[Constants.Sitecore.FieldIDs.Tracking]);
        ContentProfile profile = trackingField.Profiles.FirstOrDefault(profileData =>
                                profileData.Name.Equals("Persona") && profileData.IsSavedInField);

        ContentProfileKeyData[] profileKeys = profile.Keys;

        foreach (ContentProfileKeyData profileKey in profileKeys)
        {
            sb.AppendLine(string.Format("{0}:{1};", profileKey.Name, profileKey.Value));
        }
        return sb.ToString();
    }

Regards Lasse Rush

+2
source

After some research, I found that this is stored as an XML string in a field under the name __Trackingfor each element. Access to it is possible, like any other data field, using the collection Fields. For instance:

Item itemToCheck = Sitecore.Context.Database.GetItem("/path to item/");
string trackingXml = itemToCheck.Fields["__Tracking"].ToString();

The XML in the string is structured as follows:

<tracking>
    <profile name="profile1">
        <key name="key1" value="1" />
        <key name="key2" value="10" />
    </profile>
    <profile name="profile2">
        <key name="key3" value="12" />
        <key name="key4" value="4" />
    </profile>
</tracking>

XmlDocument SelectNodes, ,

+2

Source: https://habr.com/ru/post/1769758/


All Articles