Example #1
0
        public void ImpressionEventTestWithAttributes()
        {
            var projectConfig = Config;
            var experiment    = Config.GetExperimentFromKey("test_experiment");
            var variation     = Config.GetVariationFromId(experiment.Key, "77210100090");
            var userId        = TestUserId;

            var userAttributes = new UserAttributes {
                { "device_type", "iPhone" },
                { "company", "Optimizely" }
            };

            var impressionEvent = UserEventFactory.CreateImpressionEvent(projectConfig, experiment, variation, userId, userAttributes, "test_experiment", "experiment");

            Assert.AreEqual(Config.ProjectId, impressionEvent.Context.ProjectId);
            Assert.AreEqual(Config.Revision, impressionEvent.Context.Revision);
            Assert.AreEqual(Config.AccountId, impressionEvent.Context.AccountId);
            Assert.AreEqual(Config.AnonymizeIP, impressionEvent.Context.AnonymizeIP);
            Assert.AreEqual(Config.BotFiltering, impressionEvent.BotFiltering);
            Assert.AreEqual(experiment, impressionEvent.Experiment);
            Assert.AreEqual(variation, impressionEvent.Variation);
            Assert.AreEqual(userId, impressionEvent.UserId);
            Assert.AreEqual(Config.BotFiltering, impressionEvent.BotFiltering);

            var expectedVisitorAttributes = EventFactory.BuildAttributeList(userAttributes, projectConfig);

            TestData.CompareObjects(expectedVisitorAttributes, impressionEvent.VisitorAttributes);
        }
Example #2
0
        public void ConversionEventTest()
        {
            var projectConfig  = Config;
            var experiment     = Config.GetExperimentFromKey("test_experiment");
            var variation      = Config.GetVariationFromId(experiment.Key, "77210100090");
            var userId         = TestUserId;
            var eventKey       = "purchase";
            var userAttributes = new UserAttributes {
                { "device_type", "iPhone" },
                { "company", "Optimizely" }
            };

            var conversionEvent = UserEventFactory.CreateConversionEvent(projectConfig, eventKey, userId, userAttributes, null);

            Assert.AreEqual(Config.ProjectId, conversionEvent.Context.ProjectId);
            Assert.AreEqual(Config.Revision, conversionEvent.Context.Revision);
            Assert.AreEqual(Config.AccountId, conversionEvent.Context.AccountId);
            Assert.AreEqual(Config.AnonymizeIP, conversionEvent.Context.AnonymizeIP);
            Assert.AreEqual(Config.BotFiltering, conversionEvent.BotFiltering);
            Assert.AreEqual(Config.GetEvent(eventKey), conversionEvent.Event);
            Assert.AreEqual(userId, conversionEvent.UserId);
            Assert.AreEqual(Config.BotFiltering, conversionEvent.BotFiltering);

            var expectedVisitorAttributes = EventFactory.BuildAttributeList(userAttributes, projectConfig);

            TestData.CompareObjects(expectedVisitorAttributes, conversionEvent.VisitorAttributes);
        }
Example #3
0
        /// <summary>
        /// Sends impression event.
        /// </summary>
        /// <param name="experiment">The experiment</param>
        /// <param name="variation">The variation entity</param>
        /// <param name="userId">The user ID</param>
        /// <param name="userAttributes">The user's attributes</param>
        /// <param name="flagKey">It can either be experiment key in case if ruleType is experiment or it's feature key in case ruleType is feature-test or rollout</param>
        /// <param name="ruleType">It can either be experiment in case impression event is sent from activate or it's feature-test or rollout</param>
        private void SendImpressionEvent(Experiment experiment, Variation variation, string userId,
                                         UserAttributes userAttributes, ProjectConfig config,
                                         string flagKey, string ruleType, bool enabled)
        {
            if (experiment != null && !experiment.IsExperimentRunning)
            {
                Logger.Log(LogLevel.ERROR, @"Experiment has ""Launched"" status so not dispatching event during activation.");
            }

            var userEvent = UserEventFactory.CreateImpressionEvent(config, experiment, variation, userId, userAttributes, flagKey, ruleType, enabled);

            if (userEvent == null)
            {
                return;
            }
            EventProcessor.Process(userEvent);

            if (experiment != null)
            {
                Logger.Log(LogLevel.INFO, $"Activating user {userId} in experiment {experiment.Key}.");
            }
            // Kept For backwards compatibility.
            // This notification is deprecated and the new DecisionNotifications
            // are sent via their respective method calls.
            if (NotificationCenter.GetNotificationCount(NotificationCenter.NotificationType.Activate) > 0)
            {
                var impressionEvent = EventFactory.CreateLogEvent(userEvent, Logger);
                NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Activate, experiment, userId,
                                                     userAttributes, variation, impressionEvent);
            }
        }
