コード例 #1
0
        /// <summary>
        /// Sends impression event.
        /// </summary>
        /// <param name="experiment">The experiment</param>
        /// <param name="variationId">The variation entity</param>
        /// <param name="userId">The user ID</param>
        /// <param name="userAttributes">The user's attributes</param>
        private void SendImpressionEvent(Experiment experiment, Variation variation, string userId,
                                         UserAttributes userAttributes)
        {
            if (experiment.IsExperimentRunning)
            {
                var impressionEvent = EventBuilder.CreateImpressionEvent(Config, experiment, variation.Id, userId, userAttributes);
                Logger.Log(LogLevel.INFO, string.Format("Activating user {0} in experiment {1}.", userId, experiment.Key));
                Logger.Log(LogLevel.DEBUG, string.Format("Dispatching impression event to URL {0} with params {1}.",
                                                         impressionEvent.Url, impressionEvent.GetParamsAsJson()));

                try
                {
                    EventDispatcher.DispatchEvent(impressionEvent);
                }
                catch (Exception exception)
                {
                    Logger.Log(LogLevel.ERROR, string.Format("Unable to dispatch impression event. Error {0}", exception.Message));
                }

                NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Activate, experiment, userId,
                                                     userAttributes, variation, impressionEvent);
            }
            else
            {
                Logger.Log(LogLevel.ERROR, @"Experiment has ""Launched"" status so not dispatching event during activation.");
            }
        }
コード例 #2
0
        public void TestCreateImpressionEventNoAttributes()
        {
            var guid      = Guid.NewGuid();
            var timeStamp = TestData.SecondsSince1970();

            var payloadParams = new Dictionary <string, object>
            {
                { "visitors", new object[]
                  {
                      new Dictionary <string, object>()
                      {
                          { "snapshots", new object[]
                            {
                                new Dictionary <string, object>
                                {
                                    { "decisions", new object[]
                                        {
                                            new Dictionary <string, object>
                                            {
                                                { "campaign_id", "7719770039" },
                                                { "experiment_id", "7716830082" },
                                                { "variation_id", "77210100090" }
                                            }
                                        } },
                                    { "events", new object[]
                                        {
                                            new Dictionary <string, object>
                                            {
                                                { "entity_id", "7719770039" },
                                                { "timestamp", timeStamp },
                                                { "uuid", guid },
                                                { "key", "campaign_activated" }
                                            }
                                        } }
                                }
                            } },
                          { "attributes", new object[] { } },
                          { "visitor_id", TestUserId }
                      }
                  } },
                { "project_id", "7720880029" },
                { "account_id", "1592310167" },
                { "client_name", "csharp-sdk" },
                { "client_version", Optimizely.SDK_VERSION },
                { "revision", 15 },
                { "anonymize_ip", false }
            };

            var expectedLogEvent = new LogEvent("https://logx.optimizely.com/v1/events",
                                                payloadParams,
                                                "POST",
                                                new Dictionary <string, string>
            {
                { "Content-Type", "application/json" }
            });

            var logEvent = EventBuilder.CreateImpressionEvent(Config, Config.GetExperimentFromKey("test_experiment"), "77210100090", TestUserId, null);

            TestData.ChangeGUIDAndTimeStamp(logEvent.Params, timeStamp, guid);

            Assert.IsTrue(TestData.CompareObjects(expectedLogEvent, logEvent));
        }