Skip to content

Commit

Permalink
Refine GooleAnalyticsProvider
Browse files Browse the repository at this point in the history
1. Implement `didShowNewPageView:withProperties:` correctly
Previous implmentation doesn't contain this method. But screen view
event of Google Analytics has to be precoessed separately.

2. Refine `custom dimension` and `custom metrics` checking/finalizing
  • Loading branch information
sodastsai committed Jun 29, 2015
1 parent 77d6865 commit 473c5fe
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 45 deletions.
4 changes: 4 additions & 0 deletions ARAnalyticalProvider.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@class UINavigationController, UIViewController;

extern NSString *const ARAnalyticalProviderNewPageViewEventName;
extern NSString *const ARAnalyticalProviderNewPageViewEventScreenPropertyKey;

@interface ARAnalyticalProvider : NSObject

/// Init
Expand Down
8 changes: 5 additions & 3 deletions ARAnalyticalProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#import <sys/stat.h>

static NSString *const ARTimingEventLengthKey = @"length";
NSString *const ARAnalyticalProviderNewPageViewEventName = @"Screen view";
NSString *const ARAnalyticalProviderNewPageViewEventScreenPropertyKey = @"screen";

@interface ARAnalyticalProvider () {
aslclient _ASLClient;
Expand Down Expand Up @@ -71,12 +73,12 @@ - (void)didShowNewPageView:(NSString *)pageTitle withProperties:(NSDictionary *)
NSDictionary *props;
if (properties.count > 0) {
NSMutableDictionary *merge = [properties mutableCopy];
merge[@"screen"] = pageTitle;
merge[ARAnalyticalProviderNewPageViewEventScreenPropertyKey] = pageTitle;
props = [merge copy];
} else {
props = @{ @"screen": pageTitle };
props = @{ ARAnalyticalProviderNewPageViewEventScreenPropertyKey: pageTitle };
}
[self event:@"Screen view" withProperties:props];
[self event:ARAnalyticalProviderNewPageViewEventName withProperties:props];
}

- (void)remoteLog:(NSString *)parsedString {}
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ARAnalytics

## Version 3.6.3

* Implement `didShowNewPageView:withProperties:` correctly (Google Analytics) ( @sodastsai )
* Refine `custom dimension` and `custom metrics` checking/finalizing (Google Analytics) ( @sodastsai )

## Version 3.6.2

* Fixed breadcrumb logging in builds with `NS_BLOCK_ASSERTIONS` enabled ( @alloy )
Expand Down
102 changes: 60 additions & 42 deletions Providers/GoogleAnalyticsProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,54 +58,47 @@ - (void)event:(NSString *)event withProperties:(NSDictionary *)properties {
if (!category) {
category = @"default"; // category is a required value
}

#ifdef DEBUG
[self warnAboutIgnoredProperties:properties];
#endif

GAIDictionaryBuilder *builder = [GAIDictionaryBuilder createEventWithCategory:category
action:event
label:properties.label
value:properties.value];

NSMutableDictionary *newProperties = [builder build];

// adding custom Dimension values if we can find a key in the mappings
for (NSString *key in self.customDimensionMappings.allKeys) {
NSString *potentialValue = properties[key];
if (potentialValue) {
[newProperties setObject:potentialValue forKey:self.customDimensionMappings[key]];
}
}

// adding custom Metric values if we can find a key in the mappings
for (NSString *key in self.customMetricMappings.allKeys) {
NSString *potentialValue = properties[key];
if (potentialValue) {
[newProperties setObject:potentialValue forKey:self.customMetricMappings[key]];
}
}

[self.tracker send:newProperties];
[self.tracker send:[self finalizedPropertyDictionaryFromBuilder:[GAIDictionaryBuilder
createEventWithCategory:category
action:event
label:properties.label
value:properties.value]
withProperties:properties]];
}

- (void)didShowNewPageView:(NSString *)pageTitle {
[self event:@"Screen view" withProperties:@{ @"label": pageTitle }];
[self didShowNewPageView:pageTitle withProperties:nil];
}

