4

From a rendering controller, I am trying to list the variable names / each outcome of an A/B test programmatically.

Here is my A/B test:

enter image description here

enter image description here

Firstly, I inspected the renderings using the code below but found nothing:

var item = Sitecore.Context.Item;
Sitecore.Data.Fields.LayoutField layoutField = item.Fields["__renderings"];
Sitecore.Layouts.RenderingReference[] renderings = layoutField.GetReferences(Sitecore.Context.Device);

enter image description here

Next, I inspected the Sitecore.Analytics.Tracker.Current, but found nothing:

enter image description here

Any idea?

1 Answer 1

4

Just like personalizations, the AB tests are stored on the rendering fields. If you are able to see the AB test on the Experience editor so it means that the test has been set.

Now where does Sitecore store the tests items it associate with my page? On the Marketing Control Panel /Test Lab bucket. If you open the content editor and search for the existing tests you should be able to see the results there.

Here is a test I have configured on my build associated with my home page: enter image description here

Looking at your code above I can say you are on the right track but your problem most likely is that the test is configured on the final layout as opposed to the shared layout.

Here is the code slightly changed(look at in this case I'm reading the final layout as opposed to the shared):

Sitecore.Data.Fields.LayoutField layoutField = RenderingContext.Current.PageContext.Item.Fields["__final renderings"];
            var renderings= layoutField.GetReferences(Sitecore.Context.Device).FirstOrDefault();
            if (renderings != null)
            {
                var testID = renderings.Settings.MultiVariateTest;
            }

With this code you should be able to see the ID on a string format of the test on the renderings.Settings.MultiVariateTest property. enter image description here

Note that the Guid I'm getting on the MultiVariateTest property is matching to the guid from the item the Marketing Control Panel(first screenshot).

Hope this helps.

1
  • Thanks! That worked. Just enhancing your answer, I wanted to get the Variables name and I just need to add 3 more line of codes in order to get that. var testID = renderings.Settings.MultiVariateTest; var testItem = Sitecore.Context.Database.GetItem(new ID(testID)); if (testItem.HasChildren) testItem.Children.ToList().ForEach(s => multiVariateTestName.Add(s.Name));
    – João Neto
    Jul 17, 2017 at 14:35

Your Answer

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

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