TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by kaiinui.
Measurement Events: NSNotification
as Event for Tracking
Post a measurement event.
[AQSEvent postEvent:@"event_name" args:@{
@"key": @"value"
}];
Observe measurement events.
self.observer = [AQSEvent observeWithBlock:^(NSString *eventName, NSDictionary *eventArgs) {
// Do something with arguments.
//
// Typically perform actual tracking events in this block.
}];
A measurement event is an NSNotification
that conforms to following protocol.
NSString
.NSDictionary<NSString, id>
.Testing invocation of [Analytics trackSomething]
in a method is nearly impossible.
Taking an analytics instance as initialization arguments? Nonsense.
iOS posts numerous number of NSNotification
s that notifies App Life Cycle Event and iCloud Event and ...
Also 3rd pirty libraries do so.
However there are too many NSNotification
name and userInfo
format. To use them for tracking events, we have to find which NSNotification
are there, write a lot of NSNotification
observers and parsing and re-formatting userInfo
for each formats.
NSNotification
based - It means you can test it with Expecta
's notify()
NSNotification
whose name is kAQSEvent
. Only one observer. You only need to send the args to your analytics tracking code. (As most of analytics provides tracking an event with name and a dictionary params.)And this is just an NSNotification
. No magicically things. Easy to understand.
AQSEvent
provide a helper to post / subscribe Events.
[AQSEvent postEvent:@"location/changed" args:@{
@"latitude": @(40.712784),
@"longitude": @(-74.005941)
}];
This is a shorthand for following code.
[[NSNotificationCenter defaultCenter] postNotification:kAQSEvent object:nil userInfo:@{
kAQSEventName: @"location/changed",
kAQSEventArgs: @{
@"latitude": @(40.712784),
@"longitude": @(-74.005941)
};
}];
Subscribe to events are also easy.
AQSEvent
provides a helper.
@property (nonatomic, strong) AQSEventObserver *eventObserver;
// Then in @implementation,
self.eventObserver = [AQSEvent observeWithBlock:^(NSString *eventName, NSDictionary *eventArgs) {
// Do something
}];
This is a shorthand for following code.
[[NSNotificationCenter defaultCenter] addObserver:kAQSEvent selector:@selector(subscribeEvent:) object:nil];
AQSEventObserver
automatically does - removeObserver:
on - dealloc
. No more memory leak as long as you use AQSEvent
to observe events.
Assume you want to test that following method posts an Event for tracking.
[someObject doSomething];
XCTestExpectation
helps you testing measurement events. And AQSEvent
provides a helper category for easier testing.
- (void)testItPostsSomeMeasurementEvent {
XCTestExpectation *expectation = [self expectationForNotification:kAQSEvent object:nil handler:BOOL^(NSNotification *notification) {
[expectation fulfill];
return [notification aqs_isEqualToMeasurementEvent:@"event_name" args:@{@"key": @"value"}];
}];
[someObject doSomething]; // Assume it posts "event_name" event with {"key": "value"}
[self waitForExpectationWithTimeout:1.0 handler:nil];
}
pod "AQSEvent"