Skip to content

Commit

Permalink
Add trackSelfDescribingEvent method as alias for trackUnstructEvent (c…
Browse files Browse the repository at this point in the history
…lose #272)
  • Loading branch information
mhadam committed Feb 16, 2018
1 parent f93f7e2 commit f4d7445
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Snowplow/SPTracker.h
Expand Up @@ -32,6 +32,7 @@
@class SPScreenView;
@class SPTiming;
@class SPEcommerce;
@class SPSelfDescribingJson;

@protocol SPTrackerBuilder <NSObject>

Expand Down Expand Up @@ -120,6 +121,11 @@
*/
- (void) trackUnstructuredEvent:(SPUnstructured *)event;

/**
* Tracks an Unstructured Event object.
*/
- (void) trackSelfDescribingEvent:(SPSelfDescribingJson *)event;

/**
* Tracks a ScreenView Event object.
*/
Expand Down
7 changes: 7 additions & 0 deletions Snowplow/SPTracker.m
Expand Up @@ -214,6 +214,13 @@ - (void) trackUnstructuredEvent:(SPUnstructured *)event {
[self addEventWithPayload:[event getPayloadWithEncoding:_base64Encoded] andContext:[event getContexts] andEventId:[event getEventId]];
}

- (void) trackSelfDescribingEvent:(SPSelfDescribingJson *)event {
SPUnstructured * unstruct = [SPUnstructured build:^(id<SPUnstructuredBuilder> builder) {
[builder setEventData: event];
}];
[self trackUnstructuredEvent:unstruct];
}

- (void) trackScreenViewEvent:(SPScreenView *)event {
SPUnstructured * unstruct = [SPUnstructured build:^(id<SPUnstructuredBuilder> builder) {
[builder setEventData:[event getPayload]];
Expand Down
25 changes: 25 additions & 0 deletions SnowplowTests/TestGeneratedJsons.m
Expand Up @@ -128,6 +128,31 @@ - (void)testUnstructuredEventPayloadJson {
XCTAssertTrue([validator validateJson:unstructDictionary]);
}

- (void)testSelfDescribingEventPayloadJson {
SPTracker * tracker = [self getTracker:@"acme.fake.url"];
NSMutableDictionary * input = [[NSMutableDictionary alloc] init];
[input setObject:[NSNumber numberWithInt:23] forKey:@"level"];
[input setObject:[NSNumber numberWithInt:56473] forKey:@"score"];
SPSelfDescribingJson * sdj = [[SPSelfDescribingJson alloc] initWithSchema:@"iglu:com.acme_company/demo_ios_event/jsonschema/1-0-0"
andData:input];
SPUnstructured * event = [SPUnstructured build:^(id<SPUnstructuredBuilder> builder) {
[builder setEventData: sdj];
}];

// Check that the final payload passes validation
NSDictionary * data = [[tracker getFinalPayloadWithPayload:[event getPayloadWithEncoding:false] andContext:[event getContexts] andEventId:[event getEventId]] getAsDictionary];
NSArray * dataArray = [NSArray arrayWithObject:data];
NSDictionary * json = [[[SPSelfDescribingJson alloc] initWithSchema:kSPPayloadDataSchema andData:dataArray] getAsDictionary];

XCTAssertTrue([validator validateJson:json]);

// Check that the nested unstructured event passes validation
NSString * ue_pr = [data objectForKey:@"ue_pr"];
NSDictionary * unstructDictionary = [NSJSONSerialization JSONObjectWithData:[ue_pr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

XCTAssertTrue([validator validateJson:unstructDictionary]);
}

- (void)testPageViewEventPayloadJson {
SPTracker * tracker = [self getTracker:@"acme.fake.url"];
SPPageView *event = [SPPageView build:^(id<SPPageViewBuilder> builder) {
Expand Down
9 changes: 9 additions & 0 deletions SnowplowTests/TestRequest.m
Expand Up @@ -210,6 +210,15 @@ - (void) trackUnstructuredEventWithTracker:(SPTracker *)tracker_ {
[tracker_ trackUnstructuredEvent:event];
}

- (void) trackSelfDescribingJsonEventWithTracker:(SPTracker *)tracker_ {
NSMutableDictionary * data = [[NSMutableDictionary alloc] init];
[data setObject:[NSNumber numberWithInt:23] forKey:@"level"];
[data setObject:[NSNumber numberWithInt:56473] forKey:@"score"];
SPSelfDescribingJson * sdj = [[SPSelfDescribingJson alloc] initWithSchema:@"iglu:com.acme_company/demo_ios_event/jsonschema/1-0-0"
andData:data];
[tracker_ trackSelfDescribingEvent:sdj];
}

- (void) trackPageViewWithTracker:(SPTracker *)tracker_ {
SPPageView *event = [SPPageView build:^(id<SPPageViewBuilder> builder) {
[builder setPageUrl:@"DemoPageUrl"];
Expand Down

0 comments on commit f4d7445

Please sign in to comment.