Skip to content

Commit 2463614

Browse files
author
Todd Treece
committed
1 parent 9be7abf commit 2463614

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

Example/Tests/Tests.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@
6666
}];
6767
});
6868

69+
it(@"track with event name and prop name > 40 chars", ^{
70+
SEGTrackPayload *payload = [[SEGTrackPayload alloc] initWithEvent:@"ufddxsblvtujywadkmohvddqnpbginxozqjffhjyy"
71+
properties:@{
72+
@"ufddxsblvtujywadkmohvddqnpbginxozqjffhjyy" : @"Death Star"
73+
}
74+
context:@{}
75+
integrations:@{}];
76+
77+
[integration track:payload];
78+
[verify(mockFirebase) logEventWithName:@"ufddxsblvtujywadkmohvddqnpbginxozqjffhjy" parameters:@{
79+
@"ufddxsblvtujywadkmohvddqnpbginxozqjffhjy" : @"Death Star"
80+
}];
81+
});
82+
6983
it(@"track with event name and parmas separated by periods", ^{
7084
SEGTrackPayload *payload = [[SEGTrackPayload alloc] initWithEvent:@"Starship.Ordered"
7185
properties:@{
@@ -575,7 +589,7 @@
575589
context:@{}
576590
integrations:@{}];
577591
[integration screen:payload];
578-
[verify(mockFirebase) setScreenName:@"Home screen" screenClass:nil];
592+
[verify(mockFirebase) setScreenName:@"Home_screen" screenClass:nil];
579593
});
580594

581595
});

Segment-Firebase/Classes/SEGFirebaseIntegration.m

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,38 @@ - (void)track:(SEGTrackPayload *)payload
6666

6767
- (void)screen:(SEGScreenPayload *)payload
6868
{
69-
[self.firebaseClass setScreenName:payload.name screenClass:nil];
69+
NSString *name = [SEGFirebaseIntegration formatFirebaseNameString:payload.name];
70+
71+
[self.firebaseClass setScreenName:name screenClass:nil];
7072
SEGLog(@"[FIRAnalytics setScreenName:%@]", payload.name);
7173
}
7274

7375

7476
#pragma mark - Utilities
7577

76-
// Event names can be up to 32 characters long, may only contain alphanumeric
77-
// characters and underscores ("_"), and must start with an alphabetic character. The "firebase_"
78-
// prefix is reserved and should not be used.
78+
// Formats the following types of strings to match the Firebase requirements:
79+
//
80+
// Event Names: https://firebase.google.com/docs/reference/ios/firebaseanalytics/api/reference/Classes/FIRAnalytics#+logeventwithname:parameters:
81+
// Should contain 1 to 40 alphanumeric characters or underscores.
82+
//
83+
// Parameter Names: https://firebase.google.com/docs/reference/ios/firebaseanalytics/api/reference/Classes/FIRAnalytics#/c:objc(cs)FIRAnalytics(cm)logEventWithName:parameters:
84+
// Should contain 1 to 40 alphanumeric characters or underscores.
85+
//
86+
// Screen Names: https://firebase.google.com/docs/reference/ios/firebaseanalytics/api/reference/Classes/FIRAnalytics#setscreennamescreenclass
87+
// Should contain 1 to 40 alphanumeric characters or underscores.
88+
89+
+ (NSString *)formatFirebaseNameString:(NSString *)name
90+
{
91+
NSError *error = nil;
92+
93+
NSString *trimmed = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
94+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([^a-zA-Z0-9_])" options:0 error:&error];
95+
NSString *formatted = [regex stringByReplacingMatchesInString:trimmed options:0 range:NSMakeRange(0, [trimmed length]) withTemplate:@"_"];
96+
97+
NSLog(@"Output: %@", formatted);
98+
return [formatted substringToIndex:MIN(40, [formatted length])];
99+
}
100+
79101

80102
// Maps Segment Spec to Firebase Constants
81103
// https://firebase.google.com/docs/reference/ios/firebaseanalytics/api/reference/Constants#/c:FIRParameterNames.h@kFIRParameterCampaign
@@ -99,37 +121,19 @@ - (NSString *)formatFirebaseEventNames:(NSString *)event
99121
kFIREventSearch, @"Products Searched", nil];
100122

101123
NSString *mappedEvent = [mapper objectForKey:event];
102-
NSArray *periodSeparatedEvent = [event componentsSeparatedByString:@"."];
103-
NSString *regexString = @"^[a-zA-Z0-9_]+$";
104-
NSError *error = NULL;
105-
NSRegularExpression *regex =
106-
[NSRegularExpression regularExpressionWithPattern:regexString
107-
options:0
108-
error:&error];
109-
NSUInteger numberOfMatches = [regex numberOfMatchesInString:event
110-
options:0
111-
range:NSMakeRange(0, [event length])];
112124
if (mappedEvent) {
113125
return mappedEvent;
114-
} else if (numberOfMatches == 0) {
115-
NSString *trimmedEvent = [event stringByTrimmingCharactersInSet:
116-
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
117-
if ([periodSeparatedEvent count] > 1) {
118-
return [trimmedEvent stringByReplacingOccurrencesOfString:@"." withString:@"_"];
119-
} else {
120-
return [[trimmedEvent stringByReplacingOccurrencesOfString:@" " withString:@"_"] stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
121-
}
122126
} else {
123-
return event;
127+
return [SEGFirebaseIntegration formatFirebaseNameString:event];
124128
}
125129
}
126130

127131
/// Params supply information that contextualize Events. You can associate up to 25 unique Params
128132
/// with each Event type. Some Params are suggested below for certain common Events, but you are
129133
/// not limited to these. You may supply extra Params for suggested Events or custom Params for
130-
/// Custom events. Param names can be up to 24 characters long, may only contain alphanumeric
134+
/// Custom events. Param names can be up to 40 characters long, may only contain alphanumeric
131135
/// characters and underscores ("_"), and must start with an alphabetic character. Param values can
132-
/// be up to 36 characters long. The "firebase_" prefix is reserved and should not be used.
136+
/// be up to 100 characters long. The "firebase_" prefix is reserved and should not be used.
133137

134138
- (NSDictionary *)returnMappedFirebaseParameters:(NSDictionary *)properties
135139
{
@@ -170,14 +174,8 @@ + (NSDictionary *)mapToFirebaseParameters:(NSDictionary *)properties withMap:(NS
170174
NSMutableDictionary *output = [NSMutableDictionary dictionaryWithCapacity:dictionary.count];
171175
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id data, BOOL *stop) {
172176
[output removeObjectForKey:key];
173-
NSArray *periodSeparatedKey = [key componentsSeparatedByString:@"."];
174-
NSString *trimmedKey = [key stringByTrimmingCharactersInSet:
175-
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
176-
if ([periodSeparatedKey count] > 1) {
177-
key = [trimmedKey stringByReplacingOccurrencesOfString:@"." withString:@"_"];
178-
} else {
179-
key = [[trimmedKey stringByReplacingOccurrencesOfString:@" " withString:@"_"] stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
180-
}
177+
key = [SEGFirebaseIntegration formatFirebaseNameString:key];
178+
181179
if ([data isKindOfClass:[NSNumber class]]) {
182180
data = [NSNumber numberWithDouble:[data doubleValue]];
183181
[output setObject:data forKey:key];

0 commit comments

Comments
 (0)