public async Task <CountlyResponse> RecordOpenViewAsync(string name, bool hasSessionBegunWithView = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(new CountlyResponse
                {
                    IsSuccess = false,
                    ErrorMessage = "View name is required."
                });
            }

            var currentViewSegment =
                new ViewSegment
            {
                Name    = name,
                Segment = Constants.UnityPlatform,
                Visit   = 1,
                Exit    = 0,
                Bounce  = 0,
                HasSessionBegunWithView = hasSessionBegunWithView
            };

            if (!_viewToLastViewStartTime.ContainsKey(name))
            {
                _viewToLastViewStartTime.Add(name, DateTime.UtcNow);
            }

            Debug.Log("[ViewCountlyService] RecordOpenViewAsync: " + name);

            var currentView = new CountlyEventModel(CountlyEventModel.ViewEvent, currentViewSegment.ToDictionary());

            return(await _eventService.RecordEventAsync(currentView));
        }
Beispiel #2
0
        /// <summary>
        /// Report an event to the server with segmentation.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="segmentation"></param>
        /// <param name="useNumberInSameSession"></param>
        /// <param name="count"></param>
        /// <param name="sum"></param>
        /// <param name="duration"></param>
        /// <returns></returns>
        public async Task <CountlyResponse> RecordEventAsync(string key, SegmentModel segmentation, bool useNumberInSameSession = false,
                                                             int?count = 1, double?sum = 0, double?duration = null)
        {
            if (_countlyConfigModel.EnableConsoleLogging)
            {
                Debug.Log("[Countly] RecordEventAsync : key = " + key);
            }

            if (_countlyConfigModel.EnableTestMode)
            {
                return(new CountlyResponse
                {
                    IsSuccess = true
                });
            }

            if (string.IsNullOrEmpty(key) && string.IsNullOrWhiteSpace(key))
            {
                return(new CountlyResponse
                {
                    IsSuccess = false,
                    ErrorMessage = "Key is required."
                });
            }

            var @event = new CountlyEventModel(key, segmentation, count, sum, duration);

            if (useNumberInSameSession)
            {
                _eventNumberInSameSessionHelper.IncreaseNumberInSameSession(@event);
            }

            return(await RecordEventAsync(@event));
        }
        public async Task <CountlyResponse> ReportOpenViewAsync(string name, bool hasSessionBegunWithView = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(new CountlyResponse
                {
                    IsSuccess = false,
                    ErrorMessage = "View name is required."
                });
            }

            var currentViewSegment =
                new ViewSegment
            {
                Name    = name,
                Segment = Constants.UnityPlatform,
                Visit   = 1,
                HasSessionBegunWithView = hasSessionBegunWithView
            };


            _lastViewStartTime = DateTime.UtcNow;

            var currentView = new CountlyEventModel(CountlyEventModel.ViewEvent, currentViewSegment.ToDictionary());

            return(await _eventService.RecordEventAsync(currentView));
        }
        public async Task <CountlyResponse> ReportCloseViewAsync(string name, bool hasSessionBegunWithView = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(new CountlyResponse
                {
                    IsSuccess = false,
                    ErrorMessage = "View name is required."
                });
            }

            var currentViewSegment =
                new ViewSegment
            {
                Name    = name,
                Segment = Constants.UnityPlatform,
                Visit   = 0,
                Exit    = 1,
                HasSessionBegunWithView = hasSessionBegunWithView
            };


            double?duration;

            duration = _lastViewStartTime != null ? (DateTime.UtcNow - _lastViewStartTime.Value).TotalSeconds : (double?)null;
            if (duration.HasValue)
            {
                duration = Math.Round(duration.Value);
            }

            var currentView = new CountlyEventModel(CountlyEventModel.ViewEvent, currentViewSegment.ToDictionary(), 1, null, duration);

            return(await _eventService.RecordEventAsync(currentView));
        }
//        [TestCaseSource(typeof(CountlyEventModelDataProvider), nameof(CountlyEventModelDataProvider.OldEventValidationData))]
        public void UseNumberInSameSession_OldEvent(CountlyEventModel @event)
        {
            const int number = 3;
            var       entity = new EventNumberInSameSessionEntity
            {
                Id       = _auto.NewId(),
                EventKey = @event.Key,
                Number   = 3
            };

            _auto.Insert(Table, entity);

            _eventNumberInSameSessionHelper.IncreaseNumberInSameSession(@event);


            var ql = new StringBuilder().Append("from ").Append(Table)
                     .Append(" where EventKey==?").ToString();

            var entities = _auto.Select <EventNumberInSameSessionEntity>(ql, @event.Key);

            Assert.NotNull(entities);
            Assert.AreEqual(1, entities.Count);
            Assert.AreEqual(@event.Key, entities[0].EventKey);

            Assert.NotNull(@event.Segmentation);
            Assert.True(@event.Segmentation.ContainsKey(EventNumberInSameSessionHelper.NumberInSameSessionSegment));

            Assert.AreEqual(number + 1, entities[0].Number);
            Assert.AreEqual(number + 1, @event.Segmentation[EventNumberInSameSessionHelper.NumberInSameSessionSegment]);
        }
