Пример #1
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)
        {
            if (!IsValid)
            {
                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 conversionEvent = EventBuilder.CreateConversionEvent(Config, eventKey,
                                                                     userId, userAttributes, eventTags);

            Logger.Log(LogLevel.INFO, string.Format("Tracking event {0} for user {1}.", eventKey, userId));
            Logger.Log(LogLevel.DEBUG, string.Format("Dispatching conversion event to URL {0} with params {1}.",
                                                     conversionEvent.Url, conversionEvent.GetParamsAsJson()));

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

            NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Track, eventKey, userId,
                                                 userAttributes, eventTags, conversionEvent);
        }
Пример #2
0
        public void TestCreateConversionEventNoAttributesNoValue()
        {
            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", "7722370027" }
                                            }
                                        } },
                                    { "events", new object[]
                                        {
                                            new Dictionary <string, object>
                                            {
                                                { "entity_id", "7718020063" },
                                                { "timestamp", timeStamp },
                                                { "uuid", guid },
                                                { "key", "purchase" },
                                            }
                                        } }
                                }
                            } },
                          { "visitor_id", TestUserId },
                          { "attributes", new object[] {} }
                      }
                  } },
                { "project_id", "7720880029" },
                { "account_id", "1592310167" },
                { "client_name", "csharp-sdk" },
                { "client_version", Optimizely.SDK_VERSION },
                { "revision", 15 },
                { "anonymize_ip", false }
            };

            var expectedEvent = new LogEvent(
                "https://logx.optimizely.com/v1/events",
                payloadParams,
                "POST",
                new Dictionary <string, string>
            {
                { "Content-Type", "application/json" }
            });
            var experimentToVariationMap = new Dictionary <string, Variation>
            {
                { "7716830082", new Variation {
                      Id = "7722370027", Key = "control"
                  } }
            };

            var logEvent = EventBuilder.CreateConversionEvent(Config, "purchase", experimentToVariationMap, TestUserId, null, null);

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

            Assert.IsTrue(TestData.CompareObjects(expectedEvent, logEvent));
        }
Пример #3
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)
        {
            if (!IsValid)
            {
                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.ERROR, string.Format("Not tracking user {0} for event {1}.", userId, eventKey));
                return;
            }

            // Filter out experiments that are not running or when user(s) do not meet conditions.
            var validExperimentIdToVariationMap = new Dictionary <string, Variation>();
            var experimentIds = eevent.ExperimentIds;

            foreach (string id in eevent.ExperimentIds)
            {
                var experiment = Config.GetExperimentFromId(id);
                //Validate experiment
                var variation = DecisionService.GetVariation(experiment, userId, userAttributes);

                if (variation != null)
                {
                    validExperimentIdToVariationMap[experiment.Id] = variation;
                }
                else
                {
                    Logger.Log(LogLevel.INFO, string.Format("Not tracking user \"{0}\" for experiment \"{1}\"", userId, experiment.Key));
                }
            }

            if (validExperimentIdToVariationMap.Count > 0)
            {
                if (userAttributes != null)
                {
                    userAttributes = userAttributes.FilterNullValues(Logger);
                }

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

                var conversionEvent = EventBuilder.CreateConversionEvent(Config, eventKey, validExperimentIdToVariationMap,
                                                                         userId, userAttributes, eventTags);
                Logger.Log(LogLevel.INFO, string.Format("Tracking event {0} for user {1}.", eventKey, userId));
                Logger.Log(LogLevel.DEBUG, string.Format("Dispatching conversion event to URL {0} with params {1}.",
                                                         conversionEvent.Url, conversionEvent.GetParamsAsJson()));

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

                NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Track, eventKey, userId,
                                                     userAttributes, eventTags, conversionEvent);
            }
            else
            {
                Logger.Log(LogLevel.INFO, string.Format("There are no valid experiments for event {0} to track.", eventKey));
            }
        }