Beispiel #1
0
        public ActionResult Shop()
        {
            if (ConfigRepo == null || ConfigRepo.ExperimentKey == null)
            {
                return(RedirectToAction("Config"));
            }

            int?visitorId = (int?)Session["VisitorId"];

            if (visitorId == null)
            {
                visitorId = VisitorRepo.First().Id;
            }

            Visitor visitor   = VisitorRepo.Single(v => v.Id == visitorId);
            var     variation = Optimizely.Activate(
                experimentKey: ConfigRepo.ExperimentKey,
                userId: visitor.Id.ToString(),
                userAttributes: visitor.GetUserAttributes());

            return(View(new DemoViewModel
            {
                Products = GetProducts(variation.Key).ToArray(),
                CurrentVisitor = visitor,
                Message = TempData.ContainsKey("Message") ? (string)TempData["Message"] : null,
                VariationKey = variation.Key
            }));
        }
        public void TestSetGetForcedVariation()
        {
            var userId                = "test_user";
            var invalidUserId         = "invalid_user";
            var experimentKey         = "test_experiment";
            var experimentKey2        = "group_experiment_1";
            var invalidExperimentKey  = "invalid_experiment";
            var expectedVariationKey  = "control";
            var expectedVariationKey2 = "group_exp_1_var_1";
            var invalidVariationKey   = "invalid_variation";

            var userAttributes = new UserAttributes
            {
                { "device_type", "iPhone" },
                { "location", "San Francisco" }
            };

            var optlyObject = new Optimizely(TestData.Datafile, new ValidEventDispatcher(), LoggerMock.Object);

            optlyObject.Activate("test_experiment", "test_user", userAttributes);

            // invalid experiment key should return a null variation
            Assert.False(Config.SetForcedVariation(invalidExperimentKey, userId, expectedVariationKey));
            Assert.Null(Config.GetForcedVariation(invalidExperimentKey, userId));

            // setting a null variation should return a null variation
            Assert.True(Config.SetForcedVariation(experimentKey, userId, null));
            Assert.Null(Config.GetForcedVariation(experimentKey, userId));

            // setting an invalid variation should return a null variation
            Assert.False(Config.SetForcedVariation(experimentKey, userId, invalidVariationKey));
            Assert.Null(Config.GetForcedVariation(experimentKey, userId));

            // confirm the forced variation is returned after a set
            Assert.True(Config.SetForcedVariation(experimentKey, userId, expectedVariationKey));
            var actualForcedVariation = Config.GetForcedVariation(experimentKey, userId);

            Assert.AreEqual(expectedVariationKey, actualForcedVariation.Key);

            // check multiple sets
            Assert.True(Config.SetForcedVariation(experimentKey2, userId, expectedVariationKey2));
            var actualForcedVariation2 = Config.GetForcedVariation(experimentKey2, userId);

            Assert.AreEqual(expectedVariationKey2, actualForcedVariation2.Key);
            // make sure the second set does not overwrite the first set
            actualForcedVariation = Config.GetForcedVariation(experimentKey, userId);
            Assert.AreEqual(expectedVariationKey, actualForcedVariation.Key);
            // make sure unsetting the second experiment-to-variation mapping does not unset the
            // first experiment-to-variation mapping
            Assert.True(Config.SetForcedVariation(experimentKey2, userId, null));
            actualForcedVariation = Config.GetForcedVariation(experimentKey, userId);
            Assert.AreEqual(expectedVariationKey, actualForcedVariation.Key);

            // an invalid user ID should return a null variation
            Assert.Null(Config.GetForcedVariation(experimentKey, invalidUserId));
        }
Beispiel #3
0
        public void TestGetVariationUserWithSetForcedVariation()
        {
            var experimentKey       = "test_experiment";
            var pausedExperimentKey = "paused_experiment";
            var userId = "test_user";
            var expectedForcedVariationKey = "variation";
            var optlyObject = new Optimizely(TestData.Datafile, new ValidEventDispatcher(), LoggerMock.Object);

            var userAttributes = new UserAttributes
            {
                { "device_type", "iPhone" },
                { "location", "San Francisco" }
            };

            optlyObject.Activate(experimentKey, userId, userAttributes);

            // confirm normal bucketing occurs before setting the forced variation
            var actualVariation = optlyObject.GetVariation(experimentKey, userId, userAttributes);

            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation));

            // test valid experiment
            Assert.IsTrue(optlyObject.SetForcedVariation(experimentKey, userId, expectedForcedVariationKey), string.Format(@"Set variation to ""{0}"" failed.", expectedForcedVariationKey));

            var actualForcedVariation = optlyObject.GetVariation(experimentKey, userId, userAttributes);

            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyVariation, actualForcedVariation));

            // clear forced variation and confirm that normal bucketing occurs
            Assert.IsTrue(optlyObject.SetForcedVariation(experimentKey, userId, null));

            actualVariation = optlyObject.GetVariation(experimentKey, userId, userAttributes);
            Assert.IsTrue(TestData.CompareObjects(VariationWithKeyControl, actualVariation));

            // check that a paused experiment returns null
            Assert.IsTrue(optlyObject.SetForcedVariation(pausedExperimentKey, userId, expectedForcedVariationKey), string.Format(@"Set variation to ""{0}"" failed.", expectedForcedVariationKey));
            actualForcedVariation = optlyObject.GetVariation(pausedExperimentKey, userId, userAttributes);

            Assert.IsNull(actualForcedVariation);
        }
 public Variation TestActivate(ActivateParams parameters)
 {
     return(OptimizelyClient.Activate(parameters.ExperimentKey, parameters.UserId, parameters.UserAttributes));
 }