Skip to content

Commit 1c273f6

Browse files
OllaAshourbriemcnally
authored andcommitted
DAND-61-Add support for groups (#47)
* DAND-61-Update Mixpanel, Analytics versions, use older version of OCMOckito to avoid pod dependency issue. * DAND-61- Add support for groups with and without names and setOnce traits * DAND-61-Update tests to include groups * Add support for example usage of analytics, and mixpanel usage, with identify and use group in app delegate of example project. * DAND-61-Update changelod and update podspec version for release * DAND-61- Remove specific write key and add more group properties in tests * DAND-61-Add groupIdentifierTraits settings check and handling. Updated tests accordingly. * DAND-61- Remove check on group name, and set group with identifier value. * DAND-61-Update test to verify groupIdentifierTraits * DAND-61-Revert the version update in CHANGELOG and podspec
1 parent f0dba70 commit 1c273f6

File tree

8 files changed

+147
-22
lines changed

8 files changed

+147
-22
lines changed

Example/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ end
1010
target 'Segment-Mixpanel_Tests' do
1111
pod "Segment-Mixpanel", :path => "../"
1212

13-
pod 'OCMockito'
13+
pod 'OCMockito', '~> 3.0'
1414
pod 'Specta'
1515
pod 'Expecta'
1616
end

Example/Podfile.lock

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
PODS:
2-
- Analytics (3.2.6)
3-
- Expecta (1.0.3)
4-
- Mixpanel (3.4.9)
5-
- OCHamcrest (5.2.0)
6-
- OCMockito (3.0.1):
2+
- Analytics (3.7.0)
3+
- Expecta (1.0.6)
4+
- Mixpanel (3.5.0)
5+
- OCHamcrest (5.4.0)
6+
- OCMockito (3.0.2):
77
- OCHamcrest (~> 5.1)
8-
- Segment-Mixpanel (1.3.0):
8+
- Segment-Mixpanel (1.4.0):
99
- Analytics (~> 3.0)
10-
- Mixpanel (~> 3.4.7)
10+
- Mixpanel (~> 3.5)
1111
- Specta (1.0.7)
1212

1313
DEPENDENCIES:
1414
- Expecta
15-
- OCMockito
15+
- OCMockito (~> 3.0)
1616
- Segment-Mixpanel (from `../`)
1717
- Specta
1818

1919
SPEC REPOS:
20-
https://github.com/cocoapods/specs.git:
20+
https://github.com/CocoaPods/Specs.git:
2121
- Analytics
2222
- Expecta
2323
- Mixpanel
@@ -30,14 +30,14 @@ EXTERNAL SOURCES:
3030
:path: "../"
3131

3232
SPEC CHECKSUMS:
33-
Analytics: ad1c5bdc169866d72afdbc918084d0695010e35a
34-
Expecta: 9d1bff6c8b0eeee73a166a2ee898892478927a15
35-
Mixpanel: 5aca6e506b34650b96dc759a208513d86650fac7
36-
OCHamcrest: 218dc4022a5e928a275771da70c5dbea9f859287
37-
OCMockito: 9eee3c61e42f7cb8aa015d66f8cc16849701d973
38-
Segment-Mixpanel: 4ff970648d9c49ee0a62a32eec777346316ee7ea
33+
Analytics: 77fd5fb102a4a5eedafa2c2b0245ceb7b7c15e45
34+
Expecta: 3b6bd90a64b9a1dcb0b70aa0e10a7f8f631667d5
35+
Mixpanel: 399997670f5452de026449f9e9ecd52eccbb9ff7
36+
OCHamcrest: 5c1d441c5a82fb18ac17c2aeb52ec1a99edb971b
37+
OCMockito: 2cb6c2267cbc76537be56281cd47cdb5a7c27541
38+
Segment-Mixpanel: 625bb010c65f66acdd976d9db240a420aa67b132
3939
Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66
4040

41-
PODFILE CHECKSUM: 507a152874203f544f8656b6532dd0f45dd3c57d
41+
PODFILE CHECKSUM: 6ff827a80dc2a21042246602306af45cb1c9f2a8
4242

43-
COCOAPODS: 1.7.5
43+
COCOAPODS: 1.8.4

Example/Segment-Mixpanel/SEGAppDelegate.m

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,30 @@
77
//
88

99
#import "SEGAppDelegate.h"
10-
10+
#import <Analytics/SEGAnalytics.h>
11+
#import <Segment-Mixpanel/SEGMixpanelIntegrationFactory.h>
1112

1213
@implementation SEGAppDelegate
1314

1415
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1516
{
1617
// Override point for customization after application launch.
18+
19+
// Override point for customization after application launch.
20+
[SEGAnalytics debug:YES];
21+
SEGAnalyticsConfiguration *config = [SEGAnalyticsConfiguration configurationWithWriteKey:@"YOUR_WRITE_KEY_HERE"];
22+
23+
// Add any of your bundled integrations.
24+
[config use:[SEGMixpanelIntegrationFactory instance]];
25+
26+
[SEGAnalytics setupWithConfiguration:config];
27+
28+
[[SEGAnalytics sharedAnalytics] identify:@"segment-fake-tester-Mixpanel"
29+
traits:@{ @"email": @"[email protected]" }];
30+
31+
[[SEGAnalytics sharedAnalytics] group:@"testversion1" traits:@{ @"name": @"test group"}];
32+
33+
1734
return YES;
1835
}
1936

Example/Tests/SEGPayloadBuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@
2323

2424
+ (SEGAliasPayload *)alias:(NSString *)newId;
2525

26+
+ (SEGGroupPayload *)group:(NSString *) groupId withTraits:(NSDictionary *)traits;
27+
2628
@end

Example/Tests/SEGPayloadBuilder.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ + (SEGAliasPayload *)alias:(NSString *)newId
4141
return [[SEGAliasPayload alloc] initWithNewId:newId context:@{} integrations:@{}];
4242
}
4343

