1

I have a 2 components in my webpage running personalisation rule.

  • Component A: 1 Rule + 1 Default Rule
  • Component B: 2 Rule + 1 Default Rule

I want to be able to check if there a personalisation rule other than the default rule was applied, and print out some form of an ID. This ID data will be fed to an external analytics system.

Is there some way to determine if the current page has any any personalisation rule applied and get the corresponding ID?

1 Answer 1

4

Not sure if it's going to be a good idea or not to do this (performance implications), but if you wanted to have a look at something like this, you might find what you need...??

if you have a Sitecore Item, you can call the following extension method like so:

var renderings = item.GetRenderingReferences("default");

foreach (var rendering in renderings)
{ 
  //access rendering.Settings.Blah here
}

//extension to add to your solution
public static class RenderingExtensions
{
    public static Sitecore.Layouts.RenderingReference[] GetRenderingReferences(this Item item, string deviceName)
    {
        LayoutField layoutField = item.Fields["__final renderings"];
        if (layoutField == null)
            return null;
        Sitecore.Layouts.RenderingReference[] renderings = null;
        if (item.Database != null)
        {
            renderings = layoutField.GetReferences(item.Database.GetDeviceItem(deviceName));
        }
        else
        {
            renderings = layoutField.GetReferences(Sitecore.Context.Database.GetDeviceItem(deviceName));
        }
        return renderings;
    }

    public static DeviceItem GetDeviceItem(this Database db, string deviceName)
    {
        return db.Resources.Devices.GetAll().Where(d => d.Name.ToLower() == deviceName.ToLower()).First();
    }
}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.