-
Notifications
You must be signed in to change notification settings - Fork 244
/
CountlyViewTrackingInternal.m
843 lines (692 loc) · 27.6 KB
/
CountlyViewTrackingInternal.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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
// CountlyViewTracking.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
@interface CountlyViewTrackingInternal ()
#if (TARGET_OS_IOS || TARGET_OS_TV)
@property (nonatomic) NSMutableSet* automaticViewTrackingExclusionList;
#endif
@property (nonatomic, strong) NSMutableDictionary<NSString*, CountlyViewData *> * viewDataDictionary;
@property (nonatomic) NSMutableDictionary* viewSegmentation;
@property (nonatomic) BOOL isFirstView;
@end
NSString* const kCountlyReservedEventView = @"[CLY]_view";
NSString* const kCountlyCurrentView = @"cly_cvn";
NSString* const kCountlyPreviousView = @"cly_pvn";
NSString* const kCountlyPreviousEventName = @"cly_pen";
NSString* const kCountlyVTKeyName = @"name";
NSString* const kCountlyVTKeySegment = @"segment";
NSString* const kCountlyVTKeyVisit = @"visit";
NSString* const kCountlyVTKeyStart = @"start";
NSString* const kCountlyVTKeyBounce = @"bounce";
NSString* const kCountlyVTKeyExit = @"exit";
NSString* const kCountlyVTKeyView = @"view";
NSString* const kCountlyVTKeyDomain = @"domain";
NSString* const kCountlyVTKeyDur = @"dur";
#if (TARGET_OS_IOS || TARGET_OS_TV)
@interface UIViewController (CountlyViewTracking)
- (void)Countly_viewDidAppear:(BOOL)animated;
- (void)Countly_viewDidDisappear:(BOOL)animated;
@end
#endif
@implementation CountlyViewTrackingInternal
+ (instancetype)sharedInstance
{
if (!CountlyCommon.sharedInstance.hasStarted)
return nil;
static CountlyViewTrackingInternal* s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
#if (TARGET_OS_IOS || TARGET_OS_TV)
self.automaticViewTrackingExclusionList =
@[
@"CLYInternalViewController",
@"UINavigationController",
@"UIAlertController",
@"UIPageViewController",
@"UITabBarController",
@"UIReferenceLibraryViewController",
@"UISplitViewController",
@"UIInputViewController",
@"UISearchController",
@"UISearchContainerViewController",
@"UIApplicationRotationFollowingController",
@"MFMailComposeInternalViewController",
@"MFMailComposeInternalViewController",
@"MFMailComposePlaceholderViewController",
@"UIInputWindowController",
@"_UIFallbackPresentationViewController",
@"UIActivityViewController",
@"UIActivityGroupViewController",
@"_UIActivityGroupListViewController",
@"_UIActivityViewControllerContentController",
@"UIKeyboardCandidateRowViewController",
@"UIKeyboardCandidateGridCollectionViewController",
@"UIPrintMoreOptionsTableViewController",
@"UIPrintPanelTableViewController",
@"UIPrintPanelViewController",
@"UIPrintPaperViewController",
@"UIPrintPreviewViewController",
@"UIPrintRangeViewController",
@"UIDocumentMenuViewController",
@"UIDocumentPickerViewController",
@"UIDocumentPickerExtensionViewController",
@"UIInterfaceActionGroupViewController",
@"UISystemInputViewController",
@"UIRecentsInputViewController",
@"UICompatibilityInputViewController",
@"UIInputViewAnimationControllerViewController",
@"UISnapshotModalViewController",
@"UIMultiColumnViewController",
@"UIKeyCommandDiscoverabilityHUDViewController"
].mutableCopy;
#endif
self.viewDataDictionary = NSMutableDictionary.new;
self.viewSegmentation = nil;
self.isFirstView = YES;
}
return self;
}
- (NSArray *)reservedViewTrackingSegmentationKeys
{
NSArray* reservedViewTrackingSegmentationKeys =
@[
kCountlyVTKeyName,
kCountlyVTKeySegment,
kCountlyVTKeyVisit,
kCountlyVTKeyStart,
kCountlyVTKeyBounce,
kCountlyVTKeyExit,
kCountlyVTKeyView,
kCountlyVTKeyDomain,
kCountlyVTKeyDur
];
return reservedViewTrackingSegmentationKeys;
}
#pragma mark - Public methods
- (void)setGlobalViewSegmentation:(NSMutableDictionary *)segmentation
{
CLY_LOG_I(@"%s %@", __FUNCTION__, segmentation);
NSMutableDictionary *mutableSegmentation = segmentation.mutableCopy;
[mutableSegmentation removeObjectsForKeys:self.reservedViewTrackingSegmentationKeys];
NSDictionary *filteredSegmentation = mutableSegmentation.cly_filterSupportedDataTypes;
self.viewSegmentation = filteredSegmentation.mutableCopy;
}
- (void)updateGlobalViewSegmentation:(NSDictionary *)segmentation
{
CLY_LOG_I(@"%s %@", __FUNCTION__, segmentation);
if (!self.viewSegmentation) {
self.viewSegmentation = NSMutableDictionary.new;
}
NSMutableDictionary *mutableSegmentation = segmentation.mutableCopy;
[mutableSegmentation removeObjectsForKeys:self.reservedViewTrackingSegmentationKeys];
NSDictionary *filteredSegmentation = mutableSegmentation.cly_filterSupportedDataTypes;
[self.viewSegmentation addEntriesFromDictionary:filteredSegmentation];
}
- (NSString *)startView:(NSString *)viewName segmentation:(NSDictionary *)segmentation
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, viewName, segmentation);
#if (TARGET_OS_IOS || TARGET_OS_TV)
if (self.isAutoViewTrackingActive) {
CLY_LOG_W(@"%s Manually start view tracking is not allowed when automatic tracking is enabled!", __FUNCTION__);
return nil;
}
#endif
NSString* viewID = [self startViewInternal:viewName customSegmentation:segmentation];
return viewID;
}
- (NSString *)startAutoStoppedView:(NSString *)viewName segmentation:(NSDictionary *)segmentation
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, viewName, segmentation);
#if (TARGET_OS_IOS || TARGET_OS_TV)
if (self.isAutoViewTrackingActive) {
CLY_LOG_W(@"%s Manually start view tracking is not allowed when automatic tracking is enabled!", __FUNCTION__);
return nil;
}
#endif
NSString* viewID = [self startViewInternal:viewName customSegmentation:segmentation isAutoStoppedView:YES];
return viewID;
}
- (void)stopViewWithName:(NSString *)viewName segmentation:(NSDictionary *)segmentation
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, viewName, segmentation);
#if (TARGET_OS_IOS || TARGET_OS_TV)
if (self.isAutoViewTrackingActive) {
CLY_LOG_W(@"%s Manually stop view tracking is not allowed when automatic tracking is enabled!", __FUNCTION__);
return;
}
#endif
[self stopViewWithNameInternal:viewName customSegmentation:segmentation];
}
- (void)stopViewWithID:(NSString *)viewID segmentation:(NSDictionary *)segmentation
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, viewID, segmentation);
#if (TARGET_OS_IOS || TARGET_OS_TV)
if (self.isAutoViewTrackingActive) {
CLY_LOG_W(@"%s Manually stop view tracking is not allowed when automatic tracking is enabled!", __FUNCTION__);
return;
}
#endif
[self stopViewWithIDInternal:viewID customSegmentation:segmentation];
}
- (void)pauseViewWithID:(NSString *)viewID
{
CLY_LOG_I(@"%s %@", __FUNCTION__, viewID);
#if (TARGET_OS_IOS || TARGET_OS_TV)
if (self.isAutoViewTrackingActive) {
CLY_LOG_W(@"%s Manually pause view tracking is not allowed when automatic tracking is enabled!", __FUNCTION__);
return;
}
#endif
[self pauseViewWithIDInternal:viewID];
}
- (void)resumeViewWithID:(NSString *)viewID
{
CLY_LOG_I(@"%s %@", __FUNCTION__, viewID);
#if (TARGET_OS_IOS || TARGET_OS_TV)
if (self.isAutoViewTrackingActive) {
CLY_LOG_W(@"%s Manually resume view tracking is not allowed when automatic tracking is enabled!", __FUNCTION__);
return;
}
#endif
[self resumeViewWithIDInternal:viewID];
}
- (void)stopAllViews:(NSDictionary *)segmentation
{
CLY_LOG_I(@"%s %@", __FUNCTION__, segmentation);
#if (TARGET_OS_IOS || TARGET_OS_TV)
if (self.isAutoViewTrackingActive) {
CLY_LOG_W(@"%s Manually stop view tracking is not allowed when automatic tracking is enabled!", __FUNCTION__);
return;
}
#endif
[self stopAllViewsInternal:segmentation];
}
#if (TARGET_OS_IOS || TARGET_OS_TV)
- (void)addAutoViewTrackingExclutionList:(NSArray *)viewTrackingExclusionList
{
[self.automaticViewTrackingExclusionList addObjectsFromArray:viewTrackingExclusionList];
}
- (void)startAutoViewTracking
{
if (!self.isEnabledOnInitialConfig)
return;
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
self.isAutoViewTrackingActive = YES;
[self swizzleViewTrackingMethods];
UIViewController* topVC = CountlyCommon.sharedInstance.topViewController;
NSString* viewTitle = [self titleForViewController:topVC];
[self startViewInternal:viewTitle customSegmentation:nil];
}
- (void)stopAutoViewTracking
{
self.isAutoViewTrackingActive = NO;
// self.currentView = nil;
// self.currentViewID = nil;
}
- (void)setIsAutoViewTrackingActive:(BOOL)isAutoViewTrackingActive
{
if (!self.isEnabledOnInitialConfig)
return;
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
if (_isAutoViewTrackingActive != isAutoViewTrackingActive) {
[self stopAllViewsInternal:nil];
}
_isAutoViewTrackingActive = isAutoViewTrackingActive;
}
#pragma mark - Public methods Deprecated
- (void)addExceptionForAutoViewTracking:(NSString *)exception
{
if (!exception.length)
return;
[self.automaticViewTrackingExclusionList addObject:exception];
}
- (void)removeExceptionForAutoViewTracking:(NSString *)exception
{
[self.automaticViewTrackingExclusionList removeObject:exception];
}
#endif
#pragma mark - Internal methods old
- (void)stopViewWithNameInternal:(NSString *) viewName customSegmentation:(NSDictionary *)customSegmentation
{
if (!viewName || !viewName.length)
{
CLY_LOG_D(@"%s View name should not be null or empty", __FUNCTION__);
return;
}
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
__block NSString *viewID = nil;
[self.viewDataDictionary enumerateKeysAndObjectsUsingBlock:^(NSString * key, CountlyViewData * viewData, BOOL * stop)
{
if ([viewData.viewName isEqualToString:viewName])
{
viewID = key;
*stop = YES;
}
}];
if (viewID)
{
[self stopViewWithIDInternal:viewID customSegmentation:customSegmentation];
}
else {
CLY_LOG_D(@"%s No View exist with name: %@", __FUNCTION__, viewName);
}
}
- (void)stopViewWithIDInternal:(NSString *) viewKey customSegmentation:(NSDictionary *)customSegmentation
{
[self stopViewWithIDInternal:viewKey customSegmentation:customSegmentation autoPaused:NO];
}
- (void)stopViewWithIDInternal:(NSString *) viewKey customSegmentation:(NSDictionary *)customSegmentation autoPaused:(BOOL) autoPaused{
if (!viewKey || !viewKey.length)
{
CLY_LOG_D(@"%s View ID should not be null or empty", __FUNCTION__);
return;
}
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
CountlyViewData* viewData = self.viewDataDictionary[viewKey];
if (viewData)
{
NSMutableDictionary* segmentation = NSMutableDictionary.new;
if (viewData.segmentation)
{
[segmentation addEntriesFromDictionary:viewData.segmentation];
}
if (self.viewSegmentation)
{
[segmentation addEntriesFromDictionary:self.viewSegmentation];
}
if (customSegmentation)
{
NSMutableDictionary* mutableCustomSegmentation = customSegmentation.mutableCopy;
[mutableCustomSegmentation removeObjectsForKeys:self.reservedViewTrackingSegmentationKeys];
NSDictionary *filteredSegmentation = mutableCustomSegmentation.cly_filterSupportedDataTypes;
[segmentation addEntriesFromDictionary:filteredSegmentation];
}
NSDictionary* segmentationTruncated = [segmentation cly_truncated:@"View segmentation"];
segmentation = [segmentationTruncated cly_limited:@"View segmentation"].mutableCopy;
segmentation[kCountlyVTKeyName] = viewData.viewName;
segmentation[kCountlyVTKeySegment] = CountlyDeviceInfo.osName;
NSInteger duration = viewData.duration;
[Countly.sharedInstance recordReservedEvent:kCountlyReservedEventView segmentation:segmentation count:1 sum:0 duration:duration ID:viewData.viewID timestamp:CountlyCommon.sharedInstance.uniqueTimestamp];
CLY_LOG_D(@"%s View tracking ended: %@ duration: %ld", __FUNCTION__, viewData.viewName, (long)duration);
if (!autoPaused) {
[self.viewDataDictionary removeObjectForKey:viewKey];
}
}
else {
CLY_LOG_D(@"%s No View exist with ID: %@", __FUNCTION__, viewKey);
}
}
- (NSString*)startViewInternal:(NSString *)viewName customSegmentation:(NSDictionary *)customSegmentation
{
return [self startViewInternal:viewName customSegmentation:customSegmentation isAutoStoppedView:NO];
}
- (NSString*)startViewInternal:(NSString *)viewName customSegmentation:(NSDictionary *)customSegmentation isAutoStoppedView:(BOOL) isAutoStoppedView
{
if (!viewName || !viewName.length)
{
CLY_LOG_D(@"%s View name should not be null or empty", __FUNCTION__);
return nil;
}
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return nil;
[self stopAutoStoppedView];
viewName = viewName.copy;
CLY_LOG_D(@"%s View tracking started: %@", __FUNCTION__, viewName);
viewName = [viewName cly_truncatedKey:@"View name"];
NSMutableDictionary* segmentation = NSMutableDictionary.new;
if (self.viewSegmentation)
{
[segmentation addEntriesFromDictionary:self.viewSegmentation];
}
if (customSegmentation)
{
NSMutableDictionary* mutableCustomSegmentation = customSegmentation.mutableCopy;
[mutableCustomSegmentation removeObjectsForKeys:self.reservedViewTrackingSegmentationKeys];
NSDictionary *filteredSegmentation = mutableCustomSegmentation.cly_filterSupportedDataTypes;
[segmentation addEntriesFromDictionary:filteredSegmentation];
}
NSDictionary* segmentationTruncated = [segmentation cly_truncated:@"View segmentation"];
segmentation = [segmentationTruncated cly_limited:@"View segmentation"].mutableCopy;
segmentation[kCountlyVTKeyName] = viewName;
segmentation[kCountlyVTKeySegment] = CountlyDeviceInfo.osName;
segmentation[kCountlyVTKeyVisit] = @1;
if (self.isFirstView && [CountlyConnectionManager.sharedInstance isSessionStarted])
{
self.isFirstView = NO;
segmentation[kCountlyVTKeyStart] = @1;
}
self.previousViewID = self.currentViewID;
self.currentViewID = CountlyCommon.sharedInstance.randomEventID;
self.previousViewName = self.currentViewName;
self.currentViewName = viewName;
CountlyViewData *viewData = [[CountlyViewData alloc] initWithID:self.currentViewID viewName:viewName];
viewData.startSegmentation = customSegmentation.mutableCopy;
viewData.isAutoStoppedView = isAutoStoppedView;
self.viewDataDictionary[self.currentViewID] = viewData;
[Countly.sharedInstance recordReservedEvent:kCountlyReservedEventView segmentation:segmentation ID:self.currentViewID];
CLY_LOG_D(@"%s View name: %@ View ID: %@ isAutoStoppedView: %@", __FUNCTION__, viewName, self.currentViewID, isAutoStoppedView ? @"YES" : @"NO");
return self.currentViewID;
}
- (void)pauseViewWithIDInternal:(NSString *) viewID
{
if (!viewID || !viewID.length)
{
CLY_LOG_D(@"%s View ID should not be null or empty", __FUNCTION__);
return;
}
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
CountlyViewData* viewData = self.viewDataDictionary[viewID];
if (viewData)
{
[self pauseViewInternal:viewData];
}
else {
CLY_LOG_D(@"%s No View exist with ID: %@", __FUNCTION__, viewID);
}
}
- (void)resumeViewWithIDInternal:(NSString *) viewID
{
if (!viewID || !viewID.length)
{
CLY_LOG_D(@"%s View ID should not be null or empty", __FUNCTION__);
return;
}
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
CountlyViewData* viewData = self.viewDataDictionary[viewID];
if (viewData)
{
[viewData resumeView];
}
else {
CLY_LOG_D(@"%s No View exist with ID: %@", __FUNCTION__, viewID);
}
}
-(CountlyViewData* ) currentView
{
if (!self.currentViewID)
return nil;
return [self.viewDataDictionary objectForKey:self.currentViewID];
}
- (void)stopAutoStoppedView
{
CountlyViewData* currentView = self.currentView;
if (currentView && currentView.isAutoStoppedView && !currentView.willStartAgain)
{
[self stopViewWithIDInternal:self.currentView.viewID customSegmentation:nil];
}
}
- (void)stopCurrentView
{
if (self.currentView)
{
[self stopViewWithIDInternal:self.currentView.viewID customSegmentation:nil];
}
}
- (void)stopRunningViewsInternal
{
[self.viewDataDictionary enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, CountlyViewData * _Nonnull viewData, BOOL * _Nonnull stop) {
viewData.willStartAgain = YES;
[self stopViewWithIDInternal:viewData.viewID customSegmentation:nil autoPaused:YES];
}];
}
- (void)pauseViewInternal:(CountlyViewData*) viewData
{
[self stopViewWithIDInternal:viewData.viewID customSegmentation:nil autoPaused:YES];
[viewData pauseView];
}
- (void)startStoppedViewsInternal
{
// Create an array to store keys for views that need to be removed
NSMutableArray<NSString *> *keysToRemove = [NSMutableArray array];
NSMutableArray<NSString *> *keysToStart = [NSMutableArray array];
// Collect keys without modifying the dictionary
[self.viewDataDictionary enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, CountlyViewData * _Nonnull viewData, BOOL * _Nonnull stop) {
if (viewData.willStartAgain)
{
[keysToStart addObject:key];
[keysToRemove addObject:viewData.viewID];
}
}];
// Start the collected views after enumeration
for (NSString *key in keysToStart)
{
CountlyViewData *viewData = self.viewDataDictionary[key];
NSString *viewID = [self startViewInternal:viewData.viewName customSegmentation:viewData.startSegmentation isAutoStoppedView:viewData.isAutoStoppedView];
// Retrieve and update the newly created viewData
CountlyViewData *viewDataNew = self.viewDataDictionary[viewID];
viewDataNew.segmentation = viewData.segmentation.mutableCopy;
}
// Remove the entries from the dictionary
[self.viewDataDictionary removeObjectsForKeys:keysToRemove];
}
- (void)stopAllViewsInternal:(NSDictionary *)segmentation
{
// TODO: Should apply all the segmenation operations here at one place instead of doing it for individual view
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
[self.viewDataDictionary enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, CountlyViewData * _Nonnull viewData, BOOL * _Nonnull stop) {
[self stopViewWithIDInternal:key customSegmentation:segmentation];
}];
}
- (void)addSegmentationToViewWithNameInternal:(NSString *) viewName segmentation:(NSDictionary *)segmentation
{
if (!viewName || !viewName.length)
{
CLY_LOG_D(@"%s View name should not be null or empty", __FUNCTION__);
return;
}
__block NSString *viewID = nil;
[self.viewDataDictionary enumerateKeysAndObjectsUsingBlock:^(NSString * key, CountlyViewData * viewData, BOOL * stop)
{
if ([viewData.viewName isEqualToString:viewName])
{
viewID = key;
*stop = YES;
}
}];
if (viewID)
{
[self addSegmentationToViewWithIDInternal:viewID segmentation:segmentation];
}
else {
CLY_LOG_D(@"%s No View exist with name: %@", __FUNCTION__, viewName);
}
}
- (void)addSegmentationToViewWithIDInternal:(NSString *) viewID segmentation:(NSDictionary *)segmentation{
if (!viewID || !viewID.length)
{
CLY_LOG_D(@"%s View ID should not be null or empty", __FUNCTION__);
return;
}
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
CountlyViewData* viewData = self.viewDataDictionary[viewID];
if (viewData)
{
NSMutableDictionary *mutableSegmentation = segmentation.mutableCopy;
[mutableSegmentation removeObjectsForKeys:self.reservedViewTrackingSegmentationKeys];
NSDictionary *filteredSegmentation = mutableSegmentation.cly_filterSupportedDataTypes;
if(filteredSegmentation) {
if(!viewData.segmentation) {
viewData.segmentation = NSMutableDictionary.new;
}
[viewData.segmentation addEntriesFromDictionary:filteredSegmentation];
}
[self.viewDataDictionary setObject:viewData forKey:viewID];
}
else {
CLY_LOG_D(@"%s No View exist with ID: %@", __FUNCTION__, viewID);
}
}
- (void)addSegmentationToViewWithID:(NSString *)viewID segmentation:(NSDictionary *)segmentation
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, viewID, segmentation);
[self addSegmentationToViewWithIDInternal:viewID segmentation:segmentation];
}
- (void)addSegmentationToViewWithName:(NSString *)viewName segmentation:(NSDictionary *)segmentation
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, viewName, segmentation);
[self addSegmentationToViewWithNameInternal:viewName segmentation:segmentation];
}
#pragma mark - Internal auto view tracking methods
#if (TARGET_OS_IOS || TARGET_OS_TV)
- (void)swizzleViewTrackingMethods
{
static BOOL alreadySwizzled;
if (alreadySwizzled)
return;
alreadySwizzled = YES;
Method O_method = class_getInstanceMethod(UIViewController.class, @selector(viewDidAppear:));
Method C_method = class_getInstanceMethod(UIViewController.class, @selector(Countly_viewDidAppear:));
method_exchangeImplementations(O_method, C_method);
O_method = class_getInstanceMethod(UIViewController.class, @selector(viewDidDisappear:));
C_method = class_getInstanceMethod(UIViewController.class, @selector(Countly_viewDidDisappear:));
method_exchangeImplementations(O_method, C_method);
}
- (void)performAutoViewTrackingForViewController:(UIViewController *)viewController
{
if (!self.isAutoViewTrackingActive)
return;
if (!CountlyConsentManager.sharedInstance.consentForViewTracking)
return;
NSString* viewTitle = [self titleForViewController:viewController];
if (self.currentView && [self.currentView.viewName isEqualToString:viewTitle])
return;
BOOL isException = NO;
for (NSString* exception in self.automaticViewTrackingExclusionList)
{
isException = [viewTitle isEqualToString:exception] ||
[viewController isKindOfClass:NSClassFromString(exception)] ||
[NSStringFromClass(viewController.class) isEqualToString:exception];
if (isException)
{
CLY_LOG_V(@"%s %@ is an exceptional view, so it will be ignored for view tracking.", __FUNCTION__, viewTitle);
break;
}
}
if (!isException)
[self startViewInternal:viewTitle customSegmentation:nil];
}
- (NSString*)titleForViewController:(UIViewController *)viewController
{
if (!viewController)
return nil;
NSString* title = nil;
if ([viewController respondsToSelector:@selector(countlyAutoViewTrackingName)])
{
CLY_LOG_I(@"%s Viewcontroller conforms to CountlyAutoViewTrackingName protocol for custom auto view tracking name.", __FUNCTION__);
title = [(id<CountlyAutoViewTrackingName>)viewController countlyAutoViewTrackingName];
}
if (!title)
title = viewController.title;
if (!title)
title = [viewController.navigationItem.titleView isKindOfClass:UILabel.class] ? ((UILabel *)viewController.navigationItem.titleView).text : nil;
if (!title)
title = viewController.navigationItem.title;
if (!title)
title = NSStringFromClass(viewController.class);
return title;
}
#endif
#pragma mark - Public function for application state
- (void)applicationWillEnterForeground {
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)
if (!self.isAutoViewTrackingActive) {
[self startStoppedViewsInternal];
}
#else
[self startStoppedViewsInternal];
#endif
}
- (void)applicationDidEnterBackground {
#if (TARGET_OS_IOS || TARGET_OS_VISION || TARGET_OS_TV)
if (self.isAutoViewTrackingActive) {
[self stopCurrentView];
}
else {
[self stopRunningViewsInternal];
}
#else
[self stopRunningViewsInternal];
#endif
}
- (void)applicationWillTerminate {
[self stopAllViewsInternal:nil];
}
- (void)resetFirstView
{
self.isFirstView = YES;
}
@end
#pragma mark -
#if (TARGET_OS_IOS || TARGET_OS_TV)
@implementation UIViewController (CountlyViewTracking)
- (void)Countly_viewDidAppear:(BOOL)animated
{
[self Countly_viewDidAppear:animated];
[CountlyViewTrackingInternal.sharedInstance performAutoViewTrackingForViewController:self];
if (self.isPageSheetModal)
{
//NOTE: Since iOS 13, modals with PageSheet presentation style
// does not trigger `viewDidAppear` on presenting view controller when they are dismissed.
// Also, `self.presentingViewController` property is nil in both `viewWillDisappear` and `viewDidDisappear`.
// So, we store it here in modal's `viewDidAppear` to be used for view tracking later.
CLY_LOG_I(@"%s A modal view controller with PageSheet presentation style is presented on iOS 13+. Storing presenting view controller to be used later.", __FUNCTION__);
UIViewController* presenting = self.presentingViewController;
if ([presenting isKindOfClass:UINavigationController.class])
{
presenting = ((UINavigationController *)presenting).topViewController;
}
self.presentingVC = presenting;
}
}
- (void)Countly_viewDidDisappear:(BOOL)animated
{
[self Countly_viewDidDisappear:animated];
if (self.presentingVC)
{
CLY_LOG_I(@"%s A modal view controller with PageSheet presentation style is dismissed on iOS 13+. Forcing auto view tracking with stored presenting view controller.", __FUNCTION__);
[CountlyViewTrackingInternal.sharedInstance performAutoViewTrackingForViewController:self.presentingVC];
self.presentingVC = nil;
}
}
- (BOOL)isPageSheetModal
{
//NOTE: iOS 13 check is not related to availability of UIModalPresentationPageSheet,
// but needed due to behavioral difference in presenting logic compared to previous iOS versions.
#if (TARGET_OS_IOS)
if (@available(iOS 13.0, *))
{
if (self.modalPresentationStyle == UIModalPresentationPageSheet && self.isBeingPresented)
{
return YES;
}
}
#endif
return NO;
}
- (void)setPresentingVC:(UIViewController *)presentingVC
{
objc_setAssociatedObject(self, @selector(presentingVC), presentingVC, OBJC_ASSOCIATION_ASSIGN);
}
- (UIViewController *)presentingVC
{
return objc_getAssociatedObject(self, @selector(presentingVC));
}
@end
#endif