A/B testing


List of examples:

A/B tests

Creating an A/B test



// Gets a page to A/B test
TreeNode page = new DocumentQuery<TreeNode>()
                        .Path("/LandingPage", PathTypeEnum.Single)
                        .OnSite("MySite")
                        .Culture("en-us")
                        .TopN(1)
                        .FirstOrDefault();

if (page != null)
{
    // Gets an instance of the A/B test management service
    var abTestManager = Service.Resolve<IABTestManager>();

    // Creates a new A/B test for the page
    abTestManager.CreateABTest(page);

    // Creates the original page variant for the A/B test and an additional variant
    // (both have content identical to the original page)
    abTestManager.AddVariant(page, null);

    // Saves the new variant configuration into the page
    page.Update();
}

> Back to list of examples

Configuring conversions for an A/B test



// Gets the A/B test created for the "/LandingPage" page
ABTestInfo test = ABTestInfo.Provider.Get()
                            .WhereEquals("ABTestOriginalPage", "/LandingPage")
                            .OnSite("SiteCodeName")
                            .TopN(1)
                            .FirstOrDefault();

if (test != null)
{
    // Prepares a conversion definition of the 'Purchase' conversion type, using the default conversion Value
    var purchaseConversion = new ABTestConversion(ABTestConversionNames.PURCHASE);

    // Prepares a conversion definition of the 'Page visit' conversion type
    // Sets the conversion's Page URL to '/SpecialOffers' and the Value to 5
    var pageVisitConversion = new ABTestConversion(
                                    conversionName: ABTestConversionNames.PAGE_VISIT,
                                    relatedItemIdentifier: "/SpecialOffers",
                                    value: 5
                                );

    // Gets the A/B test's conversion configuration
    ABTestConversionConfiguration testConversionConfig = test.ABTestConversionConfiguration;

    // Adds the defined conversions to the configuration
    testConversionConfig.AddConversion(purchaseConversion);
    testConversionConfig.AddConversion(pageVisitConversion);

    // Saves the updated test
    ABTestInfo.Provider.Set(test);
}

> Back to list of examples

Starting an A/B test



// Gets the A/B test created for the "/LandingPage" page
ABTestInfo test = ABTestInfo.Provider.Get()
                            .WhereEquals("ABTestOriginalPage", "/LandingPage")
                            .OnSite("MySIte")
                            .TopN(1)
                            .FirstOrDefault();

// Checks that the test exists and is not already running
// Starts the test at the current time if it is scheduled to start in the future
if ((test?.ABTestOpenFrom == DateTime.MinValue) || (test?.ABTestOpenFrom > DateTime.Now))
{
    // Configures the test to start at the current time and end in a month
    test.ABTestOpenFrom = DateTime.Now;
    test.ABTestOpenTo = DateTime.Now.AddMonths(1);

    // Saves the test - the test automatically switches to the Running status when the start time is in the past
    ABTestInfo.Provider.Set(test);
}

> Back to list of examples

Deleting an A/B test



// Gets the A/B test created for the "/LandingPage" page
ABTestInfo test = ABTestInfo.Provider.Get()
                            .WhereEquals("ABTestOriginalPage", "/LandingPage")
                            .OnSite("MySite")
                            .TopN(1)
                            .FirstOrDefault();

if (test != null)
{
    // Gets an A/B tested page
    TreeNode page = new DocumentQuery<TreeNode>()
                            .Path("/LandingPage", PathTypeEnum.Single)
                            .OnSite("MySite")
                            .Culture("en-us")
                            .TopN(1)
                            .FirstOrDefault();

    if (page != null)
    {
        // Deletes the A/B test
        ABTestInfo.Provider.Delete(test);

        // Clears the A/B testing variant configuration of the related page
        page.SetValue("DocumentABTestConfiguration", null);
        page.Update();
    }
}

> Back to list of examples

A/B test page variants

Getting variants for a page



// Gets an A/B tested page
TreeNode page = new DocumentQuery<TreeNode>()
                        .Path("/LandingPage", PathTypeEnum.Single)
                        .OnSite("MySite")
                        .Culture("en-us")
                        .TopN(1)
                        .FirstOrDefault();

