示例#1
0
        /// <summary>
        /// Tries to send locally stored elements if any and if there's a network connection.
        /// This method is called automatically when a new instance of a MixpanelClient is created.
        /// </summary>
        /// <returns></returns>
        public async Task TrySendLocalElements()
        {
            bool          storeUpdated = false;
            MixpanelStore store        = null;
            await Task.Run(() => store = MixpanelStore.Load());

            if (store.Events.Count > 0)
            {
                IList <TrackingEvent> sentItems = await Track <TrackingEvent>(store.Events);

                foreach (TrackingEvent item in sentItems)
                {
                    store.Events.Remove(item);
                }
                storeUpdated = true;
            }
            if (store.ProfileUpdates.Count > 0)
            {
                IList <ProfileUpdate> sentItems = await Track <ProfileUpdate>(store.ProfileUpdates);

                foreach (ProfileUpdate item in sentItems)
                {
                    store.ProfileUpdates.Remove(item);
                }
                storeUpdated = true;
            }

            if (storeUpdated)
            {
                await Task.Run(() => store.Save());
            }
        }
示例#2
0
 /// <summary>
 /// Saves element locally. Event will be sent on next call of "TrySendLocalElements".
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="element">The element.</param>
 /// <returns></returns>
 public async Task SaveElement <T>(T element) where T : MixpanelEntity
 {
     // Store uses a mutex, load & save on a background thread to avoid blocking UI.
     await Task.Run(() =>
     {
         MixpanelStore store = MixpanelStore.Load();
         if (!store.Contains(element))
         {
             store.Add(element);
         }
         store.Save();
     });
 }
示例#3
0
            internal static MixpanelStore Load()
            {
                MixpanelStore store = null;

                try
                {
                    // Wait until it is safe to enter.
                    _mutex.WaitOne();

                    Task <StorageFile> fileTask = Utilities.SafeGetFile(ApplicationData.Current.LocalFolder, FileName);
                    StorageFile        file     = fileTask.Result;
                    if (file == null)
                    {
                        return(new MixpanelStore());
                    }

                    Task <Stream> streamTask = file.OpenStreamForReadAsync();
                    using (StreamReader reader = new StreamReader(streamTask.Result, Utilities.DefaultEncoding))
                    {
                        string content = reader.ReadToEnd();
                        store = JsonConvert.DeserializeObject <MixpanelStore>(content);
                    }
                }
                catch (Exception ex)
                {
                    // File is invalid: schema might have changed or something like that.
                    // Not a problem, let's return a brand new one.
                    Debug.WriteLine("Mixpanel> Could not load mixpanel events: " + ex.ToString());
                }
                finally
                {
                    _mutex.ReleaseMutex();
                }

                if (store == null)
                {
                    store = new MixpanelStore();
                }
                return(store);
            }