Ejemplo n.º 1
0
        /// <summary>
        /// Starts Countly tracking session.
        /// Call from your App.xaml.cs Application_Launching and Application_Activated events.
        /// Must be called before other SDK methods can be used.
        /// </summary>
        /// <param name="serverUrl">URL of the Countly server to submit data to; use "https://cloud.count.ly" for Countly Cloud</param>
        /// <param name="appKey">app key for the application being tracked; find in the Countly Dashboard under Management > Applications</param>
        public static async Task StartSession(string serverUrl, string appKey)
        {
            if (String.IsNullOrWhiteSpace(serverUrl))
            {
                throw new ArgumentException("invalid server url");
            }

            if (String.IsNullOrWhiteSpace(appKey))
            {
                throw new ArgumentException("invalid application key");
            }

            ServerUrl = serverUrl;
            AppKey    = appKey;

            Events.Clear();

            startTime    =
                lastTime = DateTime.UtcNow;

            var interval = TimeSpan.FromSeconds(updateInterval);

            Timer = new Timer(UpdateSession, null, interval, interval);

            var view = lastView;

            if (view != null)
            {
                view.Time = DateTime.UtcNow;
                AddEvent(new CountlyEvent(ViewEvent, 1, null, null, view.Segmentation));
            }

            await Upload(CountlyRequest.CreateBeginSession());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// End Countly tracking session.
        /// Call from your App.xaml.cs Application_Deactivated and Application_Closing events.
        /// </summary>
        public static async Task EndSession()
        {
            if (Timer != null)
            {
                Timer.Dispose();
                Timer = null;
            }

            await Upload(CountlyRequest.CreateEndSession());
        }
Ejemplo n.º 3
0
        private static async Task <bool> Upload(CountlyRequest request)
        {
            if (string.IsNullOrWhiteSpace(ServerUrl))
            {
                throw new InvalidOperationException("session is not active");
            }

            var requests = new List <CountlyRequest>();

            lock (sync)
            {
                if (UserDetails.HasChanges)
                {
                    request.UserDetails    = UserDetails;
                    UserDetails.HasChanges = false;
                }

                requests.AddRange(Events);
                Events.Clear();
            }

            requests.Add(request);

            try
            {
                var response = await Api.Call <ResultResponse>(ServerUrl, requests);

                if (!response.IsSuccess)
                {
                    OnFailed(requests);
                }
                return(response.IsSuccess);
            }
            catch
            {
                OnFailed(requests);
                throw;
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Upload sessions, events & exception queues
 /// </summary>
 /// <returns>True if success</returns>
 public static Task <bool> Upload()
 {
     return(Upload(CountlyRequest.CreateUpdateSession()));
 }