analytics_event 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 80

analytics_events_gen #

An easy generator for tracking firebase analytics events via type safe methods.

Add to pubspec.yaml #

Check pub for the latest version: Pub Pub

dependencies:
  # ...
  analytics_event: 0.1.0
dev_dependencies:
  analytics_event_gen: 0.1.0
  # include build_runner, only used for code generation.
  build_runner: ^1.6.5

Create AnalyticsEvents class: #

// analytics.dart

import 'package:analytics_event/analytics_event.dart';

// this file will be generated.
part 'analytics.g.dart';

class MyAnalyticsBloc {
  MyAnalyticsBloc() {
    // initialize generated events class implementation.
    // the generated code will simply transform the method name
    // into an `eventName` and pass it to your callback method.
    // you can then do whatever you want with it, e.g. send to 
    // firebase analytics.
    events.registerTracker((eventName, params) {
      FirebaseAnalytics().logEvent(name: event, parameters: params);
    });
  }
  
  final events = _$AnalyticsEvents();
}

abstract class AnalyticsEvents implements AnalyticsEventStubs {
  void trackMyUserInteraction({double myProp, String yourProp});
}

Run the build generator #

# For flutter projects
flutter pub pub run build_runner build --delete-conflicting-outputs

# For dart projects
pub run build_runner build --delete-conflicting-outputs

[0.1.2] #

  • Make sure during initialization no events are lost.

[0.1.1] #

  • Separate analytics_event from the source generator analytics_event_gen

[0.1.0+1] #

  • Added example, more documentation.

[0.1.0] #

  • Filter out abstract methods.

[0.0.1] - Initial Release #

example/lib/example.dart

import 'package:analytics_event/analytics_event.dart';
import 'package:logging/logging.dart';
import 'package:logging_appenders/logging_appenders.dart';

part 'example.g.dart';

final _logger = Logger('example');

/// Create an abstract class which implements [AnalyticsEventStubs]
/// and has stub methods for all events you want to track.
abstract class Events implements AnalyticsEventStubs {
  void trackAppLaunch({String date});
}

/// A analytics service which is responsible for sending events to
/// your analytics provider (e.g. firebase analytics).
class AnalyticsService {
  AnalyticsService() {
    // Instantiate the generated Events implementation.
    events = _$Events()..registerTracker(_trackEvent);
  }

  Events events;

  void _trackEvent(String event, Map<String, dynamic> params) {
    // Here you would send the event to your analytics service.
    _logger.info('We have to track event $event with parameters: $params');
  }

  void dispose() => events.removeTracker(_trackEvent);
}

void main() {
  Logger.root.level = Level.ALL;
  PrintAppender().attachToLogger(Logger.root);

  final myAnalytics = AnalyticsService();

  // Now you have a typesafe way to send events to your analytics service.
  myAnalytics.events.trackAppLaunch(date: DateTime.now().toString());

  myAnalytics.dispose();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  analytics_event: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:analytics_event/analytics_event.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
61
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
80
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
Dev dependencies
build_runner ^1.8.1
pedantic ^1.9.0