Example #4
0
        /// <summary>
        /// Sends conversion event to Optimizely.
        /// </summary>
        /// <param name="eventKey">Event key representing the event which needs to be recorded</param>
        /// <param name="userId">ID for user</param>
        /// <param name="userAttributes">Attributes of the user</param>
        /// <param name="eventTags">eventTags array Hash representing metadata associated with the event.</param>
        public void Track(string eventKey, string userId, UserAttributes userAttributes = null, EventTags eventTags = null)
        {
            var config = ProjectConfigManager?.GetConfig();

            if (config == null)
            {
                Logger.Log(LogLevel.ERROR, "Datafile has invalid format. Failing 'Track'.");
                return;
            }

            var inputValues = new Dictionary <string, string>
            {
                { USER_ID, userId },
                { EVENT_KEY, eventKey }
            };

            if (!ValidateStringInputs(inputValues))
            {
                return;
            }

            var eevent = config.GetEvent(eventKey);

            if (eevent.Key == null)
            {
                Logger.Log(LogLevel.INFO, string.Format("Not tracking user {0} for event {1}.", userId, eventKey));
                return;
            }


            if (eventTags != null)
            {
                eventTags = eventTags.FilterNullValues(Logger);
            }

            var userEvent = UserEventFactory.CreateConversionEvent(config, eventKey, userId, userAttributes, eventTags);

            EventProcessor.Process(userEvent);
            Logger.Log(LogLevel.INFO, string.Format("Tracking event {0} for user {1}.", eventKey, userId));

            if (NotificationCenter.GetNotificationCount(NotificationCenter.NotificationType.Track) > 0)
            {
                var conversionEvent = EventFactory.CreateLogEvent(userEvent, Logger);
                NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Track, eventKey, userId,
                                                     userAttributes, eventTags, conversionEvent);
            }
        }
Example #5
0
        public void ImpressionEventTest()
        {
            var projectConfig = Config;
            var experiment    = Config.GetExperimentFromKey("test_experiment");
            var variation     = Config.GetVariationFromId(experiment.Key, "77210100090");
            var userId        = TestUserId;

            var impressionEvent = UserEventFactory.CreateImpressionEvent(projectConfig, experiment, variation, userId, null, "test_experiment", "experiment");

            Assert.AreEqual(Config.ProjectId, impressionEvent.Context.ProjectId);
            Assert.AreEqual(Config.Revision, impressionEvent.Context.Revision);
            Assert.AreEqual(Config.AccountId, impressionEvent.Context.AccountId);
            Assert.AreEqual(Config.AnonymizeIP, impressionEvent.Context.AnonymizeIP);
            Assert.AreEqual(Config.BotFiltering, impressionEvent.BotFiltering);
            Assert.AreEqual(experiment, impressionEvent.Experiment);
            Assert.AreEqual(variation, impressionEvent.Variation);
            Assert.AreEqual(userId, impressionEvent.UserId);
            Assert.AreEqual(Config.BotFiltering, impressionEvent.BotFiltering);
        }
 private ConversionEvent CreateConversionEvent(string eventName)
 {
     return(UserEventFactory.CreateConversionEvent(ProjectConfig, eventName, UserId, new UserAttributes(), new EventTags()));
 }
Example #7
0
 private static ConversionEvent BuildConversionEvent(string eventName, ProjectConfig projectConfig)
 {
     return(UserEventFactory.CreateConversionEvent(projectConfig, eventName, TestUserId,
                                                   new UserAttributes(), new EventTags()));
 }