Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to the latests native SDKs #179

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}
Expand Down
10 changes: 8 additions & 2 deletions example/lib/my_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -30,7 +31,7 @@ class _MyAppState extends State<MyApp> {
late Amplitude analytics;

initAnalytics() async {
await analytics.init();
await analytics.isBuilt;

setMessage('Amplitude initialized');
}
Expand All @@ -39,7 +40,12 @@ class _MyAppState extends State<MyApp> {
void initState() {
super.initState();
analytics = Amplitude(
Configuration(apiKey: widget.apiKey, logLevel: LogLevel.debug));
Configuration(
apiKey: widget.apiKey,
logLevel: LogLevel.debug,
defaultTracking: DefaultTrackingOptions.all()
)
);
initAnalytics();
}

Expand Down
34 changes: 25 additions & 9 deletions lib/amplitude.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> isBuilt;
justin-fiedler marked this conversation as resolved.
Show resolved Hide resolved

/// 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<void> init([MethodChannel? methodChannel]) async {
Amplitude(this.configuration, [MethodChannel? methodChannel]){
_channel = methodChannel ?? this._channel;
return await _channel.invokeMethod(
"init", this.configuration.toMap());
isBuilt = _init();
justin-fiedler marked this conversation as resolved.
Show resolved Hide resolved
}

/// Private method to initialize and return a Future<bool>
Future<bool> _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.
Expand Down Expand Up @@ -184,14 +200,14 @@ class Amplitude {
Map<String, String?> 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<void> reset() async {
await await _channel.invokeMethod("reset");
return await _channel.invokeMethod("reset");
}

/// Flush events in storage.
Expand Down
14 changes: 8 additions & 6 deletions lib/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion release.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
"branches": [
"main"
"main",
{ "name": "beta", "prerelease": true },
yuhao900914 marked this conversation as resolved.
Show resolved Hide resolved
],
"tagFormat": ["v${version}"],
"plugins": [
Expand Down
4 changes: 2 additions & 2 deletions test/amplitude_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading