Skip to content

Commit f26d3c4

Browse files
edsonjabnd4p90x
andauthored
Feature - Add puglin to support Amplitude (#109)
Co-authored-by: Shane L. Duvall <[email protected]>
1 parent 62fb947 commit f26d3c4

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
26+
/pubspec.lock
27+
**/doc/api/
28+
.dart_tool/
29+
.packages
30+
build/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: "761747bfc538b5af34aa0d3fac380f1bc331ec49"
8+
channel: "stable"
9+
10+
project_type: app
11+
12+
# Tracks metadata for the flutter migrate command
13+
migration:
14+
platforms:
15+
- platform: root
16+
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
17+
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
18+
- platform: android
19+
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
20+
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
21+
- platform: ios
22+
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
23+
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
24+
- platform: linux
25+
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
26+
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
27+
- platform: macos
28+
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
29+
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
30+
- platform: web
31+
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
32+
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
33+
- platform: windows
34+
create_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
35+
base_revision: 761747bfc538b5af34aa0d3fac380f1bc331ec49
36+
37+
# User provided section
38+
39+
# List of Local paths (relative to this file) that should be
40+
# ignored by the migrate tool.
41+
#
42+
# Files that are not part of the templates will be ignored by default.
43+
unmanaged_files:
44+
- 'lib/main.dart'
45+
- 'ios/Runner.xcodeproj/project.pbxproj'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# segment_analytics_plugin_amplitude
2+
3+
`DestinationPlugin` for [amplitude](https://amplitude.com/docs/sdks/sdk-quickstart). Wraps [`amplitude_analytics`](https://pub.dev/packages/amplitude_analytics).
4+
5+
## Installation
6+
7+
Manually add this package to your `pubspec.yaml` file.
8+
9+
```yaml
10+
dependencies:
11+
analytics_plugin_amplitude:
12+
git:
13+
url: https://github.com/segmentio/analytics_flutter
14+
ref: main
15+
path: packages/plugins/plugin_amplitude
16+
```
17+
18+
## Usage
19+
20+
Follow the [instructions for adding plugins](https://github.com/segmentio/analytics_flutter_#adding-plugins) on the main Analytics client:
21+
22+
In your code where you initialize the analytics client call the `.add(plugin)` method with an `amplitudeDestination` instance.
23+
24+
```dart
25+
import 'amplitude_options.dart';
26+
import 'package:segment_analytics/client.dart';
27+
import 'package:segment_analytics_plugin_amplitude/plugin_amplitude.dart'
28+
show amplitudeDestination;
29+
30+
const writeKey = 'SEGMENT_API_KEY';
31+
32+
class _MyAppState extends State<MyApp> {
33+
final analytics = createClient(Configuration(writeKey));
34+
35+
@override
36+
void initState() {
37+
// ...
38+
39+
analytics
40+
.addPlugin(amplitudeDestination());
41+
}
42+
}
43+
```
44+
45+
## Support
46+
47+
Please use Github issues, Pull Requests, or feel free to reach out to our [support team](https://segment.com/help/).
48+
49+
## Integrating with Segment
50+
51+
Interested in integrating your service with us? Check out our [Partners page](https://segment.com/partners/) for more details.
52+
53+
## License
54+
55+
```
56+
MIT License
57+
58+
Copyright (c) 2023 Segment
59+
60+
Permission is hereby granted, free of charge, to any person obtaining a copy
61+
of this software and associated documentation files (the "Software"), to deal
62+
in the Software without restriction, including without limitation the rights
63+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
64+
copies of the Software, and to permit persons to whom the Software is
65+
furnished to do so, subject to the following conditions:
66+
67+
The above copyright notice and this permission notice shall be included in all
68+
copies or substantial portions of the Software.
69+
70+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
73+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
75+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
76+
SOFTWARE.
77+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include: package:flutter_lints/flutter.yaml
2+
3+
# Additional information about this file can be found at
4+
# https://dart.dev/guides/language/analysis-options
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'package:segment_analytics/analytics.dart';
2+
import 'package:segment_analytics/event.dart';
3+
import 'package:segment_analytics/plugin.dart';
4+
import 'package:amplitude_flutter/identify.dart';
5+
import 'package:amplitude_flutter/amplitude.dart';
6+
7+
class AmplitudePlugin extends DestinationPlugin {
8+
final String apiKey;
9+
final String instanceName;
10+
late Amplitude _amplitudeInstance;
11+
12+
AmplitudePlugin(this.apiKey, this.instanceName) : super('amplitude') {
13+
_amplitudeInstance = Amplitude.getInstance(instanceName: instanceName);
14+
}
15+
16+
@override
17+
void configure(Analytics analytics) {
18+
super.configure(analytics);
19+
_amplitudeInstance.init(apiKey);
20+
}
21+
22+
@override
23+
Future<RawEvent?> track(TrackEvent event) async {
24+
await _amplitudeInstance.logEvent(event.event, eventProperties: event.properties);
25+
return event;
26+
}
27+
28+
@override
29+
Future<RawEvent?> identify(IdentifyEvent event) async {
30+
await _amplitudeInstance.setUserId(event.userId);
31+
if (event.traits != null) {
32+
final Identify identify = Identify();
33+
await Future.wait(event.traits!.toJson().entries.map((entry) async {
34+
identify.set(entry.key, entry.value.toString());
35+
}));
36+
_amplitudeInstance.identify(identify);
37+
}
38+
return event;
39+
}
40+
41+
@override
42+
Future flush() async {
43+
_amplitudeInstance.uploadEvents();
44+
}
45+
46+
@override
47+
void reset() {
48+
_amplitudeInstance.setUserId(null);
49+
_amplitudeInstance.clearUserProperties();
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: segment_analytics_plugin_amplitude
2+
description: "amplitude plugin."
3+
version: 1.0.0
4+
homepage: https://github.com/segmentio/analytics_flutter#readme
5+
repository: https://github.com/segmentio/analytics_flutter/tree/main/packages/plugins/plugin_amplitude#readme
6+
issue_tracker: https://github.com/segmentio/analytics_flutter/issues
7+
8+
environment:
9+
sdk: '>=2.19.3 <4.0.0'
10+
flutter: ">=1.17.0"
11+
12+
dependencies:
13+
flutter:
14+
sdk: flutter
15+
segment_analytics: ^1.0.0
16+
json_annotation: ^4.8.0
17+
amplitude_flutter: ^3.13.0
18+
19+
dev_dependencies:
20+
build_runner: ^2.3.3
21+
json_serializable: ^6.6.0
22+
flutter_test:
23+
sdk: flutter
24+
flutter_lints: ^2.0.0
25+
26+
flutter:
27+

0 commit comments

Comments
 (0)