-
Notifications
You must be signed in to change notification settings - Fork 93
/
SPEvent.m
730 lines (579 loc) · 19.5 KB
/
SPEvent.m
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//
// SPEvent.m
// Snowplow
//
// Copyright (c) 2015 Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License
// Version 2.0. You may obtain a copy of the Apache License Version 2.0 at
// http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the Apache License Version 2.0 for the specific
// language governing permissions and limitations there under.
//
// Authors: Joshua Beemster
// Copyright: Copyright (c) 2015 Snowplow Analytics Ltd
// License: Apache License Version 2.0
//
#import "Snowplow.h"
#import "SPEvent.h"
#import "SPUtilities.h"
#import "SPPayload.h"
#import "SPSelfDescribingJson.h"
// Base Event
@implementation SPEvent
- (id) init {
self = [super init];
if (self) {
_timestamp = [SPUtilities getTimestamp];
_contexts = [[NSMutableArray alloc] init];
_eventId = [SPUtilities getEventId];
}
return self;
}
// --- Builder Methods
- (void) setTimestamp:(NSNumber *)timestamp {
_timestamp = timestamp;
}
- (void) setContexts:(NSMutableArray *)contexts {
for (NSObject * sdj in contexts) {
[SPUtilities checkArgument:([sdj isKindOfClass:[SPSelfDescribingJson class]])
withMessage:@"All contexts must be SelfDescribingJson objects."];
}
_contexts = contexts;
}
- (void) setEventId:(NSString *)eventId {
_eventId = eventId;
}
// --- Public Methods
- (NSMutableArray *) getContexts {
return [NSMutableArray arrayWithArray:_contexts];
}
- (NSNumber *) getTimestamp {
return _timestamp;
}
- (NSString *) getEventId {
return _eventId;
}
- (SPPayload *) addDefaultParamsToPayload:(SPPayload *)pb {
[pb addValueToPayload:[NSString stringWithFormat:@"%lld", _timestamp.longLongValue] forKey:kSPTimestamp];
[pb addValueToPayload:_eventId forKey:kSPEid];
return pb;
}
- (void) basePreconditions {
[SPUtilities checkArgument:(_contexts != nil) withMessage:@"Contexts cannot be nil."];
[SPUtilities checkArgument:([_eventId length] != 0) withMessage:@"EventID cannot be nil or empty."];
}
@end
// PageView Event
@implementation SPPageView {
NSString * _pageUrl;
NSString * _pageTitle;
NSString * _referrer;
}
+ (instancetype) build:(void(^)(id<SPPageViewBuilder>builder))buildBlock {
SPPageView* event = [SPPageView new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:([_pageUrl length] != 0) withMessage:@"PageURL cannot be nil or empty."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setPageUrl:(NSString *)pageUrl {
_pageUrl = pageUrl;
}
- (void) setPageTitle:(NSString *)pageTitle {
_pageTitle = pageTitle;
}
- (void) setReferrer:(NSString *)referrer {
_referrer = referrer;
}
// --- Public Methods
- (SPPayload *) getPayload {
SPPayload *pb = [[SPPayload alloc] init];
[pb addValueToPayload:kSPEventPageView forKey:kSPEvent];
[pb addValueToPayload:_pageUrl forKey:kSPPageUrl];
[pb addValueToPayload:_pageTitle forKey:kSPPageTitle];
[pb addValueToPayload:_referrer forKey:kSPPageRefr];
return [self addDefaultParamsToPayload:pb];
}
@end
// Structured Event
@implementation SPStructured {
NSString * _category;
NSString * _action;
NSString * _label;
NSString * _property;
NSNumber * _value;
}
+ (instancetype) build:(void(^)(id<SPStructuredBuilder>builder))buildBlock {
SPStructured* event = [SPStructured new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:([_category length] != 0) withMessage:@"Category cannot be nil or empty."];
[SPUtilities checkArgument:([_action length] != 0) withMessage:@"Action cannot be nil or empty."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setCategory:(NSString *)category {
_category = category;
}
- (void) setAction:(NSString *)action {
_action = action;
}
- (void) setLabel:(NSString *)label {
_label = label;
}
- (void) setProperty:(NSString *)property {
_property = property;
}
- (void) setValue:(double)value {
_value = [NSNumber numberWithDouble:value];
}
// --- Public Methods
- (SPPayload *) getPayload {
SPPayload *pb = [[SPPayload alloc] init];
[pb addValueToPayload:kSPEventStructured forKey:kSPEvent];
[pb addValueToPayload:_category forKey:kSPStuctCategory];
[pb addValueToPayload:_action forKey:kSPStuctAction];
[pb addValueToPayload:_label forKey:kSPStuctLabel];
[pb addValueToPayload:_property forKey:kSPStuctProperty];
[pb addValueToPayload:[NSString stringWithFormat:@"%g", [_value doubleValue]] forKey:kSPStuctValue];
return [self addDefaultParamsToPayload:pb];
}
@end
// Unstructured Event
@implementation SPUnstructured {
SPSelfDescribingJson * _eventData;
}
+ (instancetype) build:(void(^)(id<SPUnstructuredBuilder>builder))buildBlock {
SPUnstructured* event = [SPUnstructured new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:(_eventData != nil) withMessage:@"EventData cannot be nil."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setEventData:(SPSelfDescribingJson *)eventData {
_eventData = eventData;
}
// --- Public Methods
- (SPPayload *) getPayloadWithEncoding:(BOOL)encoding {
SPPayload *pb = [[SPPayload alloc] init];
[pb addValueToPayload:kSPEventUnstructured forKey:kSPEvent];
SPSelfDescribingJson * sdj = [[SPSelfDescribingJson alloc] initWithSchema:kSPUnstructSchema
andSelfDescribingJson:_eventData];
[pb addDictionaryToPayload:[sdj getAsDictionary]
base64Encoded:encoding
typeWhenEncoded:kSPUnstructuredEncoded
typeWhenNotEncoded:kSPUnstructured];
return [self addDefaultParamsToPayload:pb];
}
@end
// ScreenView Event
@implementation SPScreenView {
NSString * _name;
NSString * _id;
}
+ (instancetype) build:(void(^)(id<SPScreenViewBuilder>builder))buildBlock {
SPScreenView* event = [SPScreenView new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:([_name length] != 0 || [_id length] != 0) withMessage:@"Name and ID cannot both be nil and empty."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setName:(NSString *)name {
_name = name;
}
- (void) setId:(NSString *)sId {
_id = sId;
}
// --- Public Methods
- (SPSelfDescribingJson *) getPayload {
NSMutableDictionary * event = [[NSMutableDictionary alloc] init];
if (_id != nil) {
[event setObject:_id forKey:kSPSvId];
}
if (_name != nil) {
[event setObject:_name forKey:kSPSvName];
}
return [[SPSelfDescribingJson alloc] initWithSchema:kSPScreenViewSchema
andData:event];
}
@end
// Timing Event
@implementation SPTiming {
NSString * _category;
NSString * _variable;
NSNumber * _timing;
NSString * _label;
}
+ (instancetype) build:(void(^)(id<SPTimingBuilder>builder))buildBlock {
SPTiming* event = [SPTiming new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:([_category length] != 0) withMessage:@"Category cannot be nil or empty."];
[SPUtilities checkArgument:([_variable length] != 0) withMessage:@"Variable cannot be nil or empty."];
[SPUtilities checkArgument:(_timing != nil) withMessage:@"Timing cannot be nil."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setCategory:(NSString *)category {
_category = category;
}
- (void) setVariable:(NSString *)variable {
_variable = variable;
}
- (void) setTiming:(NSInteger)timing {
_timing = [NSNumber numberWithLong:timing];
}
- (void) setLabel:(NSString *)label {
_label = label;
}
// --- Public Methods
- (SPSelfDescribingJson *) getPayload {
NSMutableDictionary * event = [[NSMutableDictionary alloc] init];
[event setObject:_category forKey:kSPUtCategory];
[event setObject:_variable forKey:kSPUtVariable];
[event setObject:_timing forKey:kSPUtTiming];
if (_label != nil) {
[event setObject:_label forKey:kSPUtLabel];
}
return [[SPSelfDescribingJson alloc] initWithSchema:kSPUserTimingsSchema
andData:event];
}
@end
// Ecommerce Event
@implementation SPEcommerce {
NSString * _orderId;
NSNumber * _totalValue;
NSString * _affiliation;
NSNumber * _taxValue;
NSNumber * _shipping;
NSString * _city;
NSString * _state;
NSString * _country;
NSString * _currency;
NSArray * _items;
}
+ (instancetype) build:(void(^)(id<SPEcommTransactionBuilder>builder))buildBlock {
SPEcommerce* event = [SPEcommerce new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:([_orderId length] != 0) withMessage:@"OrderId cannot be nil or empty."];
[SPUtilities checkArgument:(_items != nil) withMessage:@"Items cannot be nil."];
[SPUtilities checkArgument:(_totalValue != nil) withMessage:@"TotalValue cannot be nil."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setOrderId:(NSString *)orderId {
_orderId = orderId;
}
- (void) setTotalValue:(double)totalValue {
_totalValue = [NSNumber numberWithDouble:totalValue];
}
- (void) setAffiliation:(NSString *)affiliation {
_affiliation = affiliation;
}
- (void) setTaxValue:(double)taxValue {
_taxValue = [NSNumber numberWithDouble:taxValue];
}
- (void) setShipping:(double)shipping {
_shipping = [NSNumber numberWithDouble:shipping];
}
- (void) setCity:(NSString *)city {
_city = city;
}
- (void) setState:(NSString *)state {
_state = state;
}
- (void) setCountry:(NSString *)country {
_country = country;
}
- (void) setCurrency:(NSString *)currency {
_currency = currency;
}
- (void) setItems:(NSArray *)items {
_items = items;
}
// --- Public Methods
- (SPPayload *) getPayload {
SPPayload *pb = [[SPPayload alloc] init];
[pb addValueToPayload:kSPEventEcomm forKey:kSPEvent];
[pb addValueToPayload:_orderId forKey:kSPEcommId];
[pb addValueToPayload:[NSString stringWithFormat:@"%.02f", [_totalValue doubleValue]] forKey:kSPEcommTotal];
[pb addValueToPayload:_affiliation forKey:kSPEcommAffiliation];
[pb addValueToPayload:[NSString stringWithFormat:@"%.02f", [_taxValue doubleValue]] forKey:kSPEcommTax];
[pb addValueToPayload:[NSString stringWithFormat:@"%.02f", [_shipping doubleValue]] forKey:kSPEcommShipping];
[pb addValueToPayload:_city forKey:kSPEcommCity];
[pb addValueToPayload:_state forKey:kSPEcommState];
[pb addValueToPayload:_country forKey:kSPEcommCountry];
[pb addValueToPayload:_currency forKey:kSPEcommCurrency];
return [self addDefaultParamsToPayload:pb];
}
- (NSArray *) getItems {
return _items;
}
@end
// Ecommerce Item Event
@implementation SPEcommerceItem {
NSString * _itemId;
NSString * _sku;
NSNumber * _price;
NSNumber * _quantity;
NSString * _name;
NSString * _category;
NSString * _currency;
}
+ (instancetype) build:(void(^)(id<SPEcommTransactionItemBuilder>builder))buildBlock {
SPEcommerceItem* event = [SPEcommerceItem new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:([_itemId length] != 0) withMessage:@"ItemId cannot be nil or empty."];
[SPUtilities checkArgument:([_sku length] != 0) withMessage:@"SKU cannot be nil or empty."];
[SPUtilities checkArgument:(_price != nil) withMessage:@"Price cannot be nil."];
[SPUtilities checkArgument:(_quantity != nil) withMessage:@"Quantity cannot be nil."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setItemId:(NSString *)itemId {
_itemId = itemId;
}
- (void) setSku:(NSString *)sku {
_sku = sku;
}
- (void) setPrice:(double)price {
_price = [NSNumber numberWithDouble:price];
}
- (void) setQuantity:(NSInteger)quantity {
_quantity = [NSNumber numberWithLong:quantity];
}
- (void) setName:(NSString *)name {
_name = name;
}
- (void) setCategory:(NSString *)category {
_category = category;
}
- (void) setCurrency:(NSString *)currency {
_currency = currency;
}
// --- Public Methods
- (SPPayload *) getPayload {
SPPayload *pb = [[SPPayload alloc] init];
[pb addValueToPayload:kSPEventEcommItem forKey:kSPEvent];
[pb addValueToPayload:_itemId forKey:kSPEcommItemId];
[pb addValueToPayload:_sku forKey:kSPEcommItemSku];
[pb addValueToPayload:_name forKey:kSPEcommItemName];
[pb addValueToPayload:_category forKey:kSPEcommItemCategory];
[pb addValueToPayload:[NSString stringWithFormat:@"%.02f", [_price doubleValue]] forKey:kSPEcommItemPrice];
[pb addValueToPayload:[NSString stringWithFormat:@"%ld", [_quantity longValue]] forKey:kSPEcommItemQuantity];
[pb addValueToPayload:_currency forKey:kSPEcommItemCurrency];
return [self addDefaultParamsToPayload:pb];
}
@end
// Push Notification Content
@implementation SPNotificationContent {
NSString * _title;
NSString * _subtitle;
NSString * _body;
NSNumber * _badge;
NSString * _sound;
NSString * _launchImageName;
NSDictionary * _userInfo;
NSArray * _attachments;
}
+ (instancetype) build:(void(^)(id<SPNotificationContentBuilder>builder))buildBlock {
SPNotificationContent* event = [SPNotificationContent new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:([_title length] != 0) withMessage:@"Title cannot be nil or empty."];
[SPUtilities checkArgument:([_body length] != 0) withMessage:@"Body cannot be nil or empty."];
[SPUtilities checkArgument:(_badge != nil) withMessage:@"Badge cannot be nil."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setTitle:(NSString *)title {
_title = title;
}
- (void) setSubtitle:(NSString *)subtitle {
_subtitle = subtitle;
}
- (void) setBody:(NSString *)body {
_body = body;
}
- (void) setBadge:(NSNumber *)badge {
_badge = badge;
}
- (void) setSound:(NSString *)sound {
_sound = sound;
}
- (void) setLaunchImageName:(NSString *)name {
_launchImageName = name;
}
- (void) setUserInfo:(NSDictionary *)userInfo {
_userInfo = [SPUtilities replaceHyphenatedKeysWithCamelcase:userInfo];
}
- (void) setAttachments:(NSArray *)attachments {
_attachments = attachments;
}
// --- Public Methods
- (NSDictionary *) getPayload {
NSMutableDictionary * event = [[NSMutableDictionary alloc] init];
[event setObject:_title forKey:kSPPnTitle];
[event setObject:_body forKey:kSPPnBody];
[event setValue:_badge forKey:kSPPnBadge];
if (_subtitle != nil) {
[event setObject:_subtitle forKey:kSPPnSubtitle];
}
if (_subtitle != nil) {
[event setObject:_subtitle forKey:kSPPnSubtitle];
}
if (_sound != nil) {
[event setObject:_sound forKey:kSPPnSound];
}
if (_launchImageName != nil) {
[event setObject:_launchImageName forKey:kSPPnLaunchImageName];
}
if (_userInfo != nil) {
NSMutableDictionary * aps = nil;
NSMutableDictionary * newUserInfo = nil;
// modify contentAvailable value "1" and "0" to @YES and @NO to comply with schema
if (![[_userInfo valueForKeyPath:@"aps.contentAvailable"] isEqual:nil] &&
[[_userInfo objectForKey:@"aps"] isKindOfClass:[NSDictionary class]]) {
aps = [[NSMutableDictionary alloc] initWithDictionary:_userInfo[@"aps"]];
if ([[_userInfo valueForKeyPath:@"aps.contentAvailable"] isEqual:@1]) {
[aps setObject:@YES forKey:@"contentAvailable"];
} else if ([[_userInfo valueForKeyPath:@"aps.contentAvailable"] isEqual:@0]) {
[aps setObject:@NO forKey:@"contentAvailable"];
}
newUserInfo = [[NSMutableDictionary alloc] initWithDictionary:_userInfo];
[newUserInfo setObject:aps forKey:@"aps"];
}
[event setObject:[[NSDictionary alloc] initWithDictionary:newUserInfo] forKey:kSPPnUserInfo];
}
if (_attachments != nil) {
[event setObject:_attachments forKey:kSPPnAttachments];
}
return [[NSDictionary alloc] initWithDictionary:event copyItems:YES];
}
@end
// Push Notification Event
@implementation SPPushNotification {
NSString * _action;
NSString * _trigger;
NSString * _date;
NSString * _category;
NSString * _thread;
SPNotificationContent * _notification;
}
+ (instancetype) build:(void(^)(id<SPPushNotificationBuilder>builder))buildBlock {
SPPushNotification* event = [SPPushNotification new];
if (buildBlock) { buildBlock(event); }
[event preconditions];
return event;
}
- (id) init {
self = [super init];
return self;
}
- (void) preconditions {
[SPUtilities checkArgument:([_date length] != 0) withMessage:@"Delivery date cannot be nil or empty."];
[SPUtilities checkArgument:([_action length] != 0) withMessage:@"Action cannot be nil or empty."];
[SPUtilities checkArgument:([_trigger length] != 0) withMessage:@"Trigger cannot be nil or empty."];
[SPUtilities checkArgument:([_category length] != 0) withMessage:@"Category identifier cannot be nil or empty."];
[SPUtilities checkArgument:([_thread length] != 0) withMessage:@"Thread identifier cannot be nil or empty."];
[SPUtilities checkArgument:(_notification != nil) withMessage:@"Notification cannot be nil."];
[self basePreconditions];
}
// --- Builder Methods
- (void) setAction:(NSString *)action {
_action = action;
}
- (void) setDeliveryDate:(NSString *)date {
_date = date;
}
- (void) setTrigger:(NSString *)trigger {
_trigger = trigger;
}
- (void) setCategoryIdentifier:(NSString *)category {
_category = category;
}
- (void) setThreadIdentifier:(NSString *)thread {
_thread = thread;
}
- (void) setNotification:(SPNotificationContent *)content {
_notification = content;
}
// --- Public Methods
- (SPSelfDescribingJson *) getPayload {
NSMutableDictionary * event = [[NSMutableDictionary alloc] init];
[event setObject:[_notification getPayload] forKey:kSPPushNotification];
[event setObject:_trigger forKey:kSPPushTrigger];
[event setObject:_action forKey:kSPPushAction];
[event setObject:_date forKey:kSPPushDeliveryDate];
[event setObject:_category forKey:kSPPushCategoryId];
[event setObject:_thread forKey:kSPPushThreadId];
return [[SPSelfDescribingJson alloc] initWithSchema:kSPPushNotificationSchema andData:event];
}
@end