diff --git a/CHANGELOG.md b/CHANGELOG.md index f16f1c4..01ebd0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.2 + +* Added error handling for http request. + ## 0.1.1 * Added disable feature. diff --git a/lib/plausible_analytics.dart b/lib/plausible_analytics.dart index 4a8b716..3f462d4 100644 --- a/lib/plausible_analytics.dart +++ b/lib/plausible_analytics.dart @@ -41,23 +41,28 @@ class Plausible { } // Http Post request see https://plausible.io/docs/events-api - HttpClient client = HttpClient(); - HttpClientRequest request = - await client.postUrl(Uri.parse(serverUrl + '/api/event')); - request.headers.set('User-Agent', userAgent); - request.headers.set('Content-Type', 'application/json'); - request.headers.set('X-Forwarded-For', '127.0.0.1'); - Object body = { - "domain": domain, - "name": name, - "url": page, - "referrer": referrer, - "screen_width": screenWidth - }; - request.write(json.encode(body)); - final HttpClientResponse response = await request.close(); - client.close(); - - return response.statusCode; + try { + HttpClient client = HttpClient(); + HttpClientRequest request = + await client.postUrl(Uri.parse(serverUrl + '/api/event')); + request.headers.set('User-Agent', userAgent); + request.headers.set('Content-Type', 'application/json'); + request.headers.set('X-Forwarded-For', '127.0.0.1'); + Object body = { + "domain": domain, + "name": name, + "url": page, + "referrer": referrer, + "screen_width": screenWidth + }; + request.write(json.encode(body)); + final HttpClientResponse response = await request.close(); + client.close(); + return response.statusCode; + } catch (e) { + print(e); + } + + return 1; } } diff --git a/pubspec.yaml b/pubspec.yaml index 1990d8d..4c27cf9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: plausible_analytics description: A Flutter plugin for Plausible Analytics. -version: 0.1.1 +version: 0.1.2 homepage: https://github.com/bostrot/flutter_plausible_analytics.git issue_tracker: https://github.com/bostrot/flutter_plausible_analytics/issues diff --git a/test/plausible_analytics_test.dart b/test/plausible_analytics_test.dart index 49752b0..f48b601 100644 --- a/test/plausible_analytics_test.dart +++ b/test/plausible_analytics_test.dart @@ -29,6 +29,7 @@ void main() { name: 'conversion', page: 'homescreen', referrer: 'referrerPage'); expect(await event, 202); }); + test('check disabled call', () async { final plausible = Plausible(serverUrl, domain, screenWidth: screenWidth); plausible.enabled = false; @@ -40,4 +41,16 @@ void main() { name: 'conversion', page: 'homescreen', referrer: 'referrerPage'); expect(await event, 0); }); + + test('check failed http request', () async { + final plausible = + Plausible("somewrongurl.asd21", domain, screenWidth: screenWidth); + expect(plausible.serverUrl, "somewrongurl.asd21"); + expect(plausible.domain, domain); + expect(plausible.screenWidth, screenWidth); + + final event = plausible.event( + name: 'conversion', page: 'homescreen', referrer: 'referrerPage'); + expect(await event, 1); + }); }