diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 93a3952..72c9dfb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -49,8 +49,8 @@ jobs: - name: Analyze run: flutter analyze --no-fatal-infos -# - name: Test -# run: flutter test + - name: Test + run: flutter test - name: Semantic Release --dry-run if: ${{ github.event.inputs.dryRun == 'true'}} diff --git a/example/lib/my_app.dart b/example/lib/my_app.dart index 771f5bb..d95b806 100644 --- a/example/lib/my_app.dart +++ b/example/lib/my_app.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:amplitude_flutter/amplitude.dart'; import 'package:amplitude_flutter/configuration.dart'; import 'package:amplitude_flutter/constants.dart'; +import 'package:amplitude_flutter/default_tracking.dart'; import 'package:flutter/material.dart'; import 'app_state.dart'; @@ -30,7 +31,7 @@ class _MyAppState extends State { late Amplitude analytics; initAnalytics() async { - await analytics.init(); + await analytics.isBuilt; setMessage('Amplitude initialized'); } @@ -39,7 +40,12 @@ class _MyAppState extends State { void initState() { super.initState(); analytics = Amplitude( - Configuration(apiKey: widget.apiKey, logLevel: LogLevel.debug)); + Configuration( + apiKey: widget.apiKey, + logLevel: LogLevel.debug, + defaultTracking: DefaultTrackingOptions.all() + ) + ); initAnalytics(); } diff --git a/lib/amplitude.dart b/lib/amplitude.dart index e3b680d..7c1d5d2 100644 --- a/lib/amplitude.dart +++ b/lib/amplitude.dart @@ -12,20 +12,36 @@ import 'package:amplitude_flutter/events/group_identify_event.dart'; class Amplitude { Configuration configuration; MethodChannel _channel = const MethodChannel("amplitude_flutter"); + /// Whether the Amplitude instance has been successfully initialized + /// + /// ``` + /// var amplitude = Amplitude(Configuration(apiKey: "apiKey")); + /// // If care about init complete + /// await amplitude.isBuilt; + /// ``` + late Future isBuilt; /// Returns an Amplitude instance - Amplitude(this.configuration); - - /// Initializes an Amplitude instance /// /// ``` /// var amplitude = Amplitude(Configuration(apiKey: "apiKey")); - /// await amplitude.init(); + /// // If care about init complete + /// await amplitude.isBuilt; /// ``` - Future init([MethodChannel? methodChannel]) async { + Amplitude(this.configuration, [MethodChannel? methodChannel]){ _channel = methodChannel ?? this._channel; - return await _channel.invokeMethod( - "init", this.configuration.toMap()); + isBuilt = _init(); + } + + /// Private method to initialize and return a Future + Future _init() async { + try { + await _channel.invokeMethod("init", configuration.toMap()); + return true; // Initialization successful + } catch (e) { + print("Error initializing Amplitude: $e"); + return false; // Initialization failed + } } /// Tracks an event. Events are saved locally. @@ -184,14 +200,14 @@ class Amplitude { Map properties = {}; properties["setDeviceId"] = deviceId; - await await _channel.invokeMethod("setDeviceId", properties); + return await _channel.invokeMethod("setDeviceId", properties); } /// Resets userId to "null" and deviceId to a random UUID. /// /// Note different devices on different platforms should have different device Ids. Future reset() async { - await await _channel.invokeMethod("reset"); + return await _channel.invokeMethod("reset"); } /// Flush events in storage. diff --git a/lib/configuration.dart b/lib/configuration.dart index 958c2f0..99308c8 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -2,12 +2,6 @@ import 'constants.dart'; import 'tracking_options.dart'; import 'default_tracking.dart'; -/// Configuration for Amplitude instance. -/// -/// Before initializing Amplitude instance, create a Configuration instance -/// with your desired configuration and pass it to the Amplitude instance. -/// Note the Configuration is immutable (cannot be changed) after being passed to Amplitude.init() -/// `optOut` can be changed later by calling `setOptOut()`. class Configuration { String apiKey; int flushQueueSize; @@ -40,6 +34,14 @@ class Configuration { bool useAppSetIdForDeviceId; /// Web specific String? appVersion; + + /// Configuration for Amplitude instance. + /// + /// Before initializing Amplitude instance, create a Configuration instance + /// with your desired configuration and pass it to the Amplitude instance. + /// + /// Note the Configuration is immutable (cannot be changed) after being passed to Amplitude + /// `optOut` can be changed later by calling `setOptOut()`. Configuration({ required this.apiKey, this.flushQueueSize = Constants.flushQueueSize, diff --git a/release.config.js b/release.config.js index 28bebf7..c3d538b 100644 --- a/release.config.js +++ b/release.config.js @@ -1,6 +1,7 @@ module.exports = { "branches": [ - "main" + "main", + { "name": "beta", "prerelease": true }, ], "tagFormat": ["v${version}"], "plugins": [ diff --git a/test/amplitude_test.dart b/test/amplitude_test.dart index a089422..c112510 100644 --- a/test/amplitude_test.dart +++ b/test/amplitude_test.dart @@ -100,8 +100,8 @@ void main() { mockChannel = MockMethodChannel(); when(mockChannel.invokeListMethod("init", any)) .thenAnswer((_) async => null); - amplitude = Amplitude(testConfiguration); - await amplitude.init(mockChannel); + amplitude = Amplitude(testConfiguration, mockChannel); + await amplitude.isBuilt; }); test("Should init and track call MethodChannel", () async {