Skip to content

Commit

Permalink
Merge pull request #383 from segmentio/ga-tests
Browse files Browse the repository at this point in the history
GA tests
  • Loading branch information
f2prateek committed Jul 27, 2015
2 parents 87e3b30 + f64e963 commit e97bfd2
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 42 deletions.
12 changes: 12 additions & 0 deletions Analytics.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
32F6381C1A8134A600C65643 /* SEGKahunaIntegration.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F6381A1A8134A600C65643 /* SEGKahunaIntegration.m */; };
32F6381D1A8134A600C65643 /* SEGKahunaIntegration.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F6381A1A8134A600C65643 /* SEGKahunaIntegration.m */; };
6182B863DB2D44D1B463AE3B /* libPods-iOS Tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 24AE4A015CBF4E95B18D10A7 /* libPods-iOS Tests.a */; };
6E0570C41B63038F0072B471 /* SEGGoogleAnalyticsIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E0570C31B63038F0072B471 /* SEGGoogleAnalyticsIntegrationTests.m */; };
6E12DD831B3CA2A1005E35D0 /* SEGCrittercismIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E12DD821B3CA2A1005E35D0 /* SEGCrittercismIntegrationTests.m */; };
6E165A121B54771A002C1C40 /* GAI.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E165A081B54771A002C1C40 /* GAI.h */; settings = {ATTRIBUTES = (Public, ); }; };
6E165A131B54771A002C1C40 /* GAIDictionaryBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 6E165A091B54771A002C1C40 /* GAIDictionaryBuilder.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -517,6 +518,7 @@
32F9E6B31907300300ED295B /* libPods-AnalyticsTests.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libPods-AnalyticsTests.a"; path = "Pods/build/Debug-iphoneos/libPods-AnalyticsTests.a"; sourceTree = "<group>"; };
32F9E6B61907320700ED295B /* libPods-AnalyticsTests.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libPods-AnalyticsTests.a"; path = "Pods/build/Debug-iphoneos/libPods-AnalyticsTests.a"; sourceTree = "<group>"; };
32F9E6B91907323900ED295B /* libPods-Analytics.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libPods-Analytics.a"; path = "Pods/build/Debug-iphoneos/libPods-Analytics.a"; sourceTree = "<group>"; };
6E0570C31B63038F0072B471 /* SEGGoogleAnalyticsIntegrationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SEGGoogleAnalyticsIntegrationTests.m; path = Integrations/GoogleAnalytics/SEGGoogleAnalyticsIntegrationTests.m; sourceTree = "<group>"; };
6E12DD821B3CA2A1005E35D0 /* SEGCrittercismIntegrationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SEGCrittercismIntegrationTests.m; path = Integrations/Crittercism/SEGCrittercismIntegrationTests.m; sourceTree = "<group>"; };
6E165A081B54771A002C1C40 /* GAI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = GAI.h; path = Pods/GoogleAnalytics/Headers/Public/GAI.h; sourceTree = "<group>"; };
6E165A091B54771A002C1C40 /* GAIDictionaryBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = GAIDictionaryBuilder.h; path = Pods/GoogleAnalytics/Headers/Public/GAIDictionaryBuilder.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -772,6 +774,14 @@
name = Pods;
sourceTree = "<group>";
};
6E0570C21B63037A0072B471 /* GoogleAnalytics */ = {
isa = PBXGroup;
children = (
6E0570C31B63038F0072B471 /* SEGGoogleAnalyticsIntegrationTests.m */,
);
name = GoogleAnalytics;
sourceTree = "<group>";
};
6E12DD811B3CA285005E35D0 /* Crittercism */ = {
isa = PBXGroup;
children = (
Expand All @@ -790,6 +800,7 @@
6EFF0CE31B3B63E6005190A2 /* Countly */,
6E12DD811B3CA285005E35D0 /* Crittercism */,
6E16FFF41B33AD0B00B4F7FE /* Flurry */,
6E0570C21B63037A0072B471 /* GoogleAnalytics */,
6E92C1E51B3DD5030049B5A8 /* Localytics */,
6ED462241B42D5E70042BFE4 /* Mixpanel */,
6E8F058A1B4231FE00305E99 /* Optimizely */,
Expand Down Expand Up @@ -1818,6 +1829,7 @@
6E8F058C1B42321800305E99 /* SEGOptimizelyIntegrationTests.m in Sources */,
6EFF0CE51B3B63FB005190A2 /* SEGCountlyIntegrationTests.m in Sources */,
3242C9AB1946AB4700FBE441 /* AnalyticsUtilsTests.m in Sources */,
6E0570C41B63038F0072B471 /* SEGGoogleAnalyticsIntegrationTests.m in Sources */,
6E9F1EE71B605E7400E55D88 /* SEGApptimizeIntegrationTests.m in Sources */,
3242C9AC1946AB4700FBE441 /* SegmentioTests.m in Sources */,
3242C9BF1946AFF900FBE441 /* SEGTapstreamIntegration.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
#import <Foundation/Foundation.h>
#import "SEGAnalyticsIntegration.h"
#import "SEGEcommerce.h"
#import <GoogleAnalytics/GAI.h>
#import <GoogleAnalytics/GAIDictionaryBuilder.h>
#import <GoogleAnalytics/GAIFields.h>


@interface SEGGoogleAnalyticsIntegration : SEGAnalyticsIntegration <SEGEcommerce>

@property id<GAITracker> tracker;
@property GAI *gai;
@property (nonatomic, copy) NSDictionary *traits;

- (void)resetTraits;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@
// Copyright (c) 2014 Segment.io. All rights reserved.


#import <GoogleAnalytics/GAIDictionaryBuilder.h>
#import <GoogleAnalytics/GAIFields.h>
#import <GoogleAnalytics/GAI.h>
#import "SEGAnalyticsUtils.h"
#import "SEGAnalytics.h"
#import "SEGGoogleAnalyticsIntegration.h"


@interface SEGGoogleAnalyticsIntegration ()

@property (nonatomic, copy) NSDictionary *traits;

@end


@implementation SEGGoogleAnalyticsIntegration

#pragma mark - Initialization
Expand All @@ -32,6 +22,7 @@ - (id)init
self.name = @"Google Analytics";
self.valid = NO;
self.initialized = NO;
self.gai = [GAI sharedInstance];
}
return self;
}
Expand All @@ -48,22 +39,21 @@ - (void)start
}
// Require setup with the trackingId.
NSString *trackingId = [self.settings objectForKey:@"mobileTrackingId"];
[[GAI sharedInstance] setDefaultTracker:[[GAI sharedInstance] trackerWithTrackingId:trackingId]];
_tracker = [_gai trackerWithTrackingId:trackingId];
[_gai setDefaultTracker:_tracker];

// Optionally turn on uncaught exception tracking.
NSString *reportUncaughtExceptions = [self.settings objectForKey:@"reportUncaughtExceptions"];
if ([reportUncaughtExceptions boolValue]) {
[GAI sharedInstance].trackUncaughtExceptions = YES;
[_gai setTrackUncaughtExceptions:YES];
}

// Optionally turn on GA remarketing features
NSString *demographicReports = [self.settings objectForKey:@"doubleClick"];
if ([demographicReports boolValue]) {
[[[GAI sharedInstance] defaultTracker] setAllowIDFACollection:YES];
[_tracker setAllowIDFACollection:YES];
}

// TODO: add support for sample rate

// All done!
SEGLog(@"GoogleAnalyticsIntegration initialized.");
[super start];
Expand All @@ -88,14 +78,15 @@ - (void)identify:(NSString *)userId traits:(NSDictionary *)traits options:(NSDic
[self resetTraits];

// Optionally send the userId if they have that enabled
if ([self shouldSendUserId])
[[[GAI sharedInstance] defaultTracker] set:@"&uid" value:userId];
if ([self shouldSendUserId]) {
[_tracker set:@"&uid" value:userId];
}

// We can set traits though. Iterate over a ll the traits and set them.
self.traits = traits;

[self.traits enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[[[GAI sharedInstance] defaultTracker] set:key value:obj];
[_tracker set:key value:obj];
}];
}

Expand Down Expand Up @@ -124,17 +115,17 @@ - (void)track:(NSString *)event properties:(NSDictionary *)properties options:(N
SEGLog(@"Sending to Google Analytics: category %@, action %@, label %@, value %@", category, event, label, value);

// Track the event!
[[[GAI sharedInstance] defaultTracker] send:
[[GAIDictionaryBuilder createEventWithCategory:category
action:event
label:label
value:value] build]];
[_tracker send:
[[GAIDictionaryBuilder createEventWithCategory:category
action:event
label:label
value:value] build]];
}

- (void)screen:(NSString *)screenTitle properties:(NSDictionary *)properties options:(NSDictionary *)options
{
[[[GAI sharedInstance] defaultTracker] set:kGAIScreenName value:screenTitle];
[[[GAI sharedInstance] defaultTracker] send:[[GAIDictionaryBuilder createScreenView] build]];
[_tracker set:kGAIScreenName value:screenTitle];
[_tracker send:[[GAIDictionaryBuilder createScreenView] build]];
}

#pragma mark - Ecommerce
Expand All @@ -146,35 +137,35 @@ - (void)completedOrder:(NSDictionary *)properties

SEGLog(@"Tracking completed order to Google Analytics with properties: %@", properties);

[[[GAI sharedInstance] defaultTracker] send:[[GAIDictionaryBuilder createTransactionWithId:orderId
affiliation:properties[@"affiliation"]
revenue:[self.class extractRevenue:properties]
tax:properties[@"tax"]
shipping:properties[@"shipping"]
currencyCode:currency] build]];

[[[GAI sharedInstance] defaultTracker] send:[[GAIDictionaryBuilder createItemWithTransactionId:orderId
name:properties[@"name"]
sku:properties[@"sku"]
category:properties[@"category"]
price:properties[@"price"]
quantity:properties[@"quantity"]
currencyCode:currency] build]];
[_tracker send:[[GAIDictionaryBuilder createTransactionWithId:orderId
affiliation:properties[@"affiliation"]
revenue:[self.class extractRevenue:properties]
tax:properties[@"tax"]
shipping:properties[@"shipping"]
currencyCode:currency] build]];

[_tracker send:[[GAIDictionaryBuilder createItemWithTransactionId:orderId
name:properties[@"name"]
sku:properties[@"sku"]
category:properties[@"category"]
price:properties[@"price"]
quantity:properties[@"quantity"]
currencyCode:currency] build]];
}

- (void)reset
{
[super reset];

[[[GAI sharedInstance] defaultTracker] set:@"&uid" value:nil];
[_tracker set:@"&uid" value:nil];

[self resetTraits];
}


- (void)flush
{
[[GAI sharedInstance] dispatch];
[_gai dispatch];
}

#pragma mark - Private
Expand All @@ -187,7 +178,7 @@ - (BOOL)shouldSendUserId
- (void)resetTraits
{
[self.traits enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[[[GAI sharedInstance] defaultTracker] set:key value:nil];
[_tracker set:key value:nil];
}];
self.traits = nil;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// SEGGoogleAnalyticsIntegrationTests.m
// Analytics
//
// Created by Prateek Srivastava on 2015-07-24.
// Copyright (c) 2015 Segment.io. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "SEGGoogleAnalyticsIntegration.h"


#define HC_SHORTHAND
#import <OCHamcrest/OCHamcrest.h>

#define MOCKITO_SHORTHAND
#import <OCMockito/OCMockito.h>


@interface SEGGoogleAnalyticsIntegrationTests : XCTestCase

@property SEGGoogleAnalyticsIntegration *integration;
@property GAI *gaiMock;
@property id<GAITracker> trackerMock;

@end


@implementation SEGGoogleAnalyticsIntegrationTests

- (void)setUp
{
[super setUp];

_trackerMock = mockProtocol(@protocol(GAITracker));
_gaiMock = mock([GAI class]);
[given([_gaiMock trackerWithTrackingId:anything()]) willReturn:_trackerMock];

_integration = [[SEGGoogleAnalyticsIntegration alloc] init];
[_integration setGai:_gaiMock];
[_integration setTracker:_trackerMock];
}

- (void)testFlush
{
[_integration flush];

[verifyCount(_gaiMock, times(1)) dispatch];
}

- (void)testResetTraits
{
[_integration setTraits:@{
@"foo" : @"bar",
@"qaz" : @"qux"
}];
[_integration resetTraits];

[verifyCount(_trackerMock, times(1)) set:@"foo" value:nil];
[verifyCount(_trackerMock, times(1)) set:@"qaz" value:nil];
}

- (void)testReset
{
[_integration setTraits:@{
@"foo" : @"bar",
@"qaz" : @"qux"
}];
[_integration reset];

[verifyCount(_trackerMock, times(1)) set:@"&uid" value:nil];
[verifyCount(_trackerMock, times(1)) set:@"foo" value:nil];
[verifyCount(_trackerMock, times(1)) set:@"qaz" value:nil];
}

- (void)testScreen
{
[_integration screen:@"foo" properties:nil options:nil];

[verifyCount(_trackerMock, times(1)) set:kGAIScreenName value:@"foo"];
[verifyCount(_trackerMock, times(1)) send:[[GAIDictionaryBuilder createScreenView] build]];
}

- (void)testIdentify
{
[_integration setSettings:@{ @"sendUserId" : @YES }];
[_integration setTraits:@{
@"foo" : @"bar",
@"qaz" : @"qux"
}];

[_integration identify:@"foo" traits:@{ @"foobar" : @"qazqux" } options:nil];

[verifyCount(_trackerMock, times(1)) set:@"foo" value:nil];
[verifyCount(_trackerMock, times(1)) set:@"qaz" value:nil];
[verifyCount(_trackerMock, times(1)) set:@"&uid" value:@"foo"];
[verifyCount(_trackerMock, times(1)) set:@"foobar" value:@"qazqux"];
}

- (void)testTrack
{
[_integration track:@"foo"
properties:@{ @"label" : @"bar",
@"revenue" : @10 }
options:nil];

[verifyCount(_trackerMock, times(1)) send:
[[GAIDictionaryBuilder createEventWithCategory:@"All"
action:@"foo"
label:@"bar"
value:@10] build]];
}

@end

0 comments on commit e97bfd2

Please sign in to comment.