Skip to content

Commit d3473ad

Browse files
zhu-xiaoweizhu-xiaowei
and
zhu-xiaowei
authored
feat: support disable and enable sdk (#2)
Co-authored-by: zhu-xiaowei <[email protected]>
1 parent 927af3b commit d3473ad

10 files changed

+119
-12
lines changed

README.md

+24-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Clickstream Flutter SDK can help you easily collect and report events from your
66

77
The SDK relies on the [Clickstream Android SDK](https://github.com/awslabs/clickstream-android) and [Clickstream Swift SDK](https://github.com/awslabs/clickstream-swift). Therefore, flutter SDK also supports automatically collect common user events and attributes (e.g., session start, first open). In addition, we've added easy-to-use APIs to simplify data collection in Flutter apps.
88

9+
Visit our [Documentation site](https://awslabs.github.io/clickstream-analytics-on-aws/en/sdk-manual/flutter/) to learn more about Clickstream Flutter SDK.
10+
911
## Integrate SDK
1012

1113
### Include SDK
@@ -81,7 +83,7 @@ analytics.setUserAttributes({
8183
});
8284
```
8385

84-
When opening for the first time after integrating the SDK, you need to manually set the user attributes once, and current login user's attributes will be cached in native disk, so the next time browser open you don't need to set all user's attribute again, of course you can use the same api `analytics.setUserAttributes()` to update the current user's attribute when it changes.
86+
Current login user's attributes will be cached in disk, so the next time app launch you don't need to set all user's attribute again, of course you can use the same api `analytics.setUserAttributes()` to update the current user's attribute when it changes.
8587

8688
#### Add global attribute
8789

@@ -91,6 +93,9 @@ analytics.addGlobalAttributes({
9193
"_traffic_source_name": "Summer promotion",
9294
"level": 10
9395
});
96+
97+
// delete global attribute
98+
analytics.deleteGlobalAttributes(["level"]);
9499
```
95100

96101
It is recommended to set global attributes after each SDK initialization, global attributes will be included in all events that occur after it is set.
@@ -106,7 +111,7 @@ analytics.init(
106111
endpoint: "https://example.com/collect",
107112
isLogEvents: false,
108113
isCompressEvents: false,
109-
sendEventsInterval: 5000,
114+
sendEventsInterval: 10000,
110115
isTrackScreenViewEvents: true,
111116
isTrackUserEngagementEvents: true,
112117
isTrackAppExceptionEvents: false,
@@ -115,10 +120,10 @@ analytics.init(
115120
);
116121
```
117122

118-
Here is an explanation of each property:
123+
Here is an explanation of each option:
119124

120125
- **appId (Required)**: the app id of your project in control plane.
121-
- **endpoint (Required)**: the endpoint path you will upload the event to AWS server.
126+
- **endpoint (Required)**: the endpoint path you will upload the event to Clickstream ingestion server.
122127
- **isLogEvents**: whether to print out event json for debugging, default is false.
123128
- **isCompressEvents**: whether to compress event content when uploading events, default is `true`
124129
- **sendEventsInterval**: event sending interval millisecond, works only bath send mode, the default value is `5000`
@@ -153,6 +158,21 @@ final analytics = ClickstreamAnalytics();
153158
analytics.flushEvents();
154159
```
155160

161+
#### Disable SDK
162+
163+
You can disable the SDK in the scenario you need. After disabling the SDK, the SDK will not handle the logging and
164+
sending of any events. Of course, you can enable the SDK when you need to continue logging events.
165+
166+
```dart
167+
final analytics = ClickstreamAnalytics();
168+
169+
// disable SDK
170+
analytics.disable();
171+
172+
// enable SDK
173+
analytics.enable();
174+
```
175+
156176
## How to build and test locally
157177

158178
### Build

android/src/main/kotlin/software/aws/solution/clickstream_analytics/ClickstreamFlutterPlugin.kt

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ class ClickstreamFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware
9595
ClickstreamAnalytics.flushEvents()
9696
}
9797

98+
"disable" -> {
99+
ClickstreamAnalytics.disable()
100+
}
101+
102+
"enable" -> {
103+
ClickstreamAnalytics.enable()
104+
}
105+
98106
else -> {
99107
result.notImplemented()
100108
}

example/lib/main.dart

+18
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ class _MyAppState extends State<MyApp> {
142142
},
143143
minLeadingWidth: 0,
144144
),
145+
ListTile(
146+
leading: const Icon(Icons.disabled_by_default),
147+
title: const Text('disable'),
148+
onTap: () async {
149+
analytics.disable();
150+
log("disable");
151+
},
152+
minLeadingWidth: 0,
153+
),
154+
ListTile(
155+
leading: const Icon(Icons.check),
156+
title: const Text('enable'),
157+
onTap: () async {
158+
analytics.enable();
159+
log("enable");
160+
},
161+
minLeadingWidth: 0,
162+
),
145163
],
146164
),
147165
),

