Skip to content

Commit 696b59f

Browse files
authored
Merge pull request #57 from toddtreece/master
Truncate names to 40 characters
2 parents d9f48ee + ba0b9a3 commit 696b59f

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

Example/Tests/Tests.m

Lines changed: 14 additions & 0 deletions
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:@{

Segment-Firebase/Classes/SEGFirebaseIntegration.m

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,29 @@ - (void)screen:(SEGScreenPayload *)payload
8282

8383
#pragma mark - Utilities
8484

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

89109
// Maps Segment Spec to Firebase Constants
90110
// https://firebase.google.com/docs/reference/ios/firebaseanalytics/api/reference/Constants#/c:FIRParameterNames.h@kFIRParameterCampaign
@@ -108,37 +128,19 @@ - (NSString *)formatFirebaseEventNames:(NSString *)event
108128
kFIREventSearch, @"Products Searched", nil];
109129

110130
NSString *mappedEvent = [mapper objectForKey:event];
111-
NSArray *periodSeparatedEvent = [event componentsSeparatedByString:@"."];
112-
NSString *regexString = @"^[a-zA-Z0-9_]+$";
113-
NSError *error = NULL;
114-
NSRegularExpression *regex =
115-
[NSRegularExpression regularExpressionWithPattern:regexString
116-
options:0
117-
error:&error];
118-
NSUInteger numberOfMatches = [regex numberOfMatchesInString:event
119-
options:0
120-
range:NSMakeRange(0, [event length])];
121131
if (mappedEvent) {
122132
return mappedEvent;
123-
} else if (numberOfMatches == 0) {
124-
NSString *trimmedEvent = [event stringByTrimmingCharactersInSet:
125-
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
126-
if ([periodSeparatedEvent count] > 1) {
127-
return [trimmedEvent stringByReplacingOccurrencesOfString:@"." withString:@"_"];
128-
} else {
129-
return [[trimmedEvent stringByReplacingOccurrencesOfString:@" " withString:@"_"] stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
130-
}
131133
} else {
132-
return event;
134+
return [SEGFirebaseIntegration formatFirebaseNameString:event];
133135
}
134136
}
135137

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

143145
- (NSDictionary *)returnMappedFirebaseParameters:(NSDictionary *)properties
144146
{
@@ -195,14 +197,8 @@ + (NSDictionary *)mapToFirebaseParameters:(NSDictionary *)properties withMap:(NS
195197
NSMutableDictionary *output = [NSMutableDictionary dictionaryWithCapacity:dictionary.count];
196198
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id data, BOOL *stop) {
197199
[output removeObjectForKey:key];
198-
NSArray *periodSeparatedKey = [key componentsSeparatedByString:@"."];
199-
NSString *trimmedKey = [key stringByTrimmingCharactersInSet:
200-
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
201-
if ([periodSeparatedKey count] > 1) {
202-
key = [trimmedKey stringByReplacingOccurrencesOfString:@"." withString:@"_"];
203-
} else {
204-
key = [[trimmedKey stringByReplacingOccurrencesOfString:@" " withString:@"_"] stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
205-
}
200+
key = [SEGFirebaseIntegration formatFirebaseNameString:key];
201+
206202
if ([data isKindOfClass:[NSNumber class]]) {
207203
data = [NSNumber numberWithDouble:[data doubleValue]];
208204
[output setObject:data forKey:key];

0 commit comments

Comments
 (0)