Beispiel #6
0
        private CountlyEventModel ReportViewDuration(bool hasSessionBegunWithView = false)
        {
            if (string.IsNullOrEmpty(_lastView) && string.IsNullOrWhiteSpace(_lastView))
            {
                return(null);
            }

            var viewSegment =
                new ViewSegment
            {
                Name    = _lastView,
                Segment = CountlyHelper.OperationSystem,
                HasSessionBegunWithView = hasSessionBegunWithView
            };

            var customEvent = new CountlyEventModel(CountlyEventModel.ViewEvent,
                                                    (JsonConvert.SerializeObject(viewSegment, Formatting.Indented,
                                                                                 new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore,
            })),
                                                    (DateTime.Now - _lastViewStartTime).TotalMilliseconds
                                                    );

            return(customEvent);
        }
 protected override bool ValidateModelBeforeEnqueue(CountlyEventModel model)
 {
     if (_config.EnableConsoleLogging)
     {
         Debug.Log("[ViewEventRepository] Validate model: \n" + model);
     }
     return(model.Key.Equals(CountlyEventModel.ViewEvent));
 }
 private void AddNumberInSameSessionToEvent(CountlyEventModel @event, int number)
 {
     if (@event.Segmentation == null)
     {
         @event.Segmentation = new SegmentModel();
     }
     @event.Segmentation.Add(NumberInSameSessionSegment, number);
 }
Beispiel #9
0
//        private void SetTimeZoneInfo(CountlyEventModel evt, DateTime requestDatetime)
//        {
//            var timezoneInfo = TimeMetricModel.GetTimeZoneInfoForRequest(requestDatetime);
//            evt.Timestamp = timezoneInfo.Timestamp;
//            evt.DayOfWeek = timezoneInfo.DayOfWeek;
//            evt.Hour = timezoneInfo.Hour;
//            evt.Timezone = timezoneInfo.Timezone;
//        }

        private void AddFirstAppSegment(CountlyEventModel @event)
        {
            if (@event.Segmentation == null)
            {
                @event.Segmentation = new SegmentModel();
            }
            @event.Segmentation.Add(Constants.FirstAppLaunchSegment, FirstLaunchAppHelper.IsFirstLaunchApp);
        }
Beispiel #10
0
        internal async Task <CountlyResponse> RecordEventAsync(CountlyEventModel @event, bool useNumberInSameSession = false)
        {
            if (_countlyConfigModel.EnableConsoleLogging)
            {
                Debug.Log("[Countly] RecordEventAsync : " + @event.ToString());
            }

            if (_countlyConfigModel.EnableTestMode)
            {
                return(new CountlyResponse
                {
                    IsSuccess = true
                });
            }

            if (_countlyConfigModel.EnableFirstAppLaunchSegment)
            {
                AddFirstAppSegment(@event);
            }

            if (@event.Key.Equals(CountlyEventModel.ViewEvent))
            {
                _viewEventRepo.Enqueue(@event);
            }
            else
            {
                _nonViewEventRepo.Enqueue(@event);
            }

            if (useNumberInSameSession)
            {
                _eventNumberInSameSessionHelper.IncreaseNumberInSameSession(@event);
            }

            if (_viewEventRepo.Count >= _countlyConfigModel.EventViewSendThreshold)
            {
                await ReportAllRecordedViewEventsAsync();
            }

            if (_nonViewEventRepo.Count >= _countlyConfigModel.EventNonViewSendThreshold)
            {
                await ReportAllRecordedNonViewEventsAsync();
            }

            return(new CountlyResponse
            {
                IsSuccess = true
            });
        }
        public static EventEntity ConvertEventModelToEventEntity(CountlyEventModel model, long id)
        {
            var json = JsonConvert.SerializeObject(model, Formatting.Indented,
                                                   new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            model.Id = id;

            return(new EventEntity
            {
                Id = id,
                Json = json
            });
        }
Beispiel #12
0
        public async Task <CountlyResponse> RecordEventAsync(string key, bool useNumberInSameSession = false)
        {
            if (string.IsNullOrEmpty(key) && string.IsNullOrWhiteSpace(key))
            {
                return(new CountlyResponse
                {
                    IsSuccess = false,
                    ErrorMessage = "Key is required."
                });
            }

            var @event = new CountlyEventModel(key);

            if (useNumberInSameSession)
            {
                _eventNumberInSameSessionHelper.IncreaseNumberInSameSession(@event);
            }

            return(await RecordEventAsync(@event));
        }
Beispiel #13
0
        /// <summary>
        /// Stop tracking a view
        /// </summary>
        /// <param name="name"></param>
        /// <param name="hasSessionBegunWithView"></param>
        /// <returns></returns>
        public async Task <CountlyResponse> RecordCloseViewAsync(string name, bool hasSessionBegunWithView = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(new CountlyResponse
                {
                    IsSuccess = false,
                    ErrorMessage = "View name is required."
                });
            }

            var currentViewSegment =
                new ViewSegment
            {
                Name    = name,
                Segment = Constants.UnityPlatform,
                Visit   = 0,
                Exit    = 1,
                Bounce  = 0,
                HasSessionBegunWithView = hasSessionBegunWithView
            };

            double?duration = null;

            if (_viewToLastViewStartTime.ContainsKey(name))
            {
                var lastViewStartTime = _viewToLastViewStartTime[name];
                duration = (DateTime.UtcNow - lastViewStartTime).TotalSeconds;

                _viewToLastViewStartTime.Remove(name);
            }

            if (_config.EnableConsoleLogging)
            {
                Debug.Log("[ViewCountlyService] RecordCloseViewAsync: " + name + ", duration: " + duration);
            }

            var currentView = new CountlyEventModel(CountlyEventModel.ViewEvent, currentViewSegment.ToDictionary(), 1, null, duration);

            return(await _eventService.RecordEventAsync(currentView));
        }
