182 lines
5.1 KiB
Dart
182 lines
5.1 KiB
Dart
// ignore: avoid_web_libraries_in_flutter
|
|
import 'dart:html' as html;
|
|
|
|
import 'package:analytics_sdk/manager/network_state_fields_web.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:analytics_sdk/utils/js_interop_compat.dart';
|
|
import 'package:analytics_sdk/utils/network_type_resolver.dart';
|
|
import 'package:battery_plus/battery_plus.dart';
|
|
|
|
/// Web 端环境字段采集。
|
|
Future<Map<String, dynamic>> collectEnvironmentInfo() async {
|
|
final navigator = html.window.navigator;
|
|
final screen = html.window.screen;
|
|
final browser = _parseBrowser(navigator.userAgent);
|
|
final batteryState = await _batteryState();
|
|
|
|
return {
|
|
'language': navigator.language,
|
|
'timezone': _timezoneIdentifier(),
|
|
'package_name': await _packageName(),
|
|
'screen_width': screen?.width ?? -1,
|
|
'screen_height': screen?.height ?? -1,
|
|
'screen_dpi': -1,
|
|
'orientation': _orientation(),
|
|
'viewport_width': html.window.innerWidth ?? -1,
|
|
'viewport_height': html.window.innerHeight ?? -1,
|
|
'device_pixel_ratio': (html.window.devicePixelRatio * 100).round(),
|
|
'network_type': await _networkType(),
|
|
'battery_level': await _batteryLevel(),
|
|
'is_charging': _isChargingState(batteryState),
|
|
'battery_state': _batteryStateName(batteryState),
|
|
'cpu_cores': navigator.hardwareConcurrency ?? -1,
|
|
'device_memory_gb': _deviceMemoryGb(navigator),
|
|
'browser_name': browser.name,
|
|
'browser_version': browser.version,
|
|
'cookie_enabled': navigator.cookieEnabled == true ? 1 : 0,
|
|
'local_storage_enabled': _localStorageEnabled(),
|
|
'touch_support': (navigator.maxTouchPoints ?? 0) > 0 ? 1 : 0,
|
|
'color_depth': screen?.colorDepth ?? -1,
|
|
'is_pwa': _isPwa(),
|
|
};
|
|
}
|
|
|
|
String _timezoneIdentifier() {
|
|
try {
|
|
final intl = getJsProperty(html.window, 'Intl');
|
|
if (intl == null) return DateTime.now().timeZoneName;
|
|
final formatter = callJsMethod(intl, 'DateTimeFormat', const []);
|
|
if (formatter == null) return DateTime.now().timeZoneName;
|
|
final options = callJsMethod(formatter, 'resolvedOptions', const []);
|
|
if (options == null) return DateTime.now().timeZoneName;
|
|
final timezone =
|
|
getJsProperty(options, 'timeZone')?.toString().trim() ?? '';
|
|
if (timezone.isNotEmpty) return timezone;
|
|
} catch (_) {}
|
|
return DateTime.now().timeZoneName;
|
|
}
|
|
|
|
Future<String> _packageName() async {
|
|
try {
|
|
final info = await PackageInfo.fromPlatform();
|
|
return info.packageName;
|
|
} catch (_) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
Future<String> _networkType() async {
|
|
try {
|
|
final connection = await collectNetworkStateFields();
|
|
return resolveWebNetworkType(
|
|
connectionType: connection['connection_type']?.toString() ?? 'unknown',
|
|
effectiveType: connection['effective_type']?.toString() ?? 'unknown',
|
|
);
|
|
} catch (_) {
|
|
return 'unknown';
|
|
}
|
|
}
|
|
|
|
Future<int> _batteryLevel() async {
|
|
try {
|
|
return await Battery().batteryLevel;
|
|
} catch (_) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<BatteryState> _batteryState() async {
|
|
try {
|
|
return await Battery().batteryState;
|
|
} catch (_) {
|
|
return BatteryState.unknown;
|
|
}
|
|
}
|
|
|
|
int _isChargingState(BatteryState state) {
|
|
if (state == BatteryState.charging || state == BatteryState.full) return 1;
|
|
if (state == BatteryState.discharging) return 0;
|
|
return -1;
|
|
}
|
|
|
|
String _batteryStateName(BatteryState state) {
|
|
switch (state) {
|
|
case BatteryState.charging:
|
|
return 'charging';
|
|
case BatteryState.discharging:
|
|
return 'discharging';
|
|
case BatteryState.full:
|
|
return 'full';
|
|
case BatteryState.unknown:
|
|
return 'unknown';
|
|
}
|
|
}
|
|
|
|
String _orientation() {
|
|
try {
|
|
final width = html.window.innerWidth ?? 0;
|
|
final height = html.window.innerHeight ?? 0;
|
|
if (width <= 0 || height <= 0) return '';
|
|
return width >= height ? 'landscape' : 'portrait';
|
|
} catch (_) {
|
|
return '';
|
|
}
|
|
}
|
|
|
|
int _deviceMemoryGb(html.Navigator navigator) {
|
|
try {
|
|
final memory = getJsProperty(navigator, 'deviceMemory');
|
|
return memory is num ? memory.round() : -1;
|
|
} catch (_) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
int _localStorageEnabled() {
|
|
try {
|
|
const key = '__analytics_sdk_storage_test__';
|
|
html.window.localStorage[key] = '1';
|
|
final ok = html.window.localStorage[key] == '1';
|
|
html.window.localStorage.remove(key);
|
|
return ok ? 1 : 0;
|
|
} catch (_) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int _isPwa() {
|
|
try {
|
|
final standalone = html.window
|
|
.matchMedia('(display-mode: standalone)')
|
|
.matches;
|
|
final iosStandalone =
|
|
getJsProperty(html.window.navigator, 'standalone') == true;
|
|
return standalone || iosStandalone ? 1 : 0;
|
|
} catch (_) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
_BrowserInfo _parseBrowser(String ua) {
|
|
final rules = <String, RegExp>{
|
|
'Edge': RegExp(r'Edg/([\d.]+)'),
|
|
'Chrome': RegExp(r'Chrome/([\d.]+)'),
|
|
'Firefox': RegExp(r'Firefox/([\d.]+)'),
|
|
'Safari': RegExp(r'Version/([\d.]+).*Safari'),
|
|
};
|
|
for (final entry in rules.entries) {
|
|
final match = entry.value.firstMatch(ua);
|
|
if (match != null) {
|
|
return _BrowserInfo(entry.key, match.group(1) ?? '');
|
|
}
|
|
}
|
|
return const _BrowserInfo('', '');
|
|
}
|
|
|
|
class _BrowserInfo {
|
|
final String name;
|
|
final String version;
|
|
|
|
const _BrowserInfo(this.name, this.version);
|
|
}
|