Skip to content

Commit

Permalink
Added JS and native bridge functions to support custom metrics (akson…
Browse files Browse the repository at this point in the history
  • Loading branch information
mmiani authored and cbrevik committed Sep 9, 2017
1 parent 39225ff commit b009a27
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ public void trackScreenViewWithCustomDimensionValues(String trackerId, String sc

@ReactMethod
public void trackEventWithCustomDimensionValues(String trackerId, String category, String action, ReadableMap optionalValues, ReadableMap dimensionIndexValues)
{
this.trackEventWithCustomDimensionAndMetricValues(trackerId, category, action, optionalValues, dimensionIndexValues, null);
}

@ReactMethod
public void trackEventWithCustomDimensionAndMetricValues(String trackerId, String category, String action, ReadableMap optionalValues, ReadableMap dimensionIndexValues, ReadableMap metricIndexValues)
{
Tracker tracker = getTracker(trackerId);

Expand Down Expand Up @@ -353,6 +359,15 @@ public void trackEventWithCustomDimensionValues(String trackerId, String categor
hit.setCustomDimension(Integer.parseInt(dimensionIndex), dimensionValue);
}

if(metricIndexValues != null){
ReadableMapKeySetIterator metricIterator = metricIndexValues.keySetIterator();
while (metricIterator.hasNextKey()) {
String metricIndex = metricIterator.nextKey();
int metricValue = metricIndexValues.getInt(metricIndex);
hit.setCustomMetric(Integer.parseInt(metricIndex), metricValue);
}
}

tracker.send(hit.build());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <React/RCTBridgeModule.h>

@interface RCTGoogleAnalyticsBridge : NSObject <RCTBridgeModule>
- (void) trackEventWithCustomDimensionAndMetricValues:(NSString *)trackerId category:(NSString *)category action:(NSString *)action optionalValues:(NSDictionary *)optionalValues dimensionIndexValues:(NSDictionary *)dimensionIndexValues metricIndexValues:(NSDictionary *)metricIndexValues;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ - (NSDictionary *)constantsToExport
}

RCT_EXPORT_METHOD(trackEventWithCustomDimensionValues:(NSString *)trackerId category:(NSString *)category action:(NSString *)action optionalValues:(NSDictionary *)optionalValues dimensionIndexValues:(NSDictionary *)dimensionIndexValues)
{
[self trackEventWithCustomDimensionAndMetricValues:trackerId category:category action:action optionalValues:optionalValues dimensionIndexValues:dimensionIndexValues metricIndexValues:nil];
}

RCT_EXPORT_METHOD(trackEventWithCustomDimensionAndMetricValues:(NSString *)trackerId category:(NSString *)category action:(NSString *)action optionalValues:(NSDictionary *)optionalValues dimensionIndexValues:(NSDictionary *)dimensionIndexValues metricIndexValues:(NSDictionary *)metricIndexValues)
{
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:trackerId];
NSString *label = [RCTConvert NSString:optionalValues[@"label"]];
Expand All @@ -82,7 +87,12 @@ - (NSDictionary *)constantsToExport
value:value];
for (NSString *dimensionIndex in dimensionIndexValues)
[builder set:[dimensionIndexValues objectForKey:dimensionIndex] forKey:[GAIFields customDimensionForIndex:[dimensionIndex intValue]]];


if (metricIndexValues != nil){
for (NSString *metricIndex in metricIndexValues)
[builder set:[metricIndexValues objectForKey:metricIndex] forKey:[GAIFields customMetricForIndex:[metricIndex intValue]]];
}

[tracker send:[builder build]];
}

Expand Down
17 changes: 14 additions & 3 deletions src/GoogleAnalyticsTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class GoogleAnalyticsTracker {
trackScreenView(screenName) {
GoogleAnalyticsBridge.trackScreenView(this.id, screenName);
}

/**
* Track the campaign from url
* @param {String} urlString The url of the deep link
Expand Down Expand Up @@ -95,8 +95,19 @@ export class GoogleAnalyticsTracker {
const formattedCustomDimensions = this.transformCustomDimensionsFieldsToIndexes(customDimensionValues);
GoogleAnalyticsBridge.trackEventWithCustomDimensionValues(this.id, category, action, optionalValues, formattedCustomDimensions);
}

/**
* Track an event that has occured with custom dimension and metric values.
* @param {String} category The event category
* @param {String} action The event action
* @param {Object} optionalValues An object containing optional label and value
* @param {Object} customDimensionValues An object containing custom dimension key/value pairs
* @param {Object} customMetricValues An object containing custom metric key/value pairs
*/
trackEventWithCustomDimensionAndMetricValues(category, action, optionalValues = {}, customDimensionValues, customMetricValues) {
GoogleAnalyticsBridge.trackEventWithCustomDimensionAndMetricValues(this.id, category, action, optionalValues, customDimensionValues, customMetricValues);
}

/**
* Track an event that has occured
* @param {String} category The event category
* @param {Number} value The timing measurement in milliseconds
Expand Down Expand Up @@ -225,7 +236,7 @@ export class GoogleAnalyticsTracker {
setSamplingRate(sampleRatio) {
GoogleAnalyticsBridge.setSamplingRate(this.id, sampleRatio);
}

/**
* Sets the currency for tracking.
* @param {String} currencyCode The currency ISO 4217 code
Expand Down

0 comments on commit b009a27

Please sign in to comment.