if (page != null)
{
    // Gets an instance of the A/B test management service
    var abTestManager = Service.Resolve<IABTestManager>();

    // Gets a collection of the page's A/B testing variants
    var variants = abTestManager.GetVariants(page);

    // Gets the variant named "Variant B"
    IABTestVariant variantB = variants.Where(x => x.Name == "Variant B").FirstOrDefault();
}

> Back to list of examples

Adding a variant



// Gets an A/B tested page 
TreeNode page = new DocumentQuery<TreeNode>()
                        .Path("/LandingPage", PathTypeEnum.Single)
                        .OnSite("MySite")
                        .Culture("en-us")
                        .TopN(1)
                        .FirstOrDefault();

if (page != null)
{
    // Gets an instance of the A/B test management service
    var abTestManager = Service.Resolve<IABTestManager>();

    // Gets the page's first existing A/B test variant
    IABTestVariant existingVariant = abTestManager.GetVariants(page)
                                                  .FirstOrDefault();

    // Creates a new variant for the given page
    // The variant's content is copied from the existing variant and can later be adjusted in the Pages application
    // The name of the new variant is generated automatically
    abTestManager.AddVariant(page, existingVariant?.Guid);

    // Saves the new variant configuration into the page
    page.Update();
}

> Back to list of examples

Renaming a variant



// Gets an A/B tested page 
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/LandingPage", PathTypeEnum.Single)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    // Gets an instance of the A/B test management service
    var abTestManager = Service.Resolve<IABTestManager>();

    // Gets the variant named "Variant B"
    IABTestVariant variantB = abTestManager.GetVariants(page).Where(x => x.Name == "Variant B").FirstOrDefault();

    if (variantB != null)
    {
        // Renames the variant to "Renamed variant"
        abTestManager.RenameVariant(page, variantB.Guid, "Renamed variant");

        // Saves the new variant configuration into the page
        page.Update();
    }
}

> Back to list of examples

Deleting a variant



// Gets an A/B tested page
TreeNode page = new DocumentQuery<TreeNode>()
                    .Path("/LandingPage", PathTypeEnum.Single)
                    .OnSite("MySite")
                    .Culture("en-us")
                    .TopN(1)
                    .FirstOrDefault();

if (page != null)
{
    // Gets an instance of the A/B test management service
    var abTestManager = Service.Resolve<IABTestManager>();

    // Gets the variant named "Variant B"
    IABTestVariant variantB = abTestManager.GetVariants(page).Where(x => x.Name == "Variant B").FirstOrDefault();

    if (variantB != null)
    {
        // Deletes the variant
        abTestManager.RemoveVariant(page, variantB.Guid);

        // Saves the new variant configuration into the page
        page.Update();
    }
}

> Back to list of examples

Selecting a variant as the A/B test winner



// Gets the A/B test created for the "/LandingPage" page
ABTestInfo test = ABTestInfo.Provider.Get()
                            .WhereEquals("ABTestOriginalPage", "/LandingPage")
                            .OnSite("SiteCodeName")
                            .TopN(1)
                            .FirstOrDefault();

if (test != null)
{
    // Gets an A/B tested page
    TreeNode page = new DocumentQuery<TreeNode>()
                            .Path("/LandingPage", PathTypeEnum.Single)
                            .OnSite("MySite")
                            .Culture("en-us")
                            .TopN(1)
                            .FirstOrDefault();

    if (page != null)
    {
        // Gets an instance of the A/B test management service
        var abTestManager = Service.Resolve<IABTestManager>();

        // Gets the variant named "Variant C"
        var variant = abTestManager.GetVariants(page).Where(x => x.Name == "Variant C").FirstOrDefault(); ;

        // Checks whether the A/B test is finished
        // An error occurs if you attempt to select a winner for an unfinished test
        if (ABTestStatusEvaluator.ABTestIsFinished(test) && variant != null)
        {
            // Selects "Variant C" as the A/B test winner
            // Automatically updates the page's widget and page template configuration,
            // and clears the A/B testing variant configuration
            abTestManager.PromoteVariant(page, variant.Guid);

            // Saves the cleared A/B testing variant configuration into the page
            page.Update();
        }
    }
}

> Back to list of examples