Skip to content

Commit

Permalink
chore: use positional parameters for cleanliness
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Apr 4, 2024
1 parent 74a1d14 commit 5b612af
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 176 deletions.
2 changes: 1 addition & 1 deletion example/lib/event_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class _EventFormState extends State<EventForm> {

void onPress() {
AppState.of(context)
..analytics.track(event: BaseEvent(eventType: _controller.text))
..analytics.track(BaseEvent(eventType: _controller.text))
..setMessage('Event sent.');
}

Expand Down
2 changes: 1 addition & 1 deletion example/lib/group_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class _GroupFormState extends State<GroupForm> {
void onPress() {
if (groupType.text.isNotEmpty && groupValue.text.isNotEmpty) {
AppState.of(context)
..analytics.setGroup(groupType: groupType.text, groupName: groupValue.text)
..analytics.setGroup(groupType.text, groupValue.text)
..setMessage('Group set.');
}
}
Expand Down
4 changes: 2 additions & 2 deletions example/lib/group_identify_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class _GroupIdentifyFormState extends State<GroupIdentifyForm> {
groupPropertyKey.text.isNotEmpty &&
groupPropertyValue.text.isNotEmpty) {
final Identify identify = Identify()
..set(property: groupPropertyKey.text, value: groupPropertyValue.text);
..set(groupPropertyKey.text, groupPropertyValue.text);

AppState.of(context)
..analytics.groupIdentify(groupType: groupType.text, groupName: groupValue.text, identify: identify)
..analytics.groupIdentify(groupType.text, groupValue.text, identify)
..setMessage('Group Identify sent.');
}
}
Expand Down
8 changes: 4 additions & 4 deletions example/lib/identify_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ class IdentifyForm extends StatefulWidget {
class _IdentifyFormState extends State<IdentifyForm> {
void onPress() {
final Identify identify = Identify()
..set(property: 'identify_test', value: 'identify sent at ${DateTime
..set('identify_test', 'identify sent at ${DateTime
.now()
.millisecondsSinceEpoch}')
..add(property: "identify_count", value: 1);
..add('identify_count', 1);

if (userPropKey.isNotEmpty && userPropValue.isNotEmpty) {
identify.set(property: userPropKey, value: userPropValue);
identify.set(userPropKey, userPropValue);
}

AppState.of(context)
..analytics.identify(identify: identify)
..analytics.identify(identify)
..setMessage('Identify sent.');
}

Expand Down
2 changes: 1 addition & 1 deletion example/lib/revenue_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class _RevenueFormState extends State<RevenueForm> {
..quantity = int.tryParse(quantity.text)!
.. productId = productId.text;
AppState.of(context)
..analytics.revenue(revenue: revenue)
..analytics.revenue(revenue)
..setMessage('Revenue Sent.');
}
}
Expand Down
110 changes: 59 additions & 51 deletions lib/amplitude.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import 'dart:async';

import 'package:amplitude_flutter/events/event_options.dart';
import 'package:amplitude_flutter/events/identify_event.dart';
import 'package:amplitude_flutter/events/identify.dart';
import 'package:amplitude_flutter/events/revenue.dart';
import 'package:flutter/services.dart';
import 'package:amplitude_flutter/configuration.dart';
import 'package:amplitude_flutter/events/base_event.dart';
import 'package:amplitude_flutter/events/group_identify_event.dart';
import 'events/event_options.dart';
import 'events/identify_event.dart';
import 'events/identify.dart';
import 'events/revenue.dart';
import 'configuration.dart';
import 'events/base_event.dart';
import 'events/group_identify_event.dart';

class Amplitude {
Configuration configuration;
MethodChannel _channel = const MethodChannel("amplitude_flutter");
MethodChannel _channel = const MethodChannel('amplitude_flutter');

/// Whether the Amplitude instance has been successfully initialized
///
/// ```
/// var amplitude = Amplitude(Configuration(apiKey: "apiKey"));
/// var amplitude = Amplitude(Configuration(apiKey: 'apiKey'));
/// // If care about init complete
/// await amplitude.isBuilt;
/// ```
Expand All @@ -24,22 +25,22 @@ class Amplitude {
/// Returns an Amplitude instance
///
/// ```
/// var amplitude = Amplitude(Configuration(apiKey: "apiKey"));
/// final amplitude = Amplitude(Configuration(apiKey: 'apiKey'));
/// // If care about init complete
/// await amplitude.isBuilt;
/// ```
Amplitude(this.configuration, [MethodChannel? methodChannel]){
Amplitude(this.configuration, [MethodChannel? methodChannel]) {
_channel = methodChannel ?? this._channel;
isBuilt = _init();
}

/// Private method to initialize and return a Future<bool>
Future<bool> _init() async {
try {
await _channel.invokeMethod("init", configuration.toMap());
await _channel.invokeMethod('init', configuration.toMap());
return true; // Initialization successful
} catch (e) {
print("Error initializing Amplitude: $e");
print('Error initializing Amplitude: $e');
return false; // Initialization failed
}
}
Expand All @@ -48,15 +49,19 @@ class Amplitude {
///
/// Uploads are batched to occur every 30 events or every 30 seconds
/// (whichever comes first), as well as on app close.
Future<void> track({
required BaseEvent event,
///
/// ```
/// amplitude.track(BaseEvent(eventType: 'Button Clicked'))
/// ```
Future<void> track(
BaseEvent event, [
EventOptions? options,
}) async {
]) async {
if (options != null) {
event.mergeEventOptions(options);
}

return await _channel.invokeMethod("track", event.toMap());
return await _channel.invokeMethod('track', event.toMap());
}

/// Updates user properties using operations provided via Identify API.
Expand All @@ -70,10 +75,9 @@ class Amplitude {
/// final Identify identify = Identify()
/// ..set('gender','male')
/// ..add('karma', 1);
/// Amplitude.getInstance.identify(identify);
/// amplitude.identify(identify);
/// ```
Future<void> identify(
{required Identify identify, EventOptions? options}) async {
Future<void> identify(Identify identify, [EventOptions? options]) async {
final event = IdentifyEvent();
event.userProperties = identify.properties;

Expand All @@ -89,7 +93,7 @@ class Amplitude {
}
}

return await _channel.invokeMethod("identify", event.toMap());
return await _channel.invokeMethod('identify', event.toMap());
}

/// Updates the properties of particular groups.
Expand All @@ -105,14 +109,12 @@ class Amplitude {
/// Example: if you wanted to set a key-value pair as a group property to the enterprise group with group type to be plan, you would do:
/// ```
/// final groupIdentifyEvent = Identify()
/// ..set(property: 'key1', value: 'value1');
/// amplitude.groupIdentify(groupType: "plan", groupName: "enterprise", identify: identify);
/// ..set('key1', 'value1');
/// amplitude.groupIdentify('plan', 'enterprise', identify);
/// ```
Future<void> groupIdentify(
{required String groupType,
required String groupName,
required Identify identify,
EventOptions? options}) async {
String groupType, String groupName, Identify identify,
[EventOptions? options]) async {
final event = GroupIdentifyEvent();
final group = Map<String, dynamic>();
group[groupType] = groupName;
Expand All @@ -122,29 +124,30 @@ class Amplitude {
event.mergeEventOptions(options);
}

return await _channel.invokeMethod(
"groupIdentify", event.toMap());
return await _channel.invokeMethod('groupIdentify', event.toMap());
}

/// Adds a user to a group or groups. You need to specify a groupType and groupName(s).
///
/// For example you can group people by their organization. In this case,
/// groupType is "orgId", and groupName would be the actual ID(s).
/// groupType is 'orgId', and groupName would be the actual ID(s).
/// groupName can be a string or an array of strings to indicate a user in multiple groups.
///
/// ```
/// amplitude.setGroup('orgId', '15');
/// ```
///
/// You can also call setGroup multiple times with different groupTypes to track
/// multiple types of groups (up to 5 per app).
/// Note: This will also set groupType: groupName as a user property.
Future<void> setGroup(
{required String groupType,
required dynamic groupName,
EventOptions? options}) async {
Future<void> setGroup(String groupType, dynamic groupName,
[EventOptions? options]) async {
if (!(groupName is String) && !(groupName is List<String>)) {
// TODO(xinyi): log warn that groupName should be either a string or an array of string.
return;
}

final identify = Identify().set(property: groupType, value: groupName);
final identify = Identify().set(groupType, groupName);
final event = IdentifyEvent()
..groups = {groupType: groupName}
..userProperties = identify.properties;
Expand All @@ -153,7 +156,7 @@ class Amplitude {
event.mergeEventOptions(options);
}

return await _channel.invokeMethod("setGroup", event.toMap());
return await _channel.invokeMethod('setGroup', event.toMap());
}

/// Tracks revenue generated by a user.
Expand All @@ -163,55 +166,60 @@ class Amplitude {
/// final revenue = Revenue()
/// ..price = 3.99
/// ..quantity = 3
/// ..productId = "com.company.productId";
/// amplitude.revenue(revenue: revenue);
/// ..productId = 'com.company.productId';
/// amplitude.revenue(revenue);
/// ```
Future<void> revenue({
required Revenue revenue,
EventOptions? options,
}) async {
Future<void> revenue(Revenue revenue, [EventOptions? options]) async {
if (!revenue.isValid()) {
// TODO(xinyi): logger.warn("Invalid revenue object, missing required fields")
// TODO(xinyi): logger.warn('Invalid revenue object, missing required fields')
return;
}
final event = revenue.toRevenueEvent();
if (options != null) {
event.mergeEventOptions(options);
}

return await _channel.invokeMethod("revenue", event.toMap());
return await _channel.invokeMethod('revenue', event.toMap());
}

/// Set a custom user Id.
///
/// If your app has its own login system that you want to track users with,
/// you can set the userId.
///
/// ```
/// amplitude.setUserId('user Id');
/// ```
Future<void> setUserId(String? userId) async {
Map<String, String?> properties = {};
properties["setUserId"] = userId;
properties['setUserId'] = userId;

return await _channel.invokeMethod("setUserId", properties);
return await _channel.invokeMethod('setUserId', properties);
}

/// Sets a custom device ID.
///
/// Make sure the value is sufficiently unique. Amplitude recommends using a UUID.
///
/// ```
/// amplitude.setDeviceId('device Id');
/// ```
Future<void> setDeviceId(String deviceId) async {
Map<String, String?> properties = {};
properties["setDeviceId"] = deviceId;
properties['setDeviceId'] = deviceId;

return await _channel.invokeMethod("setDeviceId", properties);
return await _channel.invokeMethod('setDeviceId', properties);
}

/// Resets userId to "null" and deviceId to a random UUID.
/// 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 {
return await _channel.invokeMethod("reset");
return await _channel.invokeMethod('reset');
}

/// Flush events in storage.
Future<void> flush() async {
await await _channel.invokeMethod("flush");
await await _channel.invokeMethod('flush');
}
}
2 changes: 1 addition & 1 deletion lib/events/base_event.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:amplitude_flutter/events/plan.dart';
import 'plan.dart';
import 'event_options.dart';
import 'ingestion_metadata.dart';

Expand Down
3 changes: 1 addition & 2 deletions lib/events/event_options.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:core';

import 'package:amplitude_flutter/events/plan.dart';

import 'plan.dart';
import 'ingestion_metadata.dart';

/// Keys for event arguments
Expand Down
2 changes: 1 addition & 1 deletion lib/events/group_identify_event.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:amplitude_flutter/constants.dart';
import '../constants.dart';
import 'base_event.dart';

class GroupIdentifyEvent extends BaseEvent {
Expand Down
Loading

0 comments on commit 5b612af

Please sign in to comment.