diff --git a/SimpleCocoaAnalytics/AnalyticsEvent.m b/SimpleCocoaAnalytics/AnalyticsEvent.m index 02aed0a..877bd71 100644 --- a/SimpleCocoaAnalytics/AnalyticsEvent.m +++ b/SimpleCocoaAnalytics/AnalyticsEvent.m @@ -26,14 +26,19 @@ + (AnalyticsEvent*)analyticsEventWithDictionary:(NSDictionary*)dict { return analyticsEvent; } -- (NSDictionary*)dictionaryRepresenation { - // We expect all of the values to be filled in here. If not, BOOM. - return @{ - kEventCategory:self.category, - kEventActionKey:self.action, - kEventLabelKey:self.label, - kEventValue:self.value - }; +- (NSDictionary*)dictionaryRepresentation { + NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary: + @{ + kEventCategory:self.category, + kEventActionKey:self.action, + }]; + if (self.label != nil) { + result[kEventLabelKey] = self.label; + } + if (self.value != nil) { + result[kEventValue] = self.value; + }; + return result.copy; } @end diff --git a/SimpleCocoaAnalytics/AnalyticsHelper.m b/SimpleCocoaAnalytics/AnalyticsHelper.m index 6adeb86..c521bd5 100644 --- a/SimpleCocoaAnalytics/AnalyticsHelper.m +++ b/SimpleCocoaAnalytics/AnalyticsHelper.m @@ -403,17 +403,22 @@ - (NSData*)createEventPayload:(AnalyticsEvent*)analyticsEvent &ea=play // Event Action. Required. */ - NSString *payloadString = [NSString stringWithFormat:@"v=1&tid=%@&cid=%@&an=%@&t=event&ec=%@&ea=%@&el=%@&ev=%@", - accountIdentifier, // account id - machineIdentifier, // client id - appName, // app name + NSMutableString *string = [NSMutableString stringWithFormat:@"v=1&tid=%@&cid=%@&an=%@", + accountIdentifier, // account id + machineIdentifier, // client id + appName // app name + ]; + [string appendFormat:@"&t=event&ec=%@&ea=%@", analyticsEvent.category, - analyticsEvent.action, - analyticsEvent.label, - analyticsEvent.value - ]; - - payloadString = [payloadString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + analyticsEvent.action]; + + if (analyticsEvent.label != nil) { + [string appendFormat:@"&el=%@", analyticsEvent.label]; + } + if (analyticsEvent.value != nil) { + [string appendFormat:@"&ev=%@", analyticsEvent.value]; + } + NSString *payloadString = [string stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet]; return [payloadString dataUsingEncoding:NSUTF8StringEncoding]; }