FBAppEvents.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2010-present Facebook.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "FBSDKMacros.h"
  18. #import "FBSession.h"
  19. /*!
  20. @typedef NS_ENUM (NSUInteger, FBAppEventsFlushBehavior)
  21. @abstract
  22. Control when <FBAppEvents> sends log events to the server
  23. @discussion
  24. */
  25. typedef NS_ENUM(NSUInteger, FBAppEventsFlushBehavior) {
  26. /*! Flush automatically: periodically (once a minute or every 100 logged events) and always at app reactivation. */
  27. FBAppEventsFlushBehaviorAuto = 0,
  28. /*! Only flush when the `flush` method is called. When an app is moved to background/terminated, the
  29. events are persisted and re-established at activation, but they will only be written with an
  30. explicit call to `flush`. */
  31. FBAppEventsFlushBehaviorExplicitOnly,
  32. };
  33. /*
  34. * Constant used by NSNotificationCenter for results of flushing AppEvents event logs
  35. */
  36. /*! NSNotificationCenter name indicating a result of a failed log flush attempt */
  37. FBSDK_EXTERN NSString *const FBAppEventsLoggingResultNotification;
  38. // Predefined event names for logging events common to many apps. Logging occurs through the `logEvent` family of methods on `FBAppEvents`.
  39. // Common event parameters are provided in the `FBAppEventsParameterNames*` constants.
  40. // General purpose
  41. /*! Deprecated: use [FBAppEvents activateApp] instead. */
  42. FBSDK_EXTERN NSString *const FBAppEventNameActivatedApp __attribute__ ((deprecated("use [FBAppEvents activateApp] instead")));
  43. /*! Log this event when a user has completed registration with the app. */
  44. FBSDK_EXTERN NSString *const FBAppEventNameCompletedRegistration;
  45. /*! Log this event when a user has viewed a form of content in the app. */
  46. FBSDK_EXTERN NSString *const FBAppEventNameViewedContent;
  47. /*! Log this event when a user has performed a search within the app. */
  48. FBSDK_EXTERN NSString *const FBAppEventNameSearched;
  49. /*! Log this event when the user has rated an item in the app. The valueToSum passed to logEvent should be the numeric rating. */
  50. FBSDK_EXTERN NSString *const FBAppEventNameRated;
  51. /*! Log this event when the user has completed a tutorial in the app. */
  52. FBSDK_EXTERN NSString *const FBAppEventNameCompletedTutorial;
  53. // Ecommerce related
  54. /*! Log this event when the user has added an item to their cart. The valueToSum passed to logEvent should be the item's price. */
  55. FBSDK_EXTERN NSString *const FBAppEventNameAddedToCart;
  56. /*! Log this event when the user has added an item to their wishlist. The valueToSum passed to logEvent should be the item's price. */
  57. FBSDK_EXTERN NSString *const FBAppEventNameAddedToWishlist;
  58. /*! Log this event when the user has entered the checkout process. The valueToSum passed to logEvent should be the total price in the cart. */
  59. FBSDK_EXTERN NSString *const FBAppEventNameInitiatedCheckout;
  60. /*! Log this event when the user has entered their payment info. */
  61. FBSDK_EXTERN NSString *const FBAppEventNameAddedPaymentInfo;
  62. /*! Deprecated: use [FBAppEvents logPurchase:currency:] or [FBAppEvents logPurchase:currency:parameters:] instead */
  63. FBSDK_EXTERN NSString *const FBAppEventNamePurchased __attribute__ ((deprecated("use [FBAppEvents logPurchase:currency:] or [FBAppEvents logPurchase:currency:parameters:] instead")));
  64. // Gaming related
  65. /*! Log this event when the user has achieved a level in the app. */
  66. FBSDK_EXTERN NSString *const FBAppEventNameAchievedLevel;
  67. /*! Log this event when the user has unlocked an achievement in the app. */
  68. FBSDK_EXTERN NSString *const FBAppEventNameUnlockedAchievement;
  69. /*! Log this event when the user has spent app credits. The valueToSum passed to logEvent should be the number of credits spent. */
  70. FBSDK_EXTERN NSString *const FBAppEventNameSpentCredits;
  71. // Predefined event name parameters for common additional information to accompany events logged through the `logEvent` family
  72. // of methods on `FBAppEvents`. Common event names are provided in the `FBAppEventName*` constants.
  73. /*! Parameter key used to specify currency used with logged event. E.g. "USD", "EUR", "GBP". See ISO-4217 for specific values. One reference for these is <http://en.wikipedia.org/wiki/ISO_4217>. */
  74. FBSDK_EXTERN NSString *const FBAppEventParameterNameCurrency;
  75. /*! Parameter key used to specify method user has used to register for the app, e.g., "Facebook", "email", "Twitter", etc */
  76. FBSDK_EXTERN NSString *const FBAppEventParameterNameRegistrationMethod;
  77. /*! Parameter key used to specify a generic content type/family for the logged event, e.g. "music", "photo", "video". Options to use will vary based upon what the app is all about. */
  78. FBSDK_EXTERN NSString *const FBAppEventParameterNameContentType;
  79. /*! Parameter key used to specify an ID for the specific piece of content being logged about. Could be an EAN, article identifier, etc., depending on the nature of the app. */
  80. FBSDK_EXTERN NSString *const FBAppEventParameterNameContentID;
  81. /*! Parameter key used to specify the string provided by the user for a search operation. */
  82. FBSDK_EXTERN NSString *const FBAppEventParameterNameSearchString;
  83. /*! Parameter key used to specify whether the activity being logged about was successful or not. `FBAppEventParameterValueYes` and `FBAppEventParameterValueNo` are good canonical values to use for this parameter. */
  84. FBSDK_EXTERN NSString *const FBAppEventParameterNameSuccess;
  85. /*! Parameter key used to specify the maximum rating available for the `FBAppEventNameRate` event. E.g., "5" or "10". */
  86. FBSDK_EXTERN NSString *const FBAppEventParameterNameMaxRatingValue;
  87. /*! Parameter key used to specify whether payment info is available for the `FBAppEventNameInitiatedCheckout` event. `FBAppEventParameterValueYes` and `FBAppEventParameterValueNo` are good canonical values to use for this parameter. */
  88. FBSDK_EXTERN NSString *const FBAppEventParameterNamePaymentInfoAvailable;
  89. /*! Parameter key used to specify how many items are being processed for an `FBAppEventNameInitiatedCheckout` or `FBAppEventNamePurchased` event. */
  90. FBSDK_EXTERN NSString *const FBAppEventParameterNameNumItems;
  91. /*! Parameter key used to specify the level achieved in a `FBAppEventNameAchieved` event. */
  92. FBSDK_EXTERN NSString *const FBAppEventParameterNameLevel;
  93. /*! Parameter key used to specify a description appropriate to the event being logged. E.g., the name of the achievement unlocked in the `FBAppEventNameAchievementUnlocked` event. */
  94. FBSDK_EXTERN NSString *const FBAppEventParameterNameDescription;
  95. // Predefined values to assign to event parameters that accompany events logged through the `logEvent` family
  96. // of methods on `FBAppEvents`. Common event parameters are provided in the `FBAppEventParameterName*` constants.
  97. /*! Yes-valued parameter value to be used with parameter keys that need a Yes/No value */
  98. FBSDK_EXTERN NSString *const FBAppEventParameterValueYes;
  99. /*! No-valued parameter value to be used with parameter keys that need a Yes/No value */
  100. FBSDK_EXTERN NSString *const FBAppEventParameterValueNo;
  101. /*!
  102. @class FBAppEvents
  103. @abstract
  104. Client-side event logging for specialized application analytics available through Facebook App Insights
  105. and for use with Facebook Ads conversion tracking and optimization.
  106. @discussion
  107. The `FBAppEvents` static class has a few related roles:
  108. + Logging predefined and application-defined events to Facebook App Insights with a
  109. numeric value to sum across a large number of events, and an optional set of key/value
  110. parameters that define "segments" for this event (e.g., 'purchaserStatus' : 'frequent', or
  111. 'gamerLevel' : 'intermediate')
  112. + Logging events to later be used for ads optimization around lifetime value.
  113. + Methods that control the way in which events are flushed out to the Facebook servers.
  114. Here are some important characteristics of the logging mechanism provided by `FBAppEvents`:
  115. + Events are not sent immediately when logged. They're cached and flushed out to the Facebook servers
  116. in a number of situations:
  117. - when an event count threshold is passed (currently 100 logged events).
  118. - when a time threshold is passed (currently 60 seconds).
  119. - when an app has gone to background and is then brought back to the foreground.
  120. + Events will be accumulated when the app is in a disconnected state, and sent when the connection is
  121. restored and one of the above 'flush' conditions are met.
  122. + The `FBAppEvents` class in thread-safe in that events may be logged from any of the app's threads.
  123. + The developer can set the `flushBehavior` on `FBAppEvents` to force the flushing of events to only
  124. occur on an explicit call to the `flush` method.
  125. + The developer can turn on console debug output for event logging and flushing to the server by using
  126. the `FBLoggingBehaviorAppEvents` value in `[FBSettings setLoggingBehavior:]`.
  127. Some things to note when logging events:
  128. + There is a limit on the number of unique event names an app can use, on the order of 300.
  129. + There is a limit to the number of unique parameter names in the provided parameters that can
  130. be used per event, on the order of 25. This is not just for an individual call, but for all
  131. invocations for that eventName.
  132. + Event names and parameter names (the keys in the NSDictionary) must be between 2 and 40 characters, and
  133. must consist of alphanumeric characters, _, -, or spaces.
  134. + The length of each parameter value can be no more than on the order of 100 characters.
  135. */
  136. @interface FBAppEvents : NSObject
  137. /*
  138. * Basic event logging
  139. */
  140. /*!
  141. @method
  142. @abstract
  143. Log an event with just an eventName.
  144. @param eventName The name of the event to record. Limitations on number of events and name length
  145. are given in the `FBAppEvents` documentation.
  146. */
  147. + (void)logEvent:(NSString *)eventName;
  148. /*!
  149. @method
  150. @abstract
  151. Log an event with an eventName and a numeric value to be aggregated with other events of this name.
  152. @param eventName The name of the event to record. Limitations on number of events and name length
  153. are given in the `FBAppEvents` documentation. Common event names are provided in `FBAppEventName*` constants.
  154. @param valueToSum Amount to be aggregated into all events of this eventName, and App Insights will report
  155. the cumulative and average value of this amount.
  156. */
  157. + (void)logEvent:(NSString *)eventName
  158. valueToSum:(double)valueToSum;
  159. /*!
  160. @method
  161. @abstract
  162. Log an event with an eventName and a set of key/value pairs in the parameters dictionary.
  163. Parameter limitations are described above.
  164. @param eventName The name of the event to record. Limitations on number of events and name construction
  165. are given in the `FBAppEvents` documentation. Common event names are provided in `FBAppEventName*` constants.
  166. @param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
  167. be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
  168. parameters and name construction are given in the `FBAppEvents` documentation. Commonly used parameter names
  169. are provided in `FBAppEventParameterName*` constants.
  170. */
  171. + (void)logEvent:(NSString *)eventName
  172. parameters:(NSDictionary *)parameters;
  173. /*!
  174. @method
  175. @abstract
  176. Log an event with an eventName, a numeric value to be aggregated with other events of this name,
  177. and a set of key/value pairs in the parameters dictionary.
  178. @param eventName The name of the event to record. Limitations on number of events and name construction
  179. are given in the `FBAppEvents` documentation. Common event names are provided in `FBAppEventName*` constants.
  180. @param valueToSum Amount to be aggregated into all events of this eventName, and App Insights will report
  181. the cumulative and average value of this amount.
  182. @param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
  183. be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
  184. parameters and name construction are given in the `FBAppEvents` documentation. Commonly used parameter names
  185. are provided in `FBAppEventParameterName*` constants.
  186. */
  187. + (void)logEvent:(NSString *)eventName
  188. valueToSum:(double)valueToSum
  189. parameters:(NSDictionary *)parameters;
  190. /*!
  191. @method
  192. @abstract
  193. Log an event with an eventName, a numeric value to be aggregated with other events of this name,
  194. and a set of key/value pairs in the parameters dictionary. Providing session lets the developer
  195. target a particular <FBSession>. If nil is provided, then `[FBSession activeSession]` will be used.
  196. @param eventName The name of the event to record. Limitations on number of events and name construction
  197. are given in the `FBAppEvents` documentation. Common event names are provided in `FBAppEventName*` constants.
  198. @param valueToSum Amount to be aggregated into all events of this eventName, and App Insights will report
  199. the cumulative and average value of this amount. Note that this is an NSNumber, and a value of `nil` denotes
  200. that this event doesn't have a value associated with it for summation.
  201. @param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
  202. be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
  203. parameters and name construction are given in the `FBAppEvents` documentation. Commonly used parameter names
  204. are provided in `FBAppEventParameterName*` constants.
  205. @param session <FBSession> to direct the event logging to, and thus be logged with whatever user (if any)
  206. is associated with that <FBSession>.
  207. */
  208. + (void)logEvent:(NSString *)eventName
  209. valueToSum:(NSNumber *)valueToSum
  210. parameters:(NSDictionary *)parameters
  211. session:(FBSession *)session;
  212. /*
  213. * Purchase logging
  214. */
  215. /*!
  216. @method
  217. @abstract
  218. Log a purchase of the specified amount, in the specified currency.
  219. @param purchaseAmount Purchase amount to be logged, as expressed in the specified currency. This value
  220. will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
  221. @param currency Currency, is denoted as, e.g. "USD", "EUR", "GBP". See ISO-4217 for
  222. specific values. One reference for these is <http://en.wikipedia.org/wiki/ISO_4217>.
  223. @discussion This event immediately triggers a flush of the `FBAppEvents` event queue, unless the `flushBehavior` is set
  224. to `FBAppEventsFlushBehaviorExplicitOnly`.
  225. */
  226. + (void)logPurchase:(double)purchaseAmount
  227. currency:(NSString *)currency;
  228. /*!
  229. @method
  230. @abstract
  231. Log a purchase of the specified amount, in the specified currency, also providing a set of
  232. additional characteristics describing the purchase.
  233. @param purchaseAmount Purchase amount to be logged, as expressed in the specified currency.This value
  234. will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
  235. @param currency Currency, is denoted as, e.g. "USD", "EUR", "GBP". See ISO-4217 for
  236. specific values. One reference for these is <http://en.wikipedia.org/wiki/ISO_4217>.
  237. @param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
  238. be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
  239. parameters and name construction are given in the `FBAppEvents` documentation. Commonly used parameter names
  240. are provided in `FBAppEventParameterName*` constants.
  241. @discussion This event immediately triggers a flush of the `FBAppEvents` event queue, unless the `flushBehavior` is set
  242. to `FBAppEventsFlushBehaviorExplicitOnly`.
  243. */
  244. + (void)logPurchase:(double)purchaseAmount
  245. currency:(NSString *)currency
  246. parameters:(NSDictionary *)parameters;
  247. /*!
  248. @method
  249. @abstract
  250. Log a purchase of the specified amount, in the specified currency, also providing a set of
  251. additional characteristics describing the purchase, as well as an <FBSession> to log to.
  252. @param purchaseAmount Purchase amount to be logged, as expressed in the specified currency.This value
  253. will be rounded to the thousandths place (e.g., 12.34567 becomes 12.346).
  254. @param currency Currency, is denoted as, e.g. "USD", "EUR", "GBP". See ISO-4217 for
  255. specific values. One reference for these is <http://en.wikipedia.org/wiki/ISO_4217>.
  256. @param parameters Arbitrary parameter dictionary of characteristics. The keys to this dictionary must
  257. be NSString's, and the values are expected to be NSString or NSNumber. Limitations on the number of
  258. parameters and name construction are given in the `FBAppEvents` documentation. Commonly used parameter names
  259. are provided in `FBAppEventParameterName*` constants.
  260. @param session <FBSession> to direct the event logging to, and thus be logged with whatever user (if any)
  261. is associated with that <FBSession>. A value of `nil` will use `[FBSession activeSession]`.
  262. @discussion This event immediately triggers a flush of the `FBAppEvents` event queue, unless the `flushBehavior` is set
  263. to `FBAppEventsFlushBehaviorExplicitOnly`.
  264. */
  265. + (void)logPurchase:(double)purchaseAmount
  266. currency:(NSString *)currency
  267. parameters:(NSDictionary *)parameters
  268. session:(FBSession *)session;
  269. /*!
  270. @method
  271. @abstract This method has been replaced by [FBSettings limitEventAndDataUsage] */
  272. + (BOOL)limitEventUsage __attribute__ ((deprecated("use [FBSettings limitEventAndDataUsage] instead")));
  273. /*!
  274. @method
  275. @abstract This method has been replaced by [FBSettings setLimitEventUsage] */
  276. + (void)setLimitEventUsage:(BOOL)limitEventUsage __attribute__ ((deprecated("use [FBSettings setLimitEventAndDataUsage] instead")));
  277. /*!
  278. @method
  279. @abstract
  280. Notifies the events system that the app has launched and, when appropriate, logs an "activated app" event. Should typically be placed in the
  281. app delegates' `applicationDidBecomeActive:` method.
  282. This method also takes care of logging the event indicating the first time this app has been launched, which, among other things, is used to
  283. track user acquisition and app install ads conversions.
  284. @discussion
  285. `activateApp` will not log an event on every app launch, since launches happen every time the app is backgrounded and then foregrounded.
  286. "activated app" events will be logged when the app has not been active for more than 60 seconds. This method also causes a "deactivated app"
  287. event to be logged when sessions are "completed", and these events are logged with the session length, with an indication of how much
  288. time has elapsed between sessions, and with the number of background/foreground interruptions that session had. This data
  289. is all visible in your app's App Events Insights.
  290. */
  291. + (void)activateApp;
  292. /*
  293. * Control over event batching/flushing
  294. */
  295. /*!
  296. @method
  297. @abstract
  298. Get the current event flushing behavior specifying when events are sent back to Facebook servers.
  299. */
  300. + (FBAppEventsFlushBehavior)flushBehavior;
  301. /*!
  302. @method
  303. @abstract
  304. Set the current event flushing behavior specifying when events are sent back to Facebook servers.
  305. @param flushBehavior The desired `FBAppEventsFlushBehavior` to be used.
  306. */
  307. + (void)setFlushBehavior:(FBAppEventsFlushBehavior)flushBehavior;
  308. /*!
  309. @method
  310. @abstract
  311. Set the 'override' App ID for App Event logging.
  312. @discussion
  313. In some cases, apps want to use one Facebook App ID for login and social presence and another
  314. for App Event logging. (An example is if multiple apps from the same company share an app ID for login, but
  315. want distinct logging.) By default, this value is `nil`, and defers to the `FacebookLoggingOverrideAppID`
  316. plist value. If that's not set, the default App ID set via [FBSettings setDefaultAppID]
  317. or in the `FacebookAppID` plist entry.
  318. This should be set before any other calls are made to `FBAppEvents`. Thus, you should set it in your application
  319. delegate's `application:didFinishLaunchingWithOptions:` delegate.
  320. @param appID The Facebook App ID to be used for App Event logging.
  321. */
  322. + (void)setLoggingOverrideAppID:(NSString *)appID;
  323. /*!
  324. @method
  325. @abstract
  326. Get the 'override' App ID for App Event logging.
  327. @discussion
  328. @see `setLoggingOverrideAppID:`
  329. */
  330. + (NSString *)loggingOverrideAppID;
  331. /*!
  332. @method
  333. @abstract
  334. Explicitly kick off flushing of events to Facebook. This is an asynchronous method, but it does initiate an immediate
  335. kick off. Server failures will be reported through the NotificationCenter with notification ID `FBAppEventsLoggingResultNotification`.
  336. */
  337. + (void)flush;
  338. @end