Ejemplo n.º 1
0
        /// <summary>
        /// Create Visitor instance
        /// </summary>
        /// <param name="conversionEvent">The ConversionEvent entity</param>
        /// <param name="logger">The ILogger entity</param>
        /// <returns>Visitor instance if ConversionEvent is valid, null otherwise</returns>
        private static Visitor CreateVisitor(ConversionEvent conversionEvent, ILogger logger)
        {
            if (conversionEvent == null)
            {
                return(null);
            }

            EventContext  userContext   = conversionEvent.Context;
            var           revenue       = EventTagUtils.GetRevenueValue(conversionEvent.EventTags, logger) as int?;
            var           value         = EventTagUtils.GetNumericValue(conversionEvent.EventTags, logger) as float?;
            SnapshotEvent snapshotEvent = new SnapshotEvent.Builder()
                                          .WithUUID(conversionEvent.UUID)
                                          .WithEntityId(conversionEvent.Event.Id)
                                          .WithKey(conversionEvent.Event?.Key)
                                          .WithTimeStamp(conversionEvent.Timestamp)
                                          .WithRevenue(revenue)
                                          .WithValue(value)
                                          .WithEventTags(conversionEvent.EventTags)
                                          .Build();


            Snapshot snapshot = new Snapshot(new SnapshotEvent[] { snapshotEvent });

            var visitor = new Visitor(new Snapshot[] { snapshot }, conversionEvent.VisitorAttributes, conversionEvent.UserId);

            return(visitor);
        }
Ejemplo n.º 2
0
        public void TestGetRevenueValue()
        {
            var expectedValue       = 42;
            var expectedValue2      = 100;
            var expectedValueString = 123;
            var validTag            = new Dictionary <string, object>()
            {
                { "revenue", 42 }
            };
            var validTag2 = new Dictionary <string, object>()
            {
                { "revenue", 100 }
            };
            var validTagStringValue = new Dictionary <string, object>()
            {
                { "revenue", "123" }
            };

            var invalidTag = new Dictionary <string, object>()
            {
                { "abc", 42 }
            };
            var nullValue = new Dictionary <string, object>()
            {
                { "revenue", null }
            };
            var invalidValue = new Dictionary <string, object>()
            {
                { "revenue", 42.5 }
            };
            var invalidTagNonRevenue = new Dictionary <string, object>()
            {
                { "non-revenue", 123 }
            };

            // Invalid data.
            Assert.Null(EventTagUtils.GetRevenueValue(null, Logger));
            Assert.Null(EventTagUtils.GetRevenueValue(invalidTag, Logger));
            Assert.Null(EventTagUtils.GetRevenueValue(nullValue, Logger));
            Assert.Null(EventTagUtils.GetRevenueValue(invalidValue, Logger));
            Assert.Null(EventTagUtils.GetRevenueValue(invalidTagNonRevenue, Logger));

            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "Event tags is undefined."), Times.Once);
            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "The revenue key is not defined in the event tags."), Times.Exactly(2));
            LoggerMock.Verify(l => l.Log(LogLevel.DEBUG, "The revenue key value is not defined in event tags."), Times.Once);
            LoggerMock.Verify(l => l.Log(LogLevel.ERROR, "Revenue value is not an integer or couldn't be parsed as an integer."), Times.Once);

            // Valid data.
            Assert.AreEqual(EventTagUtils.GetRevenueValue(validTag, Logger), expectedValue);
            Assert.AreEqual(EventTagUtils.GetRevenueValue(validTag2, Logger), expectedValue2);
            Assert.AreEqual(EventTagUtils.GetRevenueValue(validTagStringValue, Logger), expectedValueString);

            LoggerMock.Verify(l => l.Log(LogLevel.INFO, $"The revenue value {expectedValue} will be sent to results."), Times.Once);
            LoggerMock.Verify(l => l.Log(LogLevel.INFO, $"The revenue value {expectedValue2} will be sent to results."), Times.Once);
            LoggerMock.Verify(l => l.Log(LogLevel.INFO, $"The revenue value {expectedValueString} will be sent to results."), Times.Once);
        }