Beispiel #14
0
        public async Task <CountlyResponse> RecordEventAsync(string key, IDictionary <string, object> segmentation, bool useNumberInSameSession = false,
                                                             int?count = 1, double?sum = 0, double?duration = null)
        {
            if (string.IsNullOrEmpty(key) && string.IsNullOrWhiteSpace(key))
            {
                return(new CountlyResponse
                {
                    IsSuccess = false,
                    ErrorMessage = "Key is required."
                });
            }

            var @event = new CountlyEventModel(key, segmentation, count, sum, duration);

            if (useNumberInSameSession)
            {
                _eventNumberInSameSessionHelper.IncreaseNumberInSameSession(@event);
            }

            return(await RecordEventAsync(@event));
        }
        private CountlyEventModel GetLastView(bool hasSessionBegunWithView = false)
        {
            if (string.IsNullOrEmpty(_lastView) && string.IsNullOrWhiteSpace(_lastView))
            {
                return(null);
            }

            var viewSegment =
                new ViewSegment
            {
                Name    = _lastView,
                Segment = Constants.UnityPlatform,
                HasSessionBegunWithView = hasSessionBegunWithView
            };

            var customEvent = new CountlyEventModel(
                CountlyEventModel.ViewEvent, viewSegment.ToDictionary(),
                null, null, (DateTime.Now - _lastViewStartTime.Value).TotalSeconds);

            return(customEvent);
        }
Beispiel #16
0
        /// <summary>
        /// Reports a view with the specified name and a last visited view if it existed
        /// </summary>
        /// <param name="name"></param>
        /// <param name="hasSessionBegunWithView"></param>
        public void ReportView(string name, bool hasSessionBegunWithView = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("Parameter name is required.");
            }

            var events   = new List <CountlyEventModel>();
            var lastView = ReportViewDuration();

            if (lastView != null)
            {
                events.Add(lastView);
            }

            var currentViewSegment =
                new ViewSegment
            {
                Name    = name,
                Segment = CountlyHelper.OperationSystem,
                Visit   = 1,
                HasSessionBegunWithView = hasSessionBegunWithView
            };

            var currentView = new CountlyEventModel(CountlyEventModel.ViewEvent,
                                                    (JsonConvert.SerializeObject(currentViewSegment, Formatting.Indented,
                                                                                 new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore,
            })),
                                                    null
                                                    );

            events.Add(currentView);

            CountlyEventModel.StartMultipleEvents(events);

            _lastView          = name;
            _lastViewStartTime = DateTime.Now;
        }
        /// <summary>
        /// Reports a view with the specified name and a last visited view if it existed
        /// </summary>
        /// <param name="name"></param>
        /// <param name="hasSessionBegunWithView"></param>
        public async Task <CountlyResponse> ReportViewAsync(string name, bool hasSessionBegunWithView = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(new CountlyResponse
                {
                    IsSuccess = false,
                    ErrorMessage = "View name is required."
                });
            }

            var events   = new List <CountlyEventModel>();
            var lastView = GetLastView();

            Debug.Log("[ReportViewAsync] get last view: " + lastView);
            if (lastView != null)
            {
                events.Add(lastView);
            }

            var currentViewSegment =
                new ViewSegment
            {
                Name    = name,
                Segment = Constants.UnityPlatform,
                Visit   = 1,
                HasSessionBegunWithView = hasSessionBegunWithView
            };

            var currentView = new CountlyEventModel(CountlyEventModel.ViewEvent, currentViewSegment.ToDictionary());

            events.Add(currentView);

            _lastView          = name;
            _lastViewStartTime = DateTime.Now;

            return(await _eventService.ReportMultipleEventsAsync(events));
        }
