PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://gitlab.com/vectorci/td-ios-sdk
Markdown | 368 lines | 269 code | 99 blank | 0 comment | 0 complexity | 74c080df1ab25e5e54acd298fd4aa376 MD5 | raw file
  1. TreasureData iOS SDK
  2. ===============
  3. iOS SDK for [TreasureData](http://www.treasuredata.com/). With this SDK, you can import the events on your applications into TreasureData easily. This library supports iOS 5 or later.
  4. Also, there is a more modern alternative SDK written in Swift [https://github.com/recruit-lifestyle/TreasureDataSDK](https://github.com/recruit-lifestyle/TreasureDataSDK).
  5. ## Installation
  6. There are several ways to install the library.
  7. ### CocoaPods
  8. [CocoaPods](http://cocoapods.org/) is needed to set up the SDK. If you've not installed it yet, install it at first.
  9. ```
  10. $ gem install cocoapods
  11. ```
  12. Next, add this line in your Podfile.
  13. ```
  14. pod 'TreasureData-iOS-SDK', '= 0.1.19'
  15. ```
  16. If you use the SDK in Swift, add this line to your Podfile.
  17. ```
  18. use_frameworks!
  19. ```
  20. Finally, execute 'pod install'.
  21. ```
  22. $ pod install
  23. ```
  24. ### Framework
  25. Download [TreasureData.framework](http://cdn.treasuredata.com/sdk/ios/0.1.19/TreasureData-iOS-SDK.framework.zip) and add it and `libz` library into your project.
  26. ## Usage in Objective-C
  27. ### Import SDK header file
  28. ```
  29. #import <TreasureData-iOS-SDK/TreasureData.h>
  30. ```
  31. ### Register your TreasureData API key
  32. ```
  33. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  34. [TreasureData initializeWithApiKey:@"your_api_key"];
  35. }
  36. ```
  37. We recommend to use a write-only API key for the SDK. To obtain one, please:
  38. 1. Login into the Treasure Data Console at http://console.treasuredata.com;
  39. 2. Visit your Profile page at http://console.treasuredata.com/users/current;
  40. 3. Insert your password under the 'API Keys' panel;
  41. 4. In the bottom part of the panel, under 'Write-Only API keys', either copy the API key or click on 'Generate New' and copy the new API key.
  42. ### Add events to local buffer
  43. ```
  44. - (IBAction)clickButton:(id)sender {
  45. [[TreasureData sharedInstance] addEventWithCallback:@{
  46. @"name": @"boo bar",
  47. @"age": @42,
  48. @"comment": @"hello world"
  49. }
  50. database:@"testdb"
  51. table:@"demotbl"
  52. onSuccess:^(){
  53. NSLog(@"addEvent: success");
  54. }
  55. onError:^(NSString* errorCode, NSString* message) {
  56. NSLog(@"addEvent: error. errorCode=%@, message=%@", errorCode, message);
  57. }];
  58. }
  59. ```
  60. Or, simply call `addEvent` method instead of `addEventWithCallback`.
  61. ```
  62. [[TreasureData sharedInstance] addEvent:@{
  63. @"name": @"boo bar",
  64. @"age": @42,
  65. @"comment": @"hello world"
  66. }
  67. database:@"testdb"
  68. table:@"demotbl"];
  69. ```
  70. Specify the database and table to which you want to import the events.
  71. ### Upload buffered events to TreasureData
  72. ```
  73. - (void)applicationDidEnterBackground:(UIApplication *)application {
  74. __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
  75. [application endBackgroundTask:bgTask];
  76. bgTask = UIBackgroundTaskInvalid;
  77. }];
  78. [[TreasureData sharedInstance] uploadEventsWithCallback:^() {
  79. [application endBackgroundTask:bgTask];
  80. bgTask = UIBackgroundTaskInvalid;
  81. }
  82. onError:^(NSString *code, NSString *msg) {
  83. [application endBackgroundTask:bgTask];
  84. bgTask = UIBackgroundTaskInvalid;
  85. }
  86. ];
  87. }
  88. ```
  89. Or, simply call `uploadEvents` method instead of `uploadEventsWithCallback`.
  90. ```
  91. [[TreasureData sharedInstance] uploadEvents];
  92. ```
  93. The sent events are going to be buffered for a few minutes before they get sent and imported into TreasureData storage.
  94. ### Start/End session
  95. When you call `startSession` method, the SDK generates a session ID that's kept until `endSession` is called. The session id is outputs as a column name "td_session_id". Also, `startSession` and `endSession` methods add an event that includes `{"td_session_event":"start" or "end"}`.
  96. ```
  97. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  98. {
  99. [TreasureData initializeWithApiKey:@"your_api_key"];
  100. [[TreasureData sharedInstance] setDefaultDatabase:@"testdb"];
  101. [[TreasureData sharedInstance] startSession:@"demotbl"];
  102. }
  103. - (void)applicationDidEnterBackground:(UIApplication *)application
  104. {
  105. [[TreasureData sharedInstance] endSession:@"demotbl"];
  106. __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
  107. [application endBackgroundTask:bgTask];
  108. bgTask = UIBackgroundTaskInvalid;
  109. }];
  110. [[TreasureData sharedInstance] uploadEventsWithCallback:^() {
  111. [application endBackgroundTask:bgTask];
  112. bgTask = UIBackgroundTaskInvalid;
  113. }
  114. onError:^(NSString *code, NSString *msg) {
  115. [application endBackgroundTask:bgTask];
  116. bgTask = UIBackgroundTaskInvalid;
  117. }
  118. // Outputs =>>
  119. // [{"td_session_id":"cad88260-67b4-0242-1329-2650772a66b1",
  120. // "td_session_event":"start", "time":1418880000},
  121. //
  122. // {"td_session_id":"cad88260-67b4-0242-1329-2650772a66b1",
  123. // "td_session_event":"end", "time":1418880123}
  124. // ]
  125. ];
  126. ```
  127. If you want to handle the following case, use a pair of class methods `startSession` and `endSession` for global session tracking
  128. - User opens the application and starts session tracking using `startSession`. Let's call this session session#0
  129. - User moves to home screen and finishes the session using `endSession`
  130. - User reopens the application and restarts session tracking within default 10 seconds. But you want to deal with this new session as the same session as session#0
  131. ```
  132. - (void)applicationDidBecomeActive:(UIApplication *)application
  133. {
  134. [TreasureData startSession];
  135. }
  136. - (void)applicationDidEnterBackground:(UIApplication *)application
  137. {
  138. [TreasureData endSession];
  139. }
  140. ```
  141. ### Detect if it's the first running
  142. You can detect if it's the first running or not easily using `isFirstRun` method and then clear the flag with `clearFirstRun`.
  143. ```
  144. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  145. :
  146. if ([[TreasureData sharedInstance] isFirstRun]) {
  147. [[TreasureData sharedInstance] addEventWithCallback:@{ @"event": @"installed" }
  148. database:@"testdb"
  149. table:@"demotbl"
  150. onSuccess:^(){
  151. [[TreasureData sharedInstance] uploadEventsWithCallback:^() {
  152. [[TreasureData sharedInstance] clearFirstRun];
  153. }
  154. onError:^(NSString* errorCode, NSString* message) {
  155. NSLog(@"uploadEvents: error. errorCode=%@, message=%@", errorCode, message);
  156. }
  157. ];
  158. }
  159. onError:^(NSString* errorCode, NSString* message) {
  160. NSLog(@"addEvent: error. errorCode=%@, message=%@", errorCode, message);
  161. }];
  162. }
  163. ```
  164. ## About Error code
  165. `addEventWithCallback` and `uploadEventsWithCallback` methods call back `onError` block with `errorCode` argument. This argument is useful to know the cause type of the error. There are the following error codes.
  166. - `init_error` : The initialization failed.
  167. - `invalid_param` : The parameter passed to the API was invalid
  168. - `invalid_event` : The event was invalid
  169. - `data_conversion` : Failed to convert the data to/from JSON
  170. - `storage_error` : Failed to read/write data in the storage
  171. - `network_error` : Failed to communicate with the server due to network problem
  172. - `server_response` : The server returned an error response
  173. ## Additional Configuration
  174. ### Endpoint
  175. The API endpoint (default: https://in.treasuredata.com) can be modified using `initializeApiEndpoint` class method. For example,
  176. ```
  177. [TreasureData initializeApiEndpoint:@"https://in.treasuredata.com"];
  178. [TreasureData initializeWithApiKey:@"your_api_key"];
  179. ```
  180. ### Encryption key
  181. If you've set an encryption key via `initializeEncryptionKey` class method, our SDK saves the events data as encrypted when called `addEvent` or `addEventWithCallback` methods.
  182. ```
  183. [TreasureData initializeEncryptionKey:@"hello world"];
  184. :
  185. [[TreasureData sharedInstance] addEventWithCallback: ....];
  186. ```
  187. ### Default database
  188. ```
  189. [[TreasureData sharedInstance] setDefaultDatabase:@"testdb"];
  190. :
  191. [[TreasureData sharedInstance] addEventWithCallback:@{ @"event": @"clicked" } table:@"demotbl"]
  192. ```
  193. ### Adding UUID of the device to each event automatically
  194. UUID of the device will be added to each event automatically if you call `enableAutoAppendUniqId`. This value won't change until the application is uninstalled.
  195. ```
  196. [[TreasureData sharedInstance] enableAutoAppendUniqId];
  197. :
  198. [[TreasureData sharedInstance] addEventWithCallback:@{ @"event": @"dragged" }
  199. database:@"testdb" table:@"demotbl"];
  200. ```
  201. It outputs the value as a column name `td_uuid`.
  202. ### Adding an UUID to each event record automatically
  203. UUID will be added to each event record automatically if you call `enableAutoAppendRecordUUID`. Each event has different UUID.
  204. ```
  205. [[TreasureData sharedInstance] enableAutoAppendRecordUUID];
  206. // If you want to customize the column name, pass it to the API
  207. // [[TreasureData sharedInstance] enableAutoAppendRecordUUID:@"my_record_uuid"];
  208. :
  209. [[TreasureData sharedInstance] addEventWithCallback:@{ @"event": @"dragged" }
  210. database:@"testdb" table:@"demotbl"];
  211. ```
  212. It outputs the value as a column name `record_uuid` by default.
  213. ### Adding the device model information to each event automatically
  214. Device model infromation will be added to each event automatically if you call `enableAutoAppendModelInformation`.
  215. ```
  216. [[TreasureData sharedInstance] enableAutoAppendModelInformation];
  217. :
  218. [[TreasureData sharedInstance] addEventWithCallback:@{ @"event": @"dragged" }
  219. database:@"testdb" table:@"demotbl"];
  220. ```
  221. It outputs the following column names and values:
  222. - `td_device` : UIDevice.model
  223. - `td_model` : UIDevice.model
  224. - `td_os_ver` : UIDevice.model.systemVersion
  225. - `td_os_type` : "iOS"
  226. ### Adding application version information to each event automatically
  227. Application version infromation will be added to each event automatically if you call `enableAutoAppendAppInformation`.
  228. ```
  229. [[TreasureData sharedInstance] enableAutoAppendAppInformation];
  230. ```
  231. It outputs the following column names and values:
  232. - `td_app_ver` : Core Foundation key `CFBundleShortVersionString`
  233. - `td_app_ver_num` : Core Foundation key `CFBundleVersion`
  234. ### Adding locale configuration information to each event automatically
  235. Locale configuration infromation will be added to each event automatically if you call `enableAutoAppendLocaleInformation`.
  236. ```
  237. [[TreasureData sharedInstance] enableAutoAppendLocaleInformation];
  238. ```
  239. It outputs the following column names and values:
  240. - `td_locale_country` : `[[NSLocale currentLocale] objectForKey: NSLocaleCountryCode]`
  241. - `td_locale_lang` : `[[NSLocale currentLocale] objectForKey: NSLocaleLanguageCode]`
  242. ### Use server side upload timestamp
  243. If you want to use server side upload timestamp not only client device time that is recorded when your application calls `addEvent`, use `enableServerSideUploadTimestamp`.
  244. ```
  245. // Use server side upload time as `time` column
  246. [[TreasureData sharedInstance] enableServerSideUploadTimestamp];
  247. // Add server side upload time as a customized column name
  248. [[TreasureData sharedInstance] enableServerSideUploadTimestamp:@"server_upload_time"];
  249. ```
  250. ### Enable/Disable debug log
  251. ```
  252. [TreasureData enableLogging];
  253. ```
  254. ```
  255. [TreasureData disableLogging];
  256. ```
  257. ## Troubleshooting
  258. #### With "Data Protection" enabled, TD iOS SDK occasionally crashes
  259. - If your app calls the SDK's API such as `TreasureData#endSession` in `UIApplicationDelegate applicationDidEnterBackground`, check if it's likely the app calls the SDK's API several seconds after iOS is locked. If so, please make other tasks that takes time and is called prior to the SDK's API run in background.
  260. ```
  261. - (void)applicationWillResignActive:(UIApplication *)application
  262. {
  263. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  264. // Some tasks that can take more than 10 seconds.
  265. });
  266. }
  267. ```
  268. ## Usage in Swift
  269. See this example project (https://github.com/treasure-data/td-ios-sdk/tree/master/TreasureDataExampleSwift) for details.