ios/Classes/ClickstreamFlutterPlugin.swift

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class ClickstreamFlutterPlugin: NSObject, FlutterPlugin {
3636
updateConfigure(call.arguments as! [String: Any])
3737
case "flushEvents":
3838
ClickstreamAnalytics.flushEvents()
39+
case "disable":
40+
ClickstreamAnalytics.disable()
41+
case "enable":
42+
ClickstreamAnalytics.enable()
3943
default:
4044
result(FlutterMethodNotImplemented)
4145
}

ios/Clickstream

lib/clickstream_analytics.dart

+8
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ class ClickstreamAnalytics {
8585
Future<void> flushEvents() {
8686
return ClickstreamInterface.instance.flushEvents();
8787
}
88+
89+
Future<void> disable() {
90+
return ClickstreamInterface.instance.disable();
91+
}
92+
93+
Future<void> enable() {
94+
return ClickstreamInterface.instance.enable();
95+
}
8896
}

lib/clickstream_analytics_method_channel.dart

+10
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ class ClickstreamAnalyticsMethodChannel extends ClickstreamInterface {
5353
Future<void> flushEvents() async {
5454
await methodChannel.invokeMethod<void>('flushEvents');
5555
}
56+
57+
@override
58+
Future<void> disable() async {
59+
await methodChannel.invokeMethod<void>('disable');
60+
}
61+
62+
@override
63+
Future<void> enable() async {
64+
await methodChannel.invokeMethod<void>('enable');
65+
}
5666
}

lib/clickstream_analytics_platform_interface.dart

+14-5
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,31 @@ abstract class ClickstreamInterface extends PlatformInterface {
3939
}
4040

4141
Future<void> setUserAttributes(Map<String, Object?> attributes) {
42-
throw UnimplementedError('setUserId() has not been implemented.');
42+
throw UnimplementedError('setUserAttributes() has not been implemented.');
4343
}
4444

4545
Future<void> addGlobalAttributes(Map<String, Object?> attributes) {
46-
throw UnimplementedError('setUserId() has not been implemented.');
46+
throw UnimplementedError('addGlobalAttributes() has not been implemented.');
4747
}
4848

4949
Future<void> deleteGlobalAttributes(Map<String, Object?> attributes) {
50-
throw UnimplementedError('setUserId() has not been implemented.');
50+
throw UnimplementedError(
51+
'deleteGlobalAttributes() has not been implemented.');
5152
}
5253

5354
Future<void> updateConfigure(Map<String, Object?> configure) {
54-
throw UnimplementedError('setUserId() has not been implemented.');
55+
throw UnimplementedError('updateConfigure() has not been implemented.');
5556
}
5657

5758
Future<void> flushEvents() {
58-
throw UnimplementedError('setUserId() has not been implemented.');
59+
throw UnimplementedError('flushEvents() has not been implemented.');
60+
}
61+
62+
Future<void> disable() {
63+
throw UnimplementedError('disable() has not been implemented.');
64+
}
65+
66+
Future<void> enable() {
67+
throw UnimplementedError('enable() has not been implemented.');
5968
}
6069
}

test/clickstream_flutter_method_channel_test.dart

+16-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ void main() {
3838
return null;
3939
case "flushEvents":
4040
return null;
41+
case "disable":
42+
return null;
43+
case "enable":
44+
return null;
4145
}
4246
return null;
4347
},
@@ -89,7 +93,7 @@ void main() {
8993
expect(result, isNotNull);
9094
});
9195

92-
test('setGlobalAttributes', () async {
96+
test('addGlobalAttributes', () async {
9397
Map<String, Object?> attributes = {
9498
"channel": "Play Store",
9599
"level": 5.1,
@@ -116,8 +120,18 @@ void main() {
116120
expect(result, isNotNull);
117121
});
118122

119-
test('setGlobalAttributes', () async {
123+
test('flushEvents', () async {
120124
var result = platform.flushEvents();
121125
expect(result, isNotNull);
122126
});
127+
128+
test('disable', () async {
129+
var result = platform.disable();
130+
expect(result, isNotNull);
131+
});
132+
133+
test('enable', () async {
134+
var result = platform.enable();
135+
expect(result, isNotNull);
136+
});
123137
}

test/clickstream_flutter_test.dart

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class MockClickstreamFlutterPlatform
3737
@override
3838
Future<void> deleteGlobalAttributes(Map<String, Object?> attributes) =>
3939
Future.value();
40+
41+
@override
42+
Future<void> disable() => Future.value();
43+
44+
@override
45+
Future<void> enable() => Future.value();
4046
}
4147

4248
void main() {
@@ -117,4 +123,14 @@ void main() {
117123
var result = analytics.flushEvents();
118124
expect(result, isNotNull);
119125
});
126+
127+
test('disable', () async {
128+
var result = analytics.disable();
129+
expect(result, isNotNull);
130+
});
131+
132+
test('enable', () async {
133+
var result = analytics.enable();
134+
expect(result, isNotNull);
135+
});
120136
}

0 commit comments

Comments
 (0)