Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit 03d778c

Browse files
cpunioncbrevik
authored andcommitted
Refactor exception handing and format
1 parent 021bdae commit 03d778c

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

android/src/main/java/com/idehub/GoogleAnalyticsBridge/GoogleAnalyticsBridge.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,12 @@ public void createNewSession(String trackerId, String screenName) {
485485

486486
@ReactMethod
487487
public void dispatch(Promise promise) {
488-
GoogleAnalytics analytics = GoogleAnalytics.getInstance(getReactApplicationContext());
489-
analytics.dispatchLocalHits();
490-
promise.resolve(true);
488+
GoogleAnalytics analytics = getAnalyticsInstance();
489+
try {
490+
analytics.dispatchLocalHits();
491+
promise.resolve(true);
492+
} catch (Exception ex) {
493+
promise.reject(ex);
494+
}
491495
}
492496
}

ios/RCTGoogleAnalyticsBridge/RCTGoogleAnalyticsBridge/RCTGoogleAnalyticsBridge.m

+6-2
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,13 @@ - (NSDictionary *)constantsToExport
378378
}
379379

380380
RCT_EXPORT_METHOD(dispatch:(RCTPromiseResolveBlock)resolve
381-
reject:(__unused RCTPromiseRejectBlock)reject) {
381+
reject:(RCTPromiseRejectBlock)reject) {
382382
[[GAI sharedInstance] dispatchWithCompletionHandler:^void(GAIDispatchResult result){
383-
resolve(result != kGAIDispatchError ? @YES : @NO);
383+
if (result != kGAIDispatchError) {
384+
resolve(@YES);
385+
} else {
386+
reject(@"DISPATCH_FAILED", nil, RCTErrorWithMessage(@"Dispatch failed"));
387+
}
384388
}];
385389
}
386390

src/GoogleAnalyticsTracker.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { GoogleAnalyticsBridge } from './NativeBridges';
22

3+
const DEFAULT_DISPATCH_TIMEOUT = 15000;
4+
35
/**
46
* Custom dimensions accept only strings and numbers.
57
* @param customDimensionVal
@@ -250,25 +252,25 @@ export class GoogleAnalyticsTracker {
250252
}
251253

252254
dispatch() {
253-
return GoogleAnalyticsBridge.dispatch()
255+
return GoogleAnalyticsBridge.dispatch();
254256
}
255257

256258
dispatchWithTimeout(timeout = -1) {
257259
if (timeout < 0) {
258-
return this.dispatch()
260+
return GoogleAnalyticsBridge.dispatch();
259261
}
260262

261263
const withTimeout = timeout => (
262264
new Promise(resolve => {
263265
setTimeout(() => {
264-
resolve()
265-
}, (timeout > 15000) ? 15000 : timeout)
266+
resolve();
267+
}, Math.min(timeout, DEFAULT_DISPATCH_TIMEOUT));
266268
})
267-
)
269+
);
268270

269271
return Promise.race([
270272
GoogleAnalyticsBridge.dispatch(),
271273
withTimeout(timeout)
272-
])
274+
]);
273275
}
274276
}

0 commit comments

Comments
 (0)