Permalink
Browse files

Added AnalyticsKitUnitTestProvider and AnalyticsKitEvent for inspecting

logged events in tests
Renamed AnalyticsKitDebug to AnalyticsKitDebugProvider
  • Loading branch information...
1 parent 4d69684 commit a3d708d974cb4505b9802c5e662f4964b63ff04a @thuss thuss committed Nov 14, 2012
View
@@ -31,12 +31,12 @@
//Logging events
-(void)logScreen:(NSString *)screenName;
--(void)logEvent:(NSString *)value;
+-(void)logEvent:(NSString *)event;
-(void)logEvent:(NSString *)event withProperty:(NSString *)key andValue:(NSString *)value;
-(void)logEvent:(NSString *)event withProperties:(NSDictionary *)dict;
--(void)logEvent:(NSString *)eventName timed:(BOOL)timed;
--(void)logEvent:(NSString *)eventName withProperties:(NSDictionary *)dict timed:(BOOL)timed;
--(void)endTimedEvent:(NSString *)eventName withProperties:(NSDictionary *)dict;
+-(void)logEvent:(NSString *)event timed:(BOOL)timed;
+-(void)logEvent:(NSString *)event withProperties:(NSDictionary *)dict timed:(BOOL)timed;
+-(void)endTimedEvent:(NSString *)event withProperties:(NSDictionary *)dict;
-(void)logError:(NSString *)name message:(NSString *)message exception:(NSException *)exception;
-(void)logError:(NSString *)name message:(NSString *)message error:(NSError *)error;
@@ -45,20 +45,21 @@
@interface AnalyticsKit : NSObject
++(void)initialize;
+(void)initializeLoggers:(NSArray *)loggers;
-
++(NSArray *)loggers;
+(void)applicationWillEnterForeground;
+(void)applicationDidEnterBackground;
+(void)applicationWillTerminate;
+(void)uncaughtException:(NSException *)exception;
+(void)logScreen:(NSString *)screenName;
-+(void)logEvent:(NSString *)value;
-+(void)logEvent:(NSString *)key withProperty:(NSString *)property andValue:(NSString *)value;
++(void)logEvent:(NSString *)event;
++(void)logEvent:(NSString *)event withProperty:(NSString *)key andValue:(NSString *)value;
+(void)logEvent:(NSString *)event withProperties:(NSDictionary *)dict;
-+(void)logEvent:(NSString *)eventName timed:(BOOL)timed;
-+(void)logEvent:(NSString *)eventName withProperties:(NSDictionary *)dict timed:(BOOL)timed;
-+(void)endTimedEvent:(NSString *)eventName withProperties:(NSDictionary *)dict;
++(void)logEvent:(NSString *)event timed:(BOOL)timed;
++(void)logEvent:(NSString *)event withProperties:(NSDictionary *)dict timed:(BOOL)timed;
++(void)endTimedEvent:(NSString *)event withProperties:(NSDictionary *)dict;
+(void)logError:(NSString *)name message:(NSString *)message exception:(NSException *)exception;
+(void)logError:(NSString *)name message:(NSString *)message error:(NSError *)error;
View
@@ -24,6 +24,10 @@ +(void)initializeLoggers:(NSArray *)loggers {
_loggers = loggers;
}
++(NSArray *)loggers {
+ return _loggers;
+}
+
+(void)applicationWillEnterForeground {
AKINFO(@"");
for (id<AnalyticsKitProvider> logger in _loggers) {
@@ -3,7 +3,7 @@
// TeamStream
//
// Created by Susan Detwiler on 1/26/12.
-// Copyright (c) 2012 Bleacher Report. All rights reserved.
+// Copyright (c) 2012 Two Bit Labs. All rights reserved.
//
#import "AnalyticsKit.h"
@@ -3,7 +3,7 @@
// TeamStream
//
// Created by Susan Detwiler on 1/26/12.
-// Copyright (c) 2012 Bleacher Report. All rights reserved.
+// Copyright (c) 2012 Two Bit Labs. All rights reserved.
//
#import "AnalyticsKitApsalarProvider.h"
View
@@ -1,13 +0,0 @@
-//
-// AnalyticsKitDebug.h
-// TeamStream
-//
-// Created by Susan Detwiler on 5/29/12.
-// Copyright (c) 2012 Bleacher Report. All rights reserved.
-//
-
-#import "AnalyticsKit.h"
-
-@interface AnalyticsKitDebug : AnalyticsKit
-
-@end
@@ -0,0 +1,13 @@
+//
+// AnalyticsKitDebug.h
+// TeamStream
+//
+// Created by Susan Detwiler on 5/29/12.
+// Copyright (c) 2012 Two Bit Labs. All rights reserved.
+//
+
+#import "AnalyticsKit.h"
+
+@interface AnalyticsKitDebugProvider : NSObject<AnalyticsKitProvider>
+
+@end
@@ -3,12 +3,12 @@
// TeamStream
//
// Created by Susan Detwiler on 5/29/12.
-// Copyright (c) 2012 Bleacher Report. All rights reserved.
+// Copyright (c) 2012 Two Bit Labs. All rights reserved.
//
-#import "AnalyticsKitDebug.h"
+#import "AnalyticsKitDebugProvider.h"
-@implementation AnalyticsKitDebug
+@implementation AnalyticsKitDebugProvider
#pragma mark -
#pragma mark Lifecycle
View
@@ -0,0 +1,19 @@
+//
+// AnalyticsKitEvent.h
+// TeamStream
+//
+// Created by Todd Huss on 11/14/12.
+// Copyright (c) 2012 Two Bit Labs. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface AnalyticsKitEvent : NSObject
+
+@property(nonatomic,strong)NSString *name;
+@property(nonatomic,strong)NSDictionary *properties;
+
+- (id)initEvent:(NSString *)event withProperties:(NSDictionary *)dict;
+- (id)initEvent:(NSString *)event;
+
+@end
View
@@ -0,0 +1,26 @@
+//
+// AnalyticsKitEvent.m
+// TeamStream
+//
+// Created by Todd Huss on 11/14/12.
+// Copyright (c) 2012 Two Bit Labs. All rights reserved.
+//
+
+#import "AnalyticsKitEvent.h"
+
+@implementation AnalyticsKitEvent
+
+- (id)initEvent:(NSString *)event withProperties:(NSDictionary *)dict {
+ self = [super init];
+ if (self) {
+ self.name = [event copy];
+ self.properties = dict;
+ }
+ return self;
+}
+
+- (id)initEvent:(NSString *)event {
+ return [self initEvent:event withProperties:nil];
+}
+
+@end
@@ -3,7 +3,7 @@
// TeamStream
//
// Created by Susan Detwiler on 11/10/11.
-// Copyright (c) 2011 Bleacher Report. All rights reserved.
+// Copyright (c) 2011 Two Bit Labs. All rights reserved.
//
#import "AnalyticsKit.h"
@@ -3,7 +3,7 @@
// TeamStream
//
// Created by Susan Detwiler on 11/10/11.
-// Copyright (c) 2011 Bleacher Report. All rights reserved.
+// Copyright (c) 2011 Two Bit Labs. All rights reserved.
//
#import "Flurry.h"
@@ -0,0 +1,24 @@
+//
+// AnalyticsKitUnitTestProvider.h
+// TeamStream
+//
+// Created by Todd Huss on 11/14/12.
+// Copyright (c) 2012 Two Bit Labs. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "AnalyticsKitEvent.h"
+
+@interface AnalyticsKitUnitTestProvider : NSObject<AnalyticsKitProvider>
+
+@property(nonatomic,strong)NSMutableArray *events;
+
++ (AnalyticsKitUnitTestProvider *)setUp;
++ (void)clearEvents; // also called by tearDown
++ (void)tearDown;
+
+- (BOOL)hasEventLoggedWithName:(NSString *)eventName;
+- (AnalyticsKitEvent *)firstEventLoggedWithName:(NSString *)eventName;
+- (NSArray *)eventsLoggedWithName:(NSString *)eventName;
+
+@end
@@ -0,0 +1,100 @@
+//
+// AnalyticsKitUnitTestProvider.m
+// TeamStream
+//
+// Created by Todd Huss on 11/14/12.
+// Copyright (c) 2012 Two Bit Labs. All rights reserved.
+//
+
+#import "AnalyticsKitUnitTestProvider.h"
+
+@implementation AnalyticsKitUnitTestProvider
+
++ (AnalyticsKitUnitTestProvider *)setUp {
+ AnalyticsKitUnitTestProvider *provider = [[AnalyticsKitUnitTestProvider alloc] init];
+ provider.events = [NSMutableArray arrayWithCapacity:20];
+ [AnalyticsKit initializeLoggers:@[provider]];
+ return provider;
+}
+
++ (void)clearEvents {
+ // Remove the events stored in the unit test provider
+ NSArray *loggers = [AnalyticsKit loggers];
+ for (AnalyticsKitUnitTestProvider *provider in loggers) {
+ if ([provider isKindOfClass:[AnalyticsKitUnitTestProvider class]]) {
+ provider.events = nil;
+ }
+ }
+}
+
++ (void)tearDown {
+ [self clearEvents];
+ // Wipe out any loggers
+ [AnalyticsKit initialize];
+}
+
+- (BOOL)hasEventLoggedWithName:(NSString *)eventName {
+ return [self firstEventLoggedWithName:eventName] != nil;
+}
+
+- (AnalyticsKitEvent *)firstEventLoggedWithName:(NSString *)eventName {
+ AnalyticsKitEvent *event = nil;
+ NSArray *matchingEvents = [self eventsLoggedWithName:eventName];
+ if ([matchingEvents count] > 0) event = matchingEvents[0];
+ return event;
+}
+
+- (NSArray *)eventsLoggedWithName:(NSString *)eventName {
+ NSMutableArray *matchingEvents = [NSMutableArray arrayWithCapacity:5];
+ for (AnalyticsKitEvent *event in self.events) {
+ if ([eventName isEqualToString:event.name]) [matchingEvents addObject:event];
+ }
+ return matchingEvents;
+}
+
+#pragma mark -
+#pragma mark Lifecycle
+
+-(void)applicationWillEnterForeground{}
+
+-(void)applicationDidEnterBackground{}
+
+-(void)applicationWillTerminate{}
+
+-(void)uncaughtException:(NSException *)exception{}
+
+#pragma mark -
+#pragma mark Event Logging
+
+-(void)logScreen:(NSString *)screenName{
+ NSString *event = [@"Screen - " stringByAppendingString:screenName];
+ [self logEvent:event withProperties:nil];
+}
+
+-(void)logEvent:(NSString *)event {
+ [self logEvent:event withProperties:nil];
+}
+
+-(void)logEvent:(NSString *)event withProperty:(NSString *)key andValue:(NSString *)value {
+ [self logEvent:event withProperties:@{key:value}];
+}
+
+-(void)logEvent:(NSString *)event withProperties:(NSDictionary *)dict {
+ [self.events addObject:[[AnalyticsKitEvent alloc] initEvent:event withProperties:dict]];
+}
+
+-(void)logEvent:(NSString *)event timed:(BOOL)timed{
+ [self logEvent:event withProperties:nil];
+}
+
+-(void)logEvent:(NSString *)event withProperties:(NSDictionary *)dict timed:(BOOL)timed{
+ [self logEvent:event withProperties:dict];
+}
+
+-(void)endTimedEvent:(NSString *)event withProperties:(NSDictionary *)dict{}
+
+-(void)logError:(NSString *)name message:(NSString *)message exception:(NSException *)exception{}
+
+-(void)logError:(NSString *)name message:(NSString *)message error:(NSError *)error{}
+
+@end
View
@@ -21,6 +21,8 @@ INCLUDED PROVIDERS
* [Flurry](http://www.flurry.com/)
* [Apsalar](http://apsalar.com/)
* [Mixpanel](https://mixpanel.com/)
+* Debug Provider: that shows an AlertView whenever an error is logged
+* Unit Test Provider: that allows you to introspect events that were logged
USAGE
=====
@@ -52,7 +54,7 @@ NSMutableArray *loggers = [NSMutableArray arrayWithObject:[[AnalyticsKitApsalarP
3. Where significant events occur, call AnalyticsKit logEvent: or other appropriate method. Example:
```obj-c
-[AnalyticsKit logEvent:@"Notifications - Displaying Webview For Notification" withProperties:eventDict];
+[AnalyticsKit logEvent:@"User logged in" withProperties:eventDict];
```
4. You may also want to make AnalyticsKit calls at application lifecycle events, such as applicationDidEnterBackground, applicationWillTerminate, applicationWillEnterForeground

0 comments on commit a3d708d

Please sign in to comment.