/// <summary> /// Resume the current session. /// ResumeSession() is the entry point into the Amazon Mobile Analytics SDK where sessions can be resumed. Session is created and started immediately /// after instantiating the MobileAnalyticsManager object. The session remains active until it is paused. When in a paused state, the session time will /// not accumulate. When resuming a session, if enough time has elapsed from when the session is paused to when it's resumed, the session is ended and /// a new session is created and started. Otherwise, the paused session is resumed and the session time continues to accumulate. Currently session /// time out default value is 5 seconds. /// /// For example, on Android platform, when MobileAnalyticsManager is first instantiated, it creates Session 1. As the user transitions from activity to activity, the old /// activity will pause the current session, and the new activity will immediately resume the current session. In this case, Session 1 remains active /// and accumulates session time. The user continues to use the App for a total of 3 minutes, at which point, the user receives a phone call. /// When transitioning to the phone call, the current activity will pause the session and then transition to the phone app. In this case Session 1 /// remains paused while the phone call is in progress and session time does not accumulate. After completing the phone call a few minutes later, /// the user returns to the App and the activity will resume Session 1. Since enough time has elapsed since resuming Session 1, Session 1 will be ended /// with a play time of 3 minutes. Session 2 will then be immediately created and started. /// /// In order for MobileAnalyticsManager to track sessions, you must call the PauseSession() and ResumeSession() in each activity of your app. /// <example> /// The example below shows how to pause and resume session in Xamarin Android /// <code> ///public class MainActivity : Activity ///{ /// private static MobileAnalyticsManager _manager = null; /// /// protected override void OnCreate(Bundle bundle) /// { /// _manager = MobileAnalyticsManager.GetOrCreateInstance(YourAppId, YourCredential, RegionEndpoint.USEast1, YourConfig); /// base.OnCreate(bundle); /// } /// protected override void OnResume() /// { /// await _manager.ResumeSession(); /// base.OnResume(); /// } /// protected override void OnPause() /// { /// await _manager.PauseSession(); /// base.OnPause(); /// } ///} /// </code> /// </example> /// </summary> public void ResumeSession() { try { Session.Resume(); } catch (Exception e) { _logger.Error(e, "An exception occurred when resume session."); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when resuming session.", e, new List <Amazon.MobileAnalytics.Model.Event>()); OnRaiseErrorEvent(eventArgs); } }
internal void OnRaiseErrorEvent(MobileAnalyticsErrorEventArgs eventArgs) { AWSSDKUtils.InvokeInBackground(MobileAnalyticsErrorEvent, eventArgs, this); }
/// <summary> /// Resume the current session. /// ResumeSession() is the entry point into the Amazon Mobile Analytics SDK where sessions can be resumed. Session is created and started immediately /// after instantiating the MobileAnalyticsManager object. The session remains active until it is paused. When in a paused state, the session time will /// not accumulate. When resuming a session, if enough time has elapsed from when the session is paused to when it's resumed, the session is ended and /// a new session is created and started. Otherwise, the paused session is resumed and the session time continues to accumulate. Currently session /// time out default value is 5 seconds. /// /// For example, on Android platform, when MobileAnalyticsManager is first instantiated, it creates Session 1. As the user transitions from activity to activity, the old /// activity will pause the current session, and the new activity will immediately resume the current session. In this case, Session 1 remains active /// and accumulates session time. The user continues to use the App for a total of 3 minutes, at which point, the user receives a phone call. /// When transitioning to the phone call, the current activity will pause the session and then transition to the phone app. In this case Session 1 /// remains paused while the phone call is in progress and session time does not accumulate. After completing the phone call a few minutes later, /// the user returns to the App and the activity will resume Session 1. Since enough time has elapsed since resuming Session 1, Session 1 will be ended /// with a play time of 3 minutes. Session 2 will then be immediately created and started. /// /// In order for MobileAnalyticsManager to track sessions, you must call the PauseSession() and ResumeSession() in each activity of your app. /// <example> /// The example below shows how to pause and resume session in Xamarin Android /// <code> ///public class MainActivity : Activity ///{ /// private static MobileAnalyticsManager _manager = null; /// /// protected override void OnCreate(Bundle bundle) /// { /// _manager = MobileAnalyticsManager.GetOrCreateInstance(YourAppId, YourCredential, RegionEndpoint.USEast1, YourConfig); /// base.OnCreate(bundle); /// } /// protected override void OnResume() /// { /// await _manager.ResumeSession(); /// base.OnResume(); /// } /// protected override void OnPause() /// { /// await _manager.PauseSession(); /// base.OnPause(); /// } ///} /// </code> /// </example> /// </summary> public void ResumeSession() { try { Session.Resume(); } catch (Exception e) { _logger.Error(e, "An exception occurred when resume session."); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when resuming session.", e, new List<Amazon.MobileAnalytics.Model.Event>()); OnRaiseErrorEvent(eventArgs); } }
/// <summary> /// Enqueues the events for delivery. The event is stored in an instance of <see cref="Amazon.MobileAnalytics.MobileAnalyticsManager.Internal.IEventStore"/>. /// </summary> /// <param name="eventObject">Event object.<see cref="Amazon.MobileAnalytics.Model.Event"/></param> public void EnqueueEventsForDelivery(Amazon.MobileAnalytics.Model.Event eventObject) { #if BCL35 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { #elif PCL || BCL45 Task.Run(() => { #endif string eventString = null; try { eventString = JsonMapper.ToJson(eventObject); } catch (Exception e) { _logger.Error(e, "An exception occurred when converting low level client event to json string."); List<Amazon.MobileAnalytics.Model.Event> eventList = new List<Amazon.MobileAnalytics.Model.Event>(); eventList.Add(eventObject); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when converting low level client event to json string.", e, eventList); _maManager.OnRaiseErrorEvent(eventArgs); } if (null != eventString) { try { _eventStore.PutEvent(eventString, _appID); } catch (Exception e) { _logger.Error(e, "Event {0} was not stored.", eventObject.EventType); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when storing event into event store.", e, new List<Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } _logger.DebugFormat("Event {0} is queued for delivery", eventObject.EventType); } #if BCL35 })); #elif PCL || BCL45 }); #endif }
private async Task SubmitEvents(List<string> rowIds, List<Amazon.MobileAnalytics.Model.Event> eventList) #endif { PutEventsRequest putRequest = new PutEventsRequest(); putRequest.Events = eventList; putRequest.ClientContext = Convert.ToBase64String( System.Text.Encoding.UTF8.GetBytes(_clientContext.ToJsonString())); putRequest.ClientContextEncoding = "base64"; _logger.DebugFormat("Client Context is : {0}", _clientContext.ToJsonString()); PutEventsResponse resp = null; try { #if BCL35 resp = _mobileAnalyticsLowLevelClient.PutEvents(putRequest); #elif PCL || BCL45 resp = await _mobileAnalyticsLowLevelClient.PutEventsAsync(putRequest).ConfigureAwait(false); #endif } catch (AmazonMobileAnalyticsException e) { _logger.Error(e, "An AmazonMobileAnalyticsException occurred while sending Amazon Mobile Analytics request: error code is {0} ; error type is {1} ; request id is {2} ; status code is {3} ; error message is {4}", e.ErrorCode, e.ErrorType, e.RequestId, e.StatusCode, e.Message); // Delete events in any of the three error codes. if (e.StatusCode == HttpStatusCode.BadRequest && (e.ErrorCode.Equals("ValidationException", StringComparison.CurrentCultureIgnoreCase) || e.ErrorCode.Equals("SerializationException", StringComparison.CurrentCultureIgnoreCase) || e.ErrorCode.Equals("BadRequestException", StringComparison.CurrentCultureIgnoreCase))) { MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "Amazon Mobile Analytics Service returned an error.", e, eventList); _maManager.OnRaiseErrorEvent(eventArgs); _logger.InfoFormat("The error code is not retriable. Delete {0} events from local storage.", rowIds.Count); _eventStore.DeleteEvent(rowIds); } else { MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "Amazon Mobile Analytics Service returned an error.", e, new List<Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } } catch (AmazonServiceException e) { _logger.Error(e, "An AmazonServiceException occurred while sending Amazon Mobile Analytics request: error code is {0} ; error type is {1} ; request id is {2} ; status code is {3} ; error message is {4} ", e.ErrorCode, e.ErrorType, e.RequestId, e.StatusCode, e.Message); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "Amazon Web Service returned an error.", e, new List<Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } catch (Exception e) { _logger.Error(e, "An exception occurred while sending Amazon Mobile Analytics request."); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when sending request to Amazon Mobile Analytics.", e, new List<Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } finally { if (resp != null && resp.HttpStatusCode == HttpStatusCode.Accepted) { _logger.InfoFormat("Mobile Analytics client successfully delivered {0} events to service. Delete those events from local storage.", rowIds.Count); _eventStore.DeleteEvent(rowIds); } } }
private void EnqueueEventsHelper(Amazon.MobileAnalytics.Model.Event eventObject) { string eventString = null; try { eventString = JsonMapper.ToJson(eventObject); } catch (Exception e) { _logger.Error(e, "An exception occurred when converting low level client event to json string."); List<Amazon.MobileAnalytics.Model.Event> eventList = new List<Amazon.MobileAnalytics.Model.Event>(); eventList.Add(eventObject); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when converting low level client event to json string.", e, eventList); _maManager.OnRaiseErrorEvent(eventArgs); } if (null != eventString) { try { _eventStore.PutEvent(eventString, _appID); } catch (Exception e) { _logger.Error(e, "Event {0} was not stored.", eventObject.EventType); MobileAnalyticsErrorEventArgs eventArgs = new MobileAnalyticsErrorEventArgs(this.GetType().Name, "An exception occurred when storing event into event store.", e, new List<Amazon.MobileAnalytics.Model.Event>()); _maManager.OnRaiseErrorEvent(eventArgs); } _logger.DebugFormat("Event {0} is queued for delivery", eventObject.EventType); } }