diff --git a/packages/core/lib/analytics_web.dart b/packages/core/lib/analytics_web.dart index 1dbd50d..303661a 100644 --- a/packages/core/lib/analytics_web.dart +++ b/packages/core/lib/analytics_web.dart @@ -1,10 +1,9 @@ import 'dart:async'; -import 'dart:html' as html show window; -import 'package:segment_analytics/native_context.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; - import 'package:segment_analytics/analytics_platform_interface.dart'; +import 'package:segment_analytics/native_context.dart'; +import 'package:web/web.dart' as web; export 'package:segment_analytics/client.dart'; @@ -15,19 +14,20 @@ class AnalyticsPlatformImpl extends AnalyticsPlatform { /// Returns a [String] containing the version of the platform. @override - Future getContext({bool collectDeviceId = false}) => - Future.value(NativeContext( - app: NativeContextApp( - name: html.window.navigator.appName, - version: html.window.navigator.appVersion, - namespace: html.window.navigator.appCodeName), - userAgent: html.window.navigator.userAgent, - locale: html.window.navigator.language, - screen: html.window.screen != null - ? NativeContextScreen( - height: html.window.screen?.height, - width: html.window.screen?.width) - : null)); + Future getContext({bool collectDeviceId = false}) async => + NativeContext( + app: NativeContextApp( + name: web.window.navigator.appName, + version: web.window.navigator.appVersion, + namespace: web.window.navigator.appCodeName, + ), + userAgent: web.window.navigator.userAgent, + locale: web.window.navigator.language, + screen: NativeContextScreen( + height: web.window.screen.height, + width: web.window.screen.width, + ), + ); } class AnalyticsWeb { diff --git a/packages/core/lib/utils/store/web.dart b/packages/core/lib/utils/store/web.dart index 76869a8..714564a 100644 --- a/packages/core/lib/utils/store/web.dart +++ b/packages/core/lib/utils/store/web.dart @@ -1,11 +1,11 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:html' as html; import 'package:segment_analytics/utils/store/store.dart'; +import 'package:web/web.dart' as web; class StoreImpl implements Store { - html.Storage get localStorage => html.window.localStorage; + web.Storage get localStorage => web.window.localStorage; @override Future?> getPersisted(String key) { @@ -17,52 +17,45 @@ class StoreImpl implements Store { @override Future setPersisted(String key, Map value) { - return _writeToStorage(key, value); + _writeToStorage(key, value); + return Future.value(); } String _getFileName(String fileKey) { return "analytics-flutter-$fileKey.json"; } - Future _writeToStorage( - String fileKey, Map data) async { - localStorage.update( + void _writeToStorage(String fileKey, Map data) { + localStorage.setItem( _getFileName(fileKey), - (val) => json.encode(data), - ifAbsent: () => json.encode(data), + json.encode(data), ); } Future?> _readFromStorage(String fileKey) async { final fileName = _getFileName(fileKey); - MapEntry? data; + final data = localStorage.getItem(fileName); String anonymousId; - - try { - data = localStorage.entries.firstWhere((i) => i.key == fileName); - } on StateError { - data = null; - } - if(fileKey == "userInfo") { - anonymousId = getExistingAnonymousId(data); - if(data != null) { - final jsonDecoded = json.decode(data.value); - if(anonymousId.isNotEmpty){ + if (fileKey == "userInfo") { + anonymousId = getExistingAnonymousId(); + if (data != null) { + final jsonDecoded = json.decode(data); + if (anonymousId.isNotEmpty) { jsonDecoded["anonymousId"] = anonymousId; return jsonDecoded as Map; } - } else if(anonymousId.isNotEmpty){ - final json = {"anonymousId": anonymousId }; + } else if (anonymousId.isNotEmpty) { + final json = {"anonymousId": anonymousId}; return json; } } if (data != null) { - if (data.value == "{}") { + if (data == "{}") { return null; // Prefer null to empty map, because we'll want to initialise a valid empty value. } - return json.decode(data.value) as Map; + return json.decode(data) as Map; } else { return null; } @@ -71,28 +64,20 @@ class StoreImpl implements Store { @override void dispose() {} - String getExistingAnonymousId(MapEntry? data) { - String anonymousId; - try { - final entry = localStorage.entries.firstWhere( - (i) => i.key == "ajs_anonymous_id", - ); - anonymousId = entry.value; - } on StateError { - anonymousId = ''; - } + String getExistingAnonymousId() { + var anonymousId = localStorage.getItem("ajs_anonymous_id"); - if (anonymousId.isEmpty) { - final cookies = html.document.cookie?.split(";"); - if (cookies != null && cookies.isNotEmpty) { - for (var cookie in cookies) { - final cookieParts = cookie.split("="); - if (cookieParts[0].trim() == "ajs_anonymous_id") { - anonymousId = cookieParts[1]; - } + if (anonymousId?.isEmpty ?? true) { + final cookies = web.document.cookie.split(";"); + if (cookies.isNotEmpty) { + for (var cookie in cookies) { + final cookieParts = cookie.split("="); + if (cookieParts[0].trim() == "ajs_anonymous_id") { + anonymousId = cookieParts[1]; } } } - return anonymousId.isEmpty ? '' : anonymousId; + } + return anonymousId ?? ''; } } diff --git a/packages/core/pubspec.yaml b/packages/core/pubspec.yaml index 1a3e37a..056fb16 100644 --- a/packages/core/pubspec.yaml +++ b/packages/core/pubspec.yaml @@ -23,6 +23,7 @@ dependencies: path_provider: ^2.0.12 flutter_fgbg: ^0.3.0 shared_preferences: ^2.2.2 + web: ^0.5.1 dev_dependencies: build_runner: ^2.3.3