44+
+ (SEGGroupPayload *)group:(NSString *)groupId withTraits:(NSDictionary *)traits
45+
{
46+
return [[SEGGroupPayload alloc] initWithGroupId:groupId traits:traits context:@{} integrations:@{}];
47+
}
48+
4449
@end

Example/Tests/Tests.m

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@
99
// https://github.com/Specta/Specta
1010

1111
#import "SEGPayloadBuilder.h"
12+
#import <MixpanelGroup.h>
1213

1314
SpecBegin(InitialSpecs)
1415

1516
describe(@"Mixpanel Integration", ^{
1617
__block SEGMixpanelIntegration *integration;
1718
__block Mixpanel *mixpanel;
1819
__block MixpanelPeople *mixpanelPeople;
20+
__block MixpanelGroup *mixpanelGroup;
1921

2022
beforeEach(^{
2123
mixpanel = mock([Mixpanel class]);
2224
mixpanelPeople = mock([MixpanelPeople class]);
2325
[given([mixpanel people]) willReturn:mixpanelPeople];
24-
26+
27+
mixpanelGroup = mock([MixpanelGroup class]);
28+
2529
integration = [[SEGMixpanelIntegration alloc] initWithSettings:@{
2630
@"trackAllPages" : @1,
27-
@"setAllTraitsByDefault" : @1
31+
@"setAllTraitsByDefault" : @1,
32+
@"groupIdentifierTraits" : @[@"group_id", @"group_name", @"group_idtest2"]
2833
} andMixpanel:mixpanel];
2934
});
3035

@@ -118,7 +123,77 @@
118123
@"age" : @24
119124
}];
120125
});
126+
127+
128+
it(@"simple group", ^{
129+
NSDictionary *groupTraits = @{
130+
@"groupCity" : @"Cairo",
131+
@"group_name" : @"mixPanelTest1Group"
132+
};
133+
[integration group:[SEGPayloadBuilder group:@"groupTest1" withTraits:groupTraits]];
134+
135+
NSDictionary *groupIdentifierTraits = integration.settings[@"groupIdentifierTraits"];
136+
137+
for (NSString *identifierTrait in groupIdentifierTraits) {
138+
NSString *groupIdentifierVal = groupTraits[identifierTrait];
139+
if (groupIdentifierVal) {
140+
[verify(mixpanel) getGroup:groupIdentifierVal groupID:@"groupTest1"];
141+
}
142+
143+
}
144+
145+
});
146+
147+
it(@"complex group", ^{
148+
NSDictionary *groupTraits = @{
149+
@"groupCity" : @"Alexandria",
150+
@"name" : @"mixPanelTest1Group",
151+
@"address": @{@"city": @"Alexandria", @"postalCode": @"22314", @"state":@"Virginia", @"street": @"105 N Union St" },
152+
@"description": @"Art Center",
153+
@"avatar" : @"https://gravatar.com/avatar/f8b72def445675a558fe68b1cb651da1?s=400&d=robohash&r=x"
154+
};
155+
[integration group:[SEGPayloadBuilder group:@"groupTest1" withTraits:groupTraits]];
156+
157+
NSDictionary *groupIdentifierTraits = integration.settings[@"groupIdentifierTraits"];
158+
159+
for (NSString *identifierTrait in groupIdentifierTraits) {
160+
NSString *groupIdentifierVal = groupTraits[identifierTrait];
161+
if (groupIdentifierVal) {
162+
[verify(mixpanel) getGroup:groupIdentifierVal groupID:@"groupTest1"];
163+
}
164+
165+
}
166+
167+
});
121168