Ejemplo n.º 3
0
        private List <object> GetConversionParams(ProjectConfig config, string eventKey, string userId, Dictionary <string, object> eventTags)
        {
            var conversionEventParams = new List <object>();
            var snapshot = new Dictionary <string, object>();

            var eventDict = new Dictionary <string, object>
            {
                { Params.ENTITY_ID, config.EventKeyMap[eventKey].Id },
                { Params.TIMESTAMP, DateTimeUtils.SecondsSince1970 *1000 },
                { "uuid", Guid.NewGuid() },
                { "key", eventKey }
            };

            if (eventTags != null)
            {
                var revenue = EventTagUtils.GetRevenueValue(eventTags, Logger);

                if (revenue != null)
                {
                    eventDict[EventTagUtils.REVENUE_EVENT_METRIC_NAME] = revenue;
                }

                var eventVallue = EventTagUtils.GetNumericValue(eventTags, Logger);

                if (eventVallue != null)
                {
                    eventDict[EventTagUtils.VALUE_EVENT_METRIC_NAME] = eventVallue;
                }

                if (eventTags.Any())
                {
                    eventDict["tags"] = eventTags;
                }
            }

            snapshot[Params.EVENTS] = new object[] {
                eventDict
            };

            conversionEventParams.Add(snapshot);

            return(conversionEventParams);
        }
Ejemplo n.º 4
0
        public void TestGetRevenueValue()
        {
            var expectedValue  = 42;
            var expectedValue2 = 100;
            var validTag       = new Dictionary <string, object>()
            {
                { "revenue", 42 }
            };
            var validTag2 = new Dictionary <string, object>()
            {
                { "revenue", 100 }
            };

            var invalidTag = new Dictionary <string, object>()
            {
                { "abc", 42 }
            };
            var nullValue = new Dictionary <string, object>()
            {
                { "revenue", null }
            };
            var invalidValue = new Dictionary <string, object>()
            {
                { "revenue", 42.5 }
            };

            // Invalid data.
            Assert.Null(EventTagUtils.GetRevenueValue(null));
            Assert.Null(EventTagUtils.GetRevenueValue(invalidTag));
            Assert.Null(EventTagUtils.GetRevenueValue(nullValue));
            Assert.Null(EventTagUtils.GetRevenueValue(invalidValue));

            // Valid data.
            Assert.AreEqual(EventTagUtils.GetRevenueValue(validTag), expectedValue);
            Assert.AreEqual(EventTagUtils.GetRevenueValue(validTag2), expectedValue2);
        }
Ejemplo n.º 5
0
        private List <object> GetConversionParams(ProjectConfig config, string eventKey, Dictionary <string, Variation> experimentIdVariationMap, string userId, Dictionary <string, object> eventTags)
        {
            var conversionEventParams = new List <object>();

            foreach (var experimentId in experimentIdVariationMap.Keys)
            {
                var variation   = experimentIdVariationMap[experimentId];
                var experiment  = config.ExperimentIdMap[experimentId];
                var eventEntity = config.EventKeyMap[eventKey];

                if (string.IsNullOrEmpty(variation.Key))
                {
                    continue;
                }
                var decision = new Dictionary <string, object>
                {
                    {
                        Params.DECISIONS, new object[]
                        {
                            new Dictionary <string, object>
                            {
                                { Params.CAMPAIGN_ID, experiment.LayerId },
                                { Params.EXPERIMENT_ID, experiment.Id },
                                { Params.VARIATION_ID, variation.Id }
                            }
                        }
                    },
                    {
                        Params.EVENTS, new object[0]
                    }
                };

                var eventDict = new Dictionary <string, object>
                {
                    { Params.ENTITY_ID, eventEntity.Id },
                    { Params.TIMESTAMP, SecondsSince1970 *1000 },
                    { "uuid", Guid.NewGuid() },
                    { "key", eventKey }
                };

                if (eventTags != null)
                {
                    var revenue = EventTagUtils.GetRevenueValue(eventTags, Logger);

                    if (revenue != null)
                    {
                        eventDict[EventTagUtils.REVENUE_EVENT_METRIC_NAME] = revenue;
                    }

                    var eventVallue = EventTagUtils.GetNumericValue(eventTags, Logger);

                    if (eventVallue != null)
                    {
                        eventDict[EventTagUtils.VALUE_EVENT_METRIC_NAME] = eventVallue;
                    }

                    eventDict["tags"] = eventTags;
                }

                decision[Params.EVENTS] = new object[] { eventDict };

                conversionEventParams.Add(decision);
            }

            return(conversionEventParams);
        }