Wednesday, January 27, 2016

Select Test Variation & Sitecore Analytics Visitor Tags


using System;
using HA.ApplicationLogic.Session;
using HA.Web.Analytics.Tracking;
using HA.Web.Analytics.Tracking.Base;
using HA.Web.Analytics.Tracking.Enums;
using HA.Web.Analytics.Tracking.Models;
using Sitecore.Analytics.Data.Items;
using Sitecore.Analytics.Testing;
using Sitecore.Diagnostics;
using Sitecore.Mvc.Analytics.Pipelines.Response.CustomizeRendering;

namespace HA.Web.Analytics.Pipelines
{
    public class SelectTestVariation : SelectVariation
    {
        protected override void ApplyVariation(CustomizeRenderingArgs args, ComponentTestContext context)
        {
            try
            {
                //determine if there's a multivariate test currently running on this page
                AbstractTrackerBase tracker = TrackerFactory.GetTracker();
                TestDefinitionItem runningTest;
                tracker.TryGetTest(out runningTest);

                if (runningTest == null)
                    return;

                //read visitor test tag and compare to a running test
                TestDefinitionItem recordedVisitorTest;
                tracker.TryGetRunningVisitorTest(out recordedVisitorTest);

                if (recordedVisitorTest != null)
                {
                    //if recorded Contact/Visitor test is the same as the test currently running - proceed with a test
                    if (recordedVisitorTest.ID == runningTest.ID)
                    {
                        //proceed with test
                    }
                    else
                    {
                        tracker.CancelTests();
                        return;
                    }
                }

                //check session
                TestDefinitionItem recordedSessionTest;
                tracker.TryGetRunningSessionTest(out recordedSessionTest);

                if(recordedSessionTest != null)
                {
                    //if recorded Session test is the same as the test currently running - proceed with a test
                    if (recordedSessionTest.ID == runningTest.ID)
                    {
                        //proceed with test
                    }
                    else
                    {
                        tracker.CancelTests();
                        return;
                    }
                }

                //persist the test only if it's not in Contact Tag or in Session
                if (tracker.GetTestExposureStrategy() == ExposureStrategy.OncePerContact &&
                    recordedVisitorTest == null)
                {
                    VisitorTag tag = new VisitorTag(tracker.GetVisitorTagName(), runningTest.ID.ToString());
                    tracker.SetVisitorTag(tag);
                }
                else if (tracker.GetTestExposureStrategy() == ExposureStrategy.OncePerSession && recordedSessionTest == null)
                {
                    SessionManager.Instance.MultivariateTestPage = runningTest.ID.ToString();
                }

                //run the test
                base.ApplyVariation(args, context);
            }
            catch (Exception ex)
            {
                Log.Error("Could not apply A/B test variation", ex, this);
            }
        }
    }
}



No comments:

Post a Comment