mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 14:53:44 +00:00
feat: new ui (#1455)
This commit is contained in:
742 files changed
+59951
-5934
No files matched your search
@@ -4,73 +4,92 @@ import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:gopeed/database/database.dart';
|
||||
import 'package:gopeed/util/package_info.dart';
|
||||
|
||||
import '../database/database.dart';
|
||||
import 'log_util.dart';
|
||||
import 'package_info.dart';
|
||||
|
||||
/// GA4 Measurement Protocol configuration from dart-define
|
||||
class Config {
|
||||
static const String measurementId =
|
||||
String.fromEnvironment('GA4_MEASUREMENT_ID');
|
||||
static const String apiSecret = String.fromEnvironment('GA4_API_SECRET');
|
||||
/// GA4 Measurement Protocol configuration supplied by the release build.
|
||||
class AnalyticsConfig {
|
||||
const AnalyticsConfig._();
|
||||
|
||||
static bool get isConfigured =>
|
||||
measurementId.isNotEmpty && apiSecret.isNotEmpty;
|
||||
static const measurementId = String.fromEnvironment('GA4_MEASUREMENT_ID');
|
||||
static const apiSecret = String.fromEnvironment('GA4_API_SECRET');
|
||||
|
||||
static bool get isConfigured => measurementId.isNotEmpty && apiSecret.isNotEmpty;
|
||||
}
|
||||
|
||||
/// GA4 Measurement Protocol Analytics
|
||||
/// Sends only anonymous application events. The preference is stored outside
|
||||
/// downloader configuration so it remains local to the current installation.
|
||||
class Analytics {
|
||||
static final Analytics _instance = Analytics._internal();
|
||||
static Analytics get instance => _instance;
|
||||
Analytics._();
|
||||
|
||||
Analytics._internal();
|
||||
static final instance = Analytics._();
|
||||
|
||||
late String _clientId;
|
||||
late int _sessionId;
|
||||
late Dio _dio;
|
||||
late final String _clientId;
|
||||
late final int _sessionId;
|
||||
late final Dio _dio;
|
||||
bool _initialized = false;
|
||||
|
||||
Future<void> init() async {
|
||||
if (_initialized) return;
|
||||
if (!Config.isConfigured) return;
|
||||
|
||||
if (_initialized || !AnalyticsConfig.isConfigured) return;
|
||||
_clientId = await _getDeviceId();
|
||||
_dio = Dio(BaseOptions(
|
||||
connectTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 10),
|
||||
));
|
||||
_sessionId = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
_dio = Dio(BaseOptions(connectTimeout: const Duration(seconds: 10), receiveTimeout: const Duration(seconds: 10)));
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
Future<void> logAppOpen() => logEvent('app_open');
|
||||
|
||||
Future<void> logEvent(String name, [Map<String, dynamic>? params]) async {
|
||||
if (!_initialized) return;
|
||||
final payload = {
|
||||
'client_id': _clientId,
|
||||
'timestamp_micros': DateTime.now().microsecondsSinceEpoch,
|
||||
'events': [
|
||||
{
|
||||
'name': name,
|
||||
'params': {
|
||||
'session_id': _sessionId.toString(),
|
||||
'engagement_time_msec': 100,
|
||||
'platform': _platformName(),
|
||||
'app_version': appVersion,
|
||||
...?params,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
try {
|
||||
await _dio.post(
|
||||
'https://www.google-analytics.com/mp/collect',
|
||||
queryParameters: {'measurement_id': AnalyticsConfig.measurementId, 'api_secret': AnalyticsConfig.apiSecret},
|
||||
data: payload,
|
||||
);
|
||||
} catch (error) {
|
||||
logger.w('GA4Analytics: failed to send event "$name": $error');
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _getDeviceId() async {
|
||||
final deviceInfo = DeviceInfoPlugin();
|
||||
String? deviceId;
|
||||
try {
|
||||
final info = DeviceInfoPlugin();
|
||||
if (kIsWeb) {
|
||||
deviceId = null;
|
||||
} else if (Platform.isAndroid) {
|
||||
final androidInfo = await deviceInfo.androidInfo;
|
||||
deviceId = androidInfo.id;
|
||||
deviceId = (await info.androidInfo).id;
|
||||
} else if (Platform.isIOS) {
|
||||
final iosInfo = await deviceInfo.iosInfo;
|
||||
deviceId = iosInfo.identifierForVendor;
|
||||
deviceId = (await info.iosInfo).identifierForVendor;
|
||||
} else if (Platform.isMacOS) {
|
||||
final macInfo = await deviceInfo.macOsInfo;
|
||||
deviceId = macInfo.systemGUID;
|
||||
deviceId = (await info.macOsInfo).systemGUID;
|
||||
} else if (Platform.isWindows) {
|
||||
final windowsInfo = await deviceInfo.windowsInfo;
|
||||
deviceId = windowsInfo.deviceId;
|
||||
deviceId = (await info.windowsInfo).deviceId;
|
||||
} else if (Platform.isLinux) {
|
||||
final linuxInfo = await deviceInfo.linuxInfo;
|
||||
deviceId = linuxInfo.machineId;
|
||||
deviceId = (await info.linuxInfo).machineId;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('GA4Analytics: Failed to get device id: $e');
|
||||
} catch (error) {
|
||||
logger.w('GA4Analytics: failed to read device id: $error');
|
||||
}
|
||||
|
||||
// Fallback to persisted client id
|
||||
if (deviceId == null || deviceId.isEmpty) {
|
||||
deviceId = Database.instance.getAnalyticsClientId();
|
||||
if (deviceId == null || deviceId.isEmpty) {
|
||||
@@ -83,7 +102,7 @@ class Analytics {
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
String _getPlatform() {
|
||||
String _platformName() {
|
||||
if (kIsWeb) return 'web';
|
||||
if (Platform.isAndroid) return 'android';
|
||||
if (Platform.isIOS) return 'ios';
|
||||
@@ -92,46 +111,4 @@ class Analytics {
|
||||
if (Platform.isLinux) return 'linux';
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
Future<void> logAppOpen() async {
|
||||
await logEvent('app_open');
|
||||
}
|
||||
|
||||
Future<void> logEvent(String name, [Map<String, dynamic>? params]) async {
|
||||
if (!_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
final eventParams = <String, dynamic>{
|
||||
'session_id': _sessionId.toString(),
|
||||
'engagement_time_msec': 100,
|
||||
'platform': _getPlatform(),
|
||||
'app_version': packageInfo.version,
|
||||
...?params,
|
||||
};
|
||||
|
||||
final payload = {
|
||||
'client_id': _clientId,
|
||||
'timestamp_micros': DateTime.now().microsecondsSinceEpoch,
|
||||
'events': [
|
||||
{
|
||||
'name': name,
|
||||
'params': eventParams,
|
||||
}
|
||||
],
|
||||
};
|
||||
|
||||
try {
|
||||
await _dio.post(
|
||||
'https://www.google-analytics.com/mp/collect',
|
||||
queryParameters: {
|
||||
'measurement_id': Config.measurementId,
|
||||
'api_secret': Config.apiSecret,
|
||||
},
|
||||
data: payload,
|
||||
);
|
||||
} catch (e) {
|
||||
logger.w('GA4Analytics: Failed to send event "$name": $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user