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