GAI.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*!
  2. @header GAI.h
  3. @abstract Google Analytics iOS SDK Header
  4. @version 3.13
  5. @copyright Copyright 2015 Google Inc. All rights reserved.
  6. */
  7. #import <Foundation/Foundation.h>
  8. #import "GAILogger.h"
  9. #import "GAITrackedViewController.h"
  10. #import "GAITracker.h"
  11. typedef NS_ENUM(NSUInteger, GAIDispatchResult) {
  12. kGAIDispatchNoData,
  13. kGAIDispatchGood,
  14. kGAIDispatchError
  15. };
  16. /*! Google Analytics product string. */
  17. extern NSString *const kGAIProduct;
  18. /*! Google Analytics version string. */
  19. extern NSString *const kGAIVersion;
  20. /*!
  21. NSError objects returned by the Google Analytics SDK may have this error domain
  22. to indicate that the error originated in the Google Analytics SDK.
  23. */
  24. extern NSString *const kGAIErrorDomain;
  25. /*! Google Analytics error codes. */
  26. typedef enum {
  27. // This error code indicates that there was no error. Never used.
  28. kGAINoError = 0,
  29. // This error code indicates that there was a database-related error.
  30. kGAIDatabaseError,
  31. // This error code indicates that there was a network-related error.
  32. kGAINetworkError,
  33. } GAIErrorCode;
  34. /*!
  35. Google Analytics iOS top-level class. Provides facilities to create trackers
  36. and set behaviorial flags.
  37. */
  38. @interface GAI : NSObject
  39. /*!
  40. For convenience, this class exposes a default tracker instance.
  41. This is initialized to `nil` and will be set to the first tracker that is
  42. instantiated in trackerWithTrackingId:. It may be overridden as desired.
  43. The GAITrackedViewController class will, by default, use this tracker instance.
  44. */
  45. @property(nonatomic, assign) id<GAITracker> defaultTracker;
  46. /*!
  47. The GAILogger to use.
  48. */
  49. @property(nonatomic, retain) id<GAILogger> logger;
  50. /*!
  51. When this is true, no tracking information will be gathered; tracking calls
  52. will effectively become no-ops. When set to true, all tracking information that
  53. has not yet been submitted. The value of this flag will be persisted
  54. automatically by the SDK. Developers can optionally use this flag to implement
  55. an opt-out setting in the app to allows users to opt out of Google Analytics
  56. tracking.
  57. This is set to `NO` the first time the Google Analytics SDK is used on a
  58. device, and is persisted thereafter.
  59. */
  60. @property(nonatomic, assign) BOOL optOut;
  61. /*!
  62. If this value is positive, tracking information will be automatically
  63. dispatched every dispatchInterval seconds. Otherwise, tracking information must
  64. be sent manually by calling dispatch.
  65. By default, this is set to `120`, which indicates tracking information should
  66. be dispatched automatically every 120 seconds.
  67. */
  68. @property(nonatomic, assign) NSTimeInterval dispatchInterval;
  69. /*!
  70. When set to true, the SDK will record the currently registered uncaught
  71. exception handler, and then register an uncaught exception handler which tracks
  72. the exceptions that occurred using defaultTracker. If defaultTracker is not
  73. `nil`, this function will track the exception on the tracker and attempt to
  74. dispatch any outstanding tracking information for 5 seconds. It will then call
  75. the previously registered exception handler, if any. When set back to false,
  76. the previously registered uncaught exception handler will be restored.
  77. */
  78. @property(nonatomic, assign) BOOL trackUncaughtExceptions;
  79. /*!
  80. When this is 'YES', no tracking information will be sent. Defaults to 'NO'.
  81. */
  82. @property(nonatomic, assign) BOOL dryRun;
  83. /*! Get the shared instance of the Google Analytics for iOS class. */
  84. + (GAI *)sharedInstance;
  85. /*!
  86. Creates or retrieves a GAITracker implementation with the specified name and
  87. tracking ID. If the tracker for the specified name does not already exist, then
  88. it will be created and returned; otherwise, the existing tracker will be
  89. returned. If the existing tracker for the respective name has a different
  90. tracking ID, that tracking ID is not changed by this method. If defaultTracker
  91. is not set, it will be set to the tracker instance returned here.
  92. @param name The name of this tracker. Must not be `nil` or empty.
  93. @param trackingID The tracking ID to use for this tracker. It should be of
  94. the form `UA-xxxxx-y`.
  95. @return A GAITracker associated with the specified name. The tracker
  96. can be used to send tracking data to Google Analytics. The first time this
  97. method is called with a particular name, the tracker for that name will be
  98. returned, and subsequent calls with the same name will return the same
  99. instance. It is not necessary to retain the tracker because the tracker will be
  100. retained internally by the library.
  101. If an error occurs or the name is not valid, this method will return
  102. `nil`.
  103. */
  104. - (id<GAITracker>)trackerWithName:(NSString *)name
  105. trackingId:(NSString *)trackingId;
  106. /*!
  107. Creates or retrieves a GAITracker implementation with name equal to
  108. the specified tracking ID. If the tracker for the respective name does not
  109. already exist, it is created, has it's tracking ID set to |trackingId|,
  110. and is returned; otherwise, the existing tracker is returned. If the existing
  111. tracker for the respective name has a different tracking ID, that tracking ID
  112. is not changed by this method. If defaultTracker is not set, it is set to the
  113. tracker instance returned here.
  114. @param trackingID The tracking ID to use for this tracker. It should be of
  115. the form `UA-xxxxx-y`. The name of the tracker will be the same as trackingID.
  116. @return A GAITracker associated with the specified trackingID. The tracker
  117. can be used to send tracking data to Google Analytics. The first time this
  118. method is called with a particular trackingID, the tracker for the respective
  119. name will be returned, and subsequent calls with the same trackingID
  120. will return the same instance. It is not necessary to retain the tracker
  121. because the tracker will be retained internally by the library.
  122. If an error occurs or the trackingId is not valid, this method will return
  123. `nil`.
  124. */
  125. - (id<GAITracker>)trackerWithTrackingId:(NSString *)trackingId;
  126. /*!
  127. Remove a tracker from the trackers dictionary. If it is the default tracker,
  128. clears the default tracker as well.
  129. @param name The name of the tracker.
  130. */
  131. - (void)removeTrackerByName:(NSString *)name;
  132. /*!
  133. Dispatches any pending tracking information.
  134. Note that this does not have any effect on dispatchInterval, and can be used in
  135. conjunction with periodic dispatch. */
  136. - (void)dispatch;
  137. /*!
  138. Dispatches the next tracking beacon in the queue, calling completionHandler when
  139. the tracking beacon has either been sent (returning kGAIDispatchGood) or an error has resulted
  140. (returning kGAIDispatchError). If there is no network connection or there is no data to send,
  141. kGAIDispatchNoData is returned.
  142. Note that calling this method with a non-nil completionHandler disables periodic dispatch.
  143. Periodic dispatch can be reenabled by setting the dispatchInterval to a positive number when
  144. the app resumes from the background.
  145. Calling this method with a nil completionHandler is the same as calling the dispatch
  146. above.
  147. This method can be used for background data fetching in iOS 7.0 or later. It would be wise to
  148. call this when the application is exiting to initiate the submission of any unsubmitted
  149. tracking information.
  150. @param completionHandler The block to run after a single dispatch request. The GAIDispatchResult
  151. param indicates whether the dispatch succeeded, had an error, or had no hits to dispatch.
  152. */
  153. - (void)dispatchWithCompletionHandler:(void (^)(GAIDispatchResult result))completionHandler;
  154. @end