- (void)didShowNewPageView:(NSString *)pageTitle withProperties:(NSDictionary *)properties {
if (!properties) {
properties = @{@"label": pageTitle};
} else {
NSMutableDictionary *newProperties = [properties mutableCopy];
newProperties[@"label"] = pageTitle;
properties = newProperties;
}

[self event:ARAnalyticalProviderNewPageViewEventName withProperties:properties];

[self.tracker set:kGAIScreenName value:pageTitle];
GAIDictionaryBuilder *builder = nil;
if ([GAIDictionaryBuilder respondsToSelector:@selector(createScreenView)]) {
[self.tracker send:[[GAIDictionaryBuilder createScreenView] build]];
builder = [GAIDictionaryBuilder createScreenView];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[self.tracker send:[[GAIDictionaryBuilder createAppView] build]];
builder = [GAIDictionaryBuilder createAppView];
#pragma clang diagnostic pop
}
[self.tracker send:[self finalizedPropertyDictionaryFromBuilder:builder withProperties:properties]];
}

- (void)logTimingEvent:(NSString *)event withInterval:(NSNumber *)interval properties:(NSDictionary *)properties{
- (void)logTimingEvent:(NSString *)event withInterval:(NSNumber *)interval properties:(NSDictionary *)properties {
// Prepare properties dictionary
if (!properties) {
properties = @{ @"value": @([interval intValue]) };
properties = @{@"value": @([interval intValue])};
} else {
NSMutableDictionary *newProperties = [properties mutableCopy];
newProperties[@"value"] = @([interval intValue]);
Expand All @@ -120,7 +113,34 @@ - (void)logTimingEvent:(NSString *)event withInterval:(NSNumber *)interval prop
interval:@((int)([interval doubleValue]*1000))
name:event
label:nil];
[self.tracker send:[builder build]];
[self.tracker send:[self finalizedPropertyDictionaryFromBuilder:builder withProperties:properties]];
}

- (NSMutableDictionary *)finalizedPropertyDictionaryFromBuilder:(GAIDictionaryBuilder *)builder
withProperties:(NSDictionary *)properties {
NSMutableDictionary *finalizedProperties = [builder build];

#ifdef DEBUG
[self warnAboutIgnoredProperties:properties];
#endif

// adding custom Dimension values if we can find a key in the mappings
for (NSString *key in self.customDimensionMappings.allKeys) {
NSString *potentialValue = properties[key];
if (potentialValue) {
[finalizedProperties setObject:potentialValue forKey:self.customDimensionMappings[key]];
}
}

// adding custom Metric values if we can find a key in the mappings
for (NSString *key in self.customMetricMappings.allKeys) {
NSString *potentialValue = properties[key];
if (potentialValue) {
[finalizedProperties setObject:potentialValue forKey:self.customMetricMappings[key]];
}
}

return finalizedProperties;
}

#pragma mark - Dispatch
Expand All @@ -131,14 +151,12 @@ - (void)dispatchGA {

#pragma mark - Warnings

- (void)warnAboutIgnoredProperties:(NSDictionary*)propertiesDictionary
{
for (id key in propertiesDictionary) {
if ( [key isEqualToString:[NSDictionary googleAnalyticsLabelKey]] ||
[key isEqualToString:[NSDictionary googleAnalyticsCategoryKey]] ||
[key isEqualToString:[NSDictionary googleAnalyticsValueKey]] ||
[self.customDimensionMappings.allKeys containsObject:key]
) {
- (void)warnAboutIgnoredProperties:(NSDictionary*)propertiesDictionary {
for (NSString *key in propertiesDictionary) {
if ([key isEqualToString:[NSDictionary googleAnalyticsLabelKey]] ||
[key isEqualToString:[NSDictionary googleAnalyticsCategoryKey]] ||
[key isEqualToString:[NSDictionary googleAnalyticsValueKey]] ||
[self.customDimensionMappings.allKeys containsObject:key]) {
continue;
}
NSLog(@"%@: property ignored %@:%@",self,key,propertiesDictionary[key]);
Expand Down

0 comments on commit 473c5fe

Please sign in to comment.