-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeenClient.h
223 lines (168 loc) · 10 KB
/
KeenClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// KeenClient.h
// KeenClient
//
// Created by Daniel Kador on 2/8/12.
// Copyright (c) 2012 Keen Labs. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "KeenProperties.h"
// defines the KCLog macro
#define KEEN_DEBUG
#ifdef KEEN_DEBUG
#define KCLog(...) NSLog(__VA_ARGS__)
#else
#define KCLog(...)
#endif
// defines a type for the block we'll use with our global properties
typedef NSDictionary* (^KeenGlobalPropertiesBlock)(NSString *eventCollection);
/**
KeenClient has class methods to return managed instances of itself and instance methods
to collect new events and upload them through the keen API.
IMPORTANT NOTE: Please remember to add the value "-ObjC" to the "Other Linker Flags" section of your build target.
Without doing this, your code will compile but will fail at runtime.
Example usage:
[KeenClient sharedClientWithProjectId:@"my_id" andApiKey:@"my_token"];
NSDictionary *myEvent = [NSDictionary dictionary];
[[KeenClient sharedClient] addEvent:myEvent toEventCollection:@"purchases"];
[[KeenClient sharedClient] uploadWithFinishedBlock:nil];
*/
@interface KeenClient : NSObject <CLLocationManagerDelegate>
/**
This Objective-C property represents the Keen Global Properties dictionary for this instance of the
KeenClient. The dictionary is used every time an event is added to an event collection.
Keen Global Properties are properties which are sent with EVERY event. For example, you may wish to always
capture static information like user ID, app version, etc.
Every time an event is added to an event collection, the SDK will check to see if this property is defined.
If it is, the SDK will copy all the properties from the global properties into the newly added event.
Note that because this is just a dictionary, it's much more difficult to create DYNAMIC global properties.
It also doesn't support per-collection properties. If either of these use cases are important to you, please use
the Objective-C property globalPropertiesBlock.
Also note that the Keen properties defined in the globalPropertiesBlock take precendence over the properties
defined in the globalPropertiesDictionary, and that the Keen Properties defined in each individual event take
precedence over either of the Global Properties.
Example usage:
KeenClient *client = [KeenClient sharedClient];
client.globalPropertiesDictionary = @{@"some_standard_key": @"some_standard_value"};
*/
@property (nonatomic, retain) NSDictionary *globalPropertiesDictionary;
/**
This Objective-C property represents the Keen Global Properties block for this instance of the KeenClient.
The block is invoked every time an event is added to an event collection.
Keen Global Properties are properties which are sent with EVERY event. For example, you may wish to always
capture device information like OS version, handset type, orientation, etc.
The block is invoked every time an event is added to an event collection. It takes as a parameter a single
NSString, which is the name of the event collection the event's being added to. The user is responsible
for returning an NSDictionary which represents the global properties for this particular event collection.
Note that because we use a block, you can create DYNAMIC global properties. For example, if you want to
capture device orientation, then your block can ask the device for its current orientation and then construct
the NSDictionary. If your global properties aren't dynamic, then just return the same NSDictionary every time.
Also note that the Keen properties defined in the globalPropertiesBlock take precendence over the properties
defined in the globalPropertiesDictionary, and that the Keen Properties defined in each individual event take
precedence over either of the Global Properties.
Example usage:
KeenClient *client = [KeenClient sharedClient];
client.globalPropertiesBlock = ^NSDictionary *(NSString *eventCollection) {
if ([eventCollection isEqualToString:@"apples"]) {
return @{ @"color": @"red" };
} else if ([eventCollection isEqualToString:@"pears"]) {
return @{ @"color": @"green" };
} else {
return nil;
}
};
*/
@property (nonatomic, copy) KeenGlobalPropertiesBlock globalPropertiesBlock;
/**
A property that holds the current location of the device. You can either call
[KeenClient refreshCurrentLocation] to pull location from the device or you can set this property with
your own value.
*/
@property (nonatomic, retain) CLLocation *currentLocation;
/**
Call this to retrieve the managed instance of KeenClient and set its project ID and API key
to the given parameters.
You'll generally want to call this the first time you ask for the shared client. Once you've called
this, you can simply call [KeenClient sharedClient] afterwards.
@param projectId The ID of your project.
@param apiKey The authorization token for your project.
@return A managed instance of KeenClient, or nil if projectId or apiKey are invalid.
*/
+ (KeenClient *)sharedClientWithProjectId:(NSString *)projectId andApiKey:(NSString *)apiKey;
/**
Call this to retrieve the managed instance of KeenClient.
If you only have to use a single Keen project, just use this.
@return A managed instance of KeenClient, or nil if you haven't called [KeenClient sharedClientWithProjectId:andApiKey:].
*/
+ (KeenClient *)sharedClient;
/**
Call this to disable geo location. If you don't want to pop up a message to users asking them to approve geo location
services, call this BEFORE doing anything else with KeenClient.
Geo location is ENABLED by default.
*/
+ (void)disableGeoLocation;
/**
Call this to enable geo location. You'll probably only have to call this if for some reason you've explicitly
disabled geo location.
Geo location is ENABLED by default.
*/
+ (void)enableGeoLocation;
/**
Call this if your code needs to use more than one Keen project and API key. By convention, if you
call this, you're responsible for releasing the returned instance once you're finished with it.
Otherwise, just use [KeenClient sharedClient].
@param projectId The ID of your project.
@param apiKey The authorization token for your project.
@return An initialized instance of KeenClient.
*/
- (id)initWithProjectId:(NSString *)projectId andApiKey:(NSString *)apiKey;
/**
Call this to set the global properties block for this instance of the KeenClient. The block is invoked
every time an event is added to an event collection.
Global properties are properties which are sent with EVERY event. For example, you may wish to always
capture device information like OS version, handset type, orientation, etc.
The block is invoked every time an event is added to an event collection. It takes as a parameter a single
NSString, which is the name of the event collection the event's being added to. The user is responsible
for returning an NSDictionary which represents the global properties for this particular event collection.
Note that because we use a block, you can create DYNAMIC global properties. For example, if you want to
capture device orientation, then your block can ask the device for its current orientation and then construct
the NSDictionary. If your global properties aren't dynamic, then just return the same NSDictionary every time.
@param block The block which is invoked any time an event is added to an event collection.
*/
- (void)setGlobalPropertiesBlock:(NSDictionary * (^)(NSString *eventCollection))block;
/**
Call this any time you want to add an event that will eventually be sent to the keen.io server.
The event will be stored on the local file system until you decide to upload (usually this will happen
in your application delegate right before your app goes into the background, but it could be any time).
@param event An NSDictionary that consists of key/value pairs. Keen naming conventions apply. Nested NSDictionaries or NSArrays are acceptable.
@param eventCollection The name of the collection you want to put this event into.
@param anError If the event was added, anError will be nil, otherwise it will contain information about why it wasn't added.
*/
- (void)addEvent:(NSDictionary *)event toEventCollection:(NSString *)eventCollection error:(NSError **)anError;
/**
Call this any time you want to add an event that will eventually be sent to the keen.io server AND you
want to override keen-default properties (like timestamp).
The event will be stored on the local file system until you decide to upload (usually this will happen
in your application delegate right before your app goes into the background, but it could be any time).
@param event An NSDictionary that consists of key/value pairs. Keen naming conventions apply. Nested NSDictionaries or NSArrays are acceptable.
@param keenProperties An NSDictionary that consists of key/value pairs to override defaulte properties. ex: "timestamp" -> NSDate
@param eventCollection The name of the event collection you want to put this event into.
@param anError If the event was added, anError will be nil, otherwise it will contain information about why it wasn't added.
*/
- (void)addEvent:(NSDictionary *)event withKeenProperties:(KeenProperties *)keenProperties toEventCollection:(NSString *)eventCollection error:(NSError **)anError;
/**
Call this whenever you want to upload all the events captured so far. This will spawn a low
priority background thread and process all required HTTP requests.
If an upload fails, the events will be saved for a later attempt.
If a particular event is invalid, the event will be dropped from the queue and the failure message
will be logged.
@param block The block to be executed once uploading is finished, regardless of whether or not the upload succeeded.
*/
- (void)uploadWithFinishedBlock:(void (^)())block;
/**
Refresh the current geo location. The Keen Client only gets geo at the beginning of each session (i.e. when the client is created).
If you want to update geo to the current location, call this method.
*/
- (void)refreshCurrentLocation;
@end