PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Localytics/LocalyticsSession.h

https://github.com/emersonbroga/Gorillas
C Header | 201 lines | 37 code | 19 blank | 145 comment | 0 complexity | 93c8fae4dd8f7b6d59c111e53c7b8dff MD5 | raw file
  1. // LocalyticsSession.h
  2. // Copyright (C) 2009 Char Software Inc., DBA Localytics
  3. //
  4. // This code is provided under the Localytics Modified BSD License.
  5. // A copy of this license has been distributed in a file called LICENSE
  6. // with this source code.
  7. //
  8. // Please visit www.localytics.com for more information.
  9. #import <UIKit/UIKit.h>
  10. // Set this to true to enable localytics traces (useful for debugging)
  11. #define DO_LOCALYTICS_LOGGING false
  12. /*!
  13. @class LocalyticsSession
  14. @discussion The class which manages creating, collecting, & uploading a Localytics session.
  15. Please see the following guides for information on how to best use this
  16. library, sample code, and other useful information:
  17. <ul>
  18. <li><a href="http://wiki.localytics.com/index.php?title=Developer's_Integration_Guide">Main Developer's Integration Guide</a></li>
  19. </ul>
  20. <strong>Best Practices</strong>
  21. <ul>
  22. <li>Instantiate the LocalyticsSession object in applicationDidFinishLaunching.</li>
  23. <li>Open your session and begin your uploads in applicationDidFinishLaunching. This way the
  24. upload has time to complete and it all happens before your users have a
  25. chance to begin any data intensive actions of their own.</li>
  26. <li>Close the session in applicationWillTerminate, and in applicationDidEnterBackground.</li>
  27. <li>Resume the session in applicationWillEnterForeground.</li>
  28. <li>Do not call any Localytics functions inside a loop. Instead, calls
  29. such as <code>tagEvent</code> should follow user actions. This limits the
  30. amount of data which is stored and uploaded.</li>
  31. <li>Do not use multiple LocalticsSession objects to upload data with
  32. multiple application keys. This can cause invalid state.</li>
  33. </ul>
  34. @author Localytics
  35. @version 2.0
  36. */
  37. @interface LocalyticsSession : NSObject {
  38. BOOL _hasInitialized; // Whether or not the session object has been initialized.
  39. BOOL _isSessionOpen; // Whether or not this session has been opened.
  40. float _backgroundSessionTimeout; // If an App stays in the background for more
  41. // than this many seconds, start a new session
  42. // when it returns to foreground.
  43. @private
  44. #pragma mark Member Variables
  45. NSString *_sessionUUID; // Unique identifier for this session.
  46. NSString *_applicationKey; // Unique identifier for the instrumented application
  47. NSTimeInterval _lastSessionStartTimestamp; // The start time of the most recent session.
  48. NSDate *_sessionResumeTime; // Time session was started or resumed.
  49. NSDate *_sessionCloseTime; // Time session was closed.
  50. NSMutableString *_newFlowEvents; // Comma-delimited list of app screens and events tagged during this
  51. // session that have NOT been staged for upload.
  52. NSMutableString *_oldFlowEvents; // App screens and events tagged during this session that HAVE been staged
  53. // for upload.
  54. NSMutableString *_screens; // Comma-delimited list of screens tagged during this session.
  55. NSTimeInterval _sessionActiveDuration; // Duration that session open.
  56. BOOL _sessionHasBeenOpen; // Whether or not this session has ever been open.
  57. }
  58. @property BOOL isSessionOpen;
  59. @property BOOL hasInitialized;
  60. @property float backgroundSessionTimeout;
  61. #pragma mark Public Methods
  62. /*!
  63. @method sharedLocalyticsSession
  64. @abstract Accesses the Session object. This is a Singleton class which maintains
  65. a single session throughout your application. It is possible to manage your own
  66. session, but this is the easiest way to access the Localytics object throughout your code.
  67. The class is accessed within the code using the following syntax:
  68. [[LocalyticsSession sharedLocalyticsSession] functionHere]
  69. So, to tag an event, all that is necessary, anywhere in the code is:
  70. [[LocalyticsSession sharedLocalyticsSession] tagEvent:@"MY_EVENT"];
  71. */
  72. + (LocalyticsSession *)sharedLocalyticsSession;
  73. /*!
  74. @method LocalyticsSession
  75. @abstract Initializes the Localytics Object. Not necessary if you choose to use startSession.
  76. @param applicationKey The key unique for each application generated at www.localytics.com
  77. */
  78. - (void)LocalyticsSession:(NSString *)appKey;
  79. /*!
  80. @method startSession
  81. @abstract An optional convenience initialize method that also calls the LocalyticsSession, open &
  82. upload methods. Best Practice is to call open & upload immediately after Localytics Session when loading an app,
  83. this method fascilitates that behavior.
  84. It is recommended that this call be placed in <code>applicationDidFinishLaunching</code>.
  85. @param applicationKey The key unique for each application generated
  86. at www.localytics.com
  87. */
  88. - (void)startSession:(NSString *)appKey;
  89. /*!
  90. @method setOptIn
  91. @abstract (OPTIONAL) Allows the application to control whether or not it will collect user data.
  92. Even if this call is used, it is necessary to continue calling upload(). No new data will be
  93. collected, so nothing new will be uploaded but it is necessary to upload an event telling the
  94. server this user has opted out.
  95. @param optedIn True if the user is opted in, false otherwise.
  96. */
  97. - (void)setOptIn:(BOOL)optedIn;
  98. /*!
  99. @method isOptedIn
  100. @abstract (OPTIONAL) Whether or not this user has is opted in or out. The only way they can be
  101. opted out is if setOptIn(false) has been called before this. This function should only be
  102. used to pre-populate a checkbox in an options menu. It is not recommended that an application
  103. branch based on Localytics instrumentation because this creates an additional test case. If
  104. the app is opted out, all localytics calls will return immediately.
  105. @result true if the user is opted in, false otherwise.
  106. */
  107. - (BOOL)isOptedIn;
  108. /*!
  109. @method open
  110. @abstract Opens the Localytics session. Not necessary if you choose to use startSession.
  111. The session time as presented on the website is the time between <code>open</code> and the
  112. final <code>close</code> so it is recommended to open the session as early as possible, and close
  113. it at the last moment. The session must be opened before any tags can
  114. be written. It is recommended that this call be placed in <code>applicationDidFinishLaunching</code>.
  115. <br>
  116. If for any reason this is called more than once every subsequent open call
  117. will be ignored.
  118. */
  119. - (void)open;
  120. /*!
  121. @method resume
  122. @abstract Resumes the Localytics session. When the App enters the background, the session is
  123. closed and the time of closing is recorded. When the app returns to the foreground, the session
  124. is resumed. If the time since closing is greater than BACKGROUND_SESSION_TIMEOUT, (15 seconds
  125. by default) a new session is created, and uploading is triggered. Otherwise, the previous session
  126. is reopened.
  127. */
  128. - (void)resume;
  129. /*!
  130. @method close
  131. @abstract Closes the Localytics session. This should be called in
  132. <code>applicationWillTerminate</code>.
  133. <br>
  134. If close is not called, the session will still be uploaded but no
  135. events will be processed and the session time will not appear. This is
  136. because the session is not yet closed so it should not be used in
  137. comparison with sessions which are closed.
  138. */
  139. - (void)close;
  140. /*!
  141. @method tagEvent
  142. @abstract Allows a session to tag a particular event as having occurred. For
  143. example, if a view has three buttons, it might make sense to tag
  144. each button click with the name of the button which was clicked.
  145. For another example, in a game with many levels it might be valuable
  146. to create a new tag every time the user gets to a new level in order
  147. to determine how far the average user is progressing in the game.
  148. <br>
  149. <strong>Tagging Best Practices</strong>
  150. <ul>
  151. <li>DO NOT use tags to record personally identifiable information.</li>
  152. <li>The best way to use tags is to create all the tag strings as predefined
  153. constants and only use those. This is more efficient and removes the risk of
  154. collecting personal information.</li>
  155. <li>Do not set tags inside loops or any other place which gets called
  156. frequently. This can cause a lot of data to be stored and uploaded.</li>
  157. </ul>
  158. <br>
  159. See the tagging guide at: http://wiki.localytics.com/
  160. @param event The name of the event which occurred.
  161. */
  162. - (void)tagEvent:(NSString *)event;
  163. - (void)tagEvent:(NSString *)event attributes:(NSDictionary *)attributes;
  164. - (void)tagEvent:(NSString *)event attributes:(NSDictionary *)attributes reportAttributes:(NSDictionary *)reportAttributes;
  165. /*!
  166. @method tagScreen
  167. @abstract Allows tagging the flow of screens encountered during the session.
  168. @param screen The name of the screen
  169. */
  170. - (void)tagScreen:(NSString *)screen;
  171. /*!
  172. @method upload
  173. @abstract Creates a low priority thread which uploads any Localytics data already stored
  174. on the device. This should be done early in the process life in order to
  175. guarantee as much time as possible for slow connections to complete. It is also reasonable
  176. to upload again when the application is exiting because if the upload is cancelled the data
  177. will just get uploaded the next time the app comes up.
  178. */
  179. - (void)upload;
  180. @end