169+
it(@"simple group without name", ^{
170+
NSDictionary *groupTraits = @{
171+
@"groupCity" : @"Cairo"
172+
};
173+
[integration group:[SEGPayloadBuilder group:@"groupTest2" withTraits:groupTraits]];
174+
175+
NSDictionary *groupIdentifierTraits = integration.settings[@"groupIdentifierTraits"];
176+
177+
for (NSString *identifierTrait in groupIdentifierTraits) {
178+
NSString *groupIdentifierVal = groupTraits[identifierTrait];
179+
[given([mixpanel getGroup:groupIdentifierVal groupID:@"groupTest2"]) willReturn:nil];
180+
181+
}
182+
});
183+
184+
it(@"simple group setOnce traits", ^{
185+
[given([mixpanel getGroup:@"test1" groupID:@"groupTest2"]) willReturn:mixpanelGroup];
186+
NSDictionary *groupTraits = @{
187+
@"groupCity" : @"Cairo",
188+
@"groupCount" : @"20",
189+
@"group_id" : @"test1"
190+
};
191+
[integration group:[SEGPayloadBuilder group:@"groupTest2" withTraits:groupTraits]];
192+
193+
[verify(mixpanelGroup) setOnce:groupTraits];
194+
[verify(mixpanel) getGroup:@"test1" groupID:@"groupTest2"];
195+
});
196+
122197
it(@"alias", ^{
123198
[given([mixpanel distinctId]) willReturn:@"123456"];
124199
[integration alias:[SEGPayloadBuilder alias:@"prateek"]];

Pod/Classes/SEGMixpanelIntegration.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "SEGMixpanelIntegration.h"
22
#import <Analytics/SEGAnalyticsUtils.h>
3+
#import <MixpanelGroup.h>
34

45

56
@implementation SEGMixpanelIntegration
@@ -139,6 +140,31 @@ - (void)screen:(SEGScreenPayload *)payload
139140
}
140141
}
141142

143+
144+
- (void) group:(SEGGroupPayload *)payload {
145+
146+
NSString *groupID = payload.groupId;
147+
NSDictionary *traits = [payload traits];
148+
149+
NSArray *groupIdentifierProperties = [self.settings objectForKey:@"groupIdentifierTraits"];
150+
151+
if(!groupID || groupID.length == 0 || groupIdentifierProperties.count == 0) {
152+
return;
153+
}
154+
155+
if(traits != nil || traits.count != 0){
156+
for (NSString *groupIdentiferProperty in groupIdentifierProperties) {
157+
[[self.mixpanel getGroup:traits[groupIdentiferProperty] groupID:groupID ] setOnce:traits];
158+
}
159+
}
160+
161+
for (NSString *groupIdentiferProperty in groupIdentifierProperties) {
162+
[self.mixpanel setGroup:traits[groupIdentiferProperty] groupID:groupID];
163+
SEGLog(@"[Mixpanel setGroup:%@ groupID:%@]", groupIdentiferProperty, groupID);
164+
}
165+
166+
}
167+
142168
+ (NSNumber *)extractRevenue:(NSDictionary *)dictionary withKey:(NSString *)revenueKey
143169
{
144170
id revenueProperty = nil;

Segment-Mixpanel.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ Pod::Spec.new do |s|
2222
s.source_files = 'Pod/Classes/**/*'
2323

2424
s.dependency 'Analytics', '~> 3.0'
25-
s.dependency 'Mixpanel', '~> 3.4.7'
25+
s.dependency 'Mixpanel', '~> 3.5'
2626
end

0 commit comments

Comments
 (0)