Beispiel #18
0
        /// <summary>
        /// Reports a particular action with the specified details
        /// </summary>
        /// <param name="type"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public bool ReportAction(string type, int x, int y, int width, int height)
        {
            var segment =
                new ActionSegment
            {
                Type      = type,
                PositionX = x,
                PositionY = y,
                Width     = width,
                Height    = height
            };

            var action = new CountlyEventModel(CountlyEventModel.ViewActionEvent,
                                               (JsonConvert.SerializeObject(segment, Formatting.Indented,
                                                                            new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore,
            })),
                                               null
                                               );

            action.ReportCustomEvent();
            return(true);
        }
Beispiel #19
0
        public async Task <CountlyResponse> RecordEventAsync(CountlyEventModel @event, bool useNumberInSameSession = false)
        {
            if (_countlyConfigModel.EnableFirstAppLaunchSegment && FirstLaunchAppHelper.IsFirstLaunchApp)
            {
                AddFirstAppSegment(@event);
            }


            if (@event.Key.Equals(CountlyEventModel.ViewEvent))
            {
                _viewEventRepo.Enqueue(@event);
            }
            else
            {
                _nonViewEventRepo.Enqueue(@event);
            }

            if (useNumberInSameSession)
            {
                _eventNumberInSameSessionHelper.IncreaseNumberInSameSession(@event);
            }

            if (_viewEventRepo.Count >= _countlyConfigModel.EventViewSendThreshold)
            {
                await ReportAllRecordedViewEventsAsync();
            }

            if (_nonViewEventRepo.Count >= _countlyConfigModel.EventNonViewSendThreshold)
            {
                await ReportAllRecordedNonViewEventsAsync();
            }

            return(new CountlyResponse
            {
                IsSuccess = true
            });
        }
Beispiel #20
0
        /// <summary>
        ///     Reports a custom event to the Counlty server.
        /// </summary>
        /// <returns></returns>
        public async Task <CountlyResponse> ReportCustomEventAsync(string key,
                                                                   IDictionary <string, object> segmentation = null,
                                                                   int?count = 1, double?sum = null, double?duration = null)
        {
            if (string.IsNullOrEmpty(key) && string.IsNullOrWhiteSpace(key))
            {
                return new CountlyResponse
                       {
                           IsSuccess    = false,
                           ErrorMessage = "Key is required."
                       }
            }
            ;

            var evt = new CountlyEventModel(key, segmentation, count, sum, duration);

            if (_countlyConfigModel.EnableFirstAppLaunchSegment)
            {
                AddFirstAppSegment(evt);
            }
//            SetTimeZoneInfo(evt, DateTime.UtcNow);

            var requestParams =
                new Dictionary <string, object>
            {
                {
                    "events", JsonConvert.SerializeObject(new List <CountlyEventModel> {
                        evt
                    }, Formatting.Indented,
                                                          new JsonSerializerSettings {
                        NullValueHandling = NullValueHandling.Ignore
                    })
                }
            };

            return(await _requestCountlyHelper.GetResponseAsync(requestParams));
        }
Beispiel #21
0
 /// <summary>
 /// Adds an event with the specified key. Doesn't send the request to the Countly API
 /// </summary>
 /// <param name="key"></param>
 public void StartEvent(string key)
 {
     _countlyEventModel = new CountlyEventModel(key);
 }
        public void IncreaseNumberInSameSession(CountlyEventModel @event)
        {
            var entity = IncrementEventNumberInSameSessionAndSaveOrUpdate(@event.Key);

            AddNumberInSameSessionToEvent(@event, entity.Number);
        }
Beispiel #23
0
 protected override bool ValidateModelBeforeEnqueue(CountlyEventModel model)
 {
     return(!model.Key.Equals(CountlyEventModel.ViewEvent));
 }
Beispiel #24
0
 public Task <CountlyResponse> RecordEventAsync(CountlyEventModel @event, bool useNumberInSameSession = false)
 {
     Debug.Log("[EventCountlyServiceWrapper] RecordEventAsync: " + @event);
     return(Task.FromResult(new CountlyResponse()));
 }
 protected override bool ValidateModelBeforeEnqueue(CountlyEventModel model)
 {
     Debug.Log("[ViewEventRepository] Validate model: \n" + model);
     return(model.Key.Equals(CountlyEventModel.ViewEvent));
 }