Skip to content

Commit

Permalink
fix: replace Google Analytics with PostHog (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrarafer authored Nov 30, 2023
1 parent 561e3a3 commit 0b553fc
Show file tree
Hide file tree
Showing 21 changed files with 448 additions and 300 deletions.
12 changes: 6 additions & 6 deletions bin/stacked.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/exceptions/invalid_stacked_structure_exception.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/pub_service.dart';

Future<void> main(List<String> arguments) async {
Expand Down Expand Up @@ -59,14 +59,14 @@ Future<void> main(List<String> arguments) async {
runner.run(arguments);
} on InvalidStackedStructureException catch (e) {
stdout.writeln(e.message);
locator<AnalyticsService>().logExceptionEvent(
locator<PosthogService>().logExceptionEvent(
runtimeType: e.runtimeType.toString(),
message: e.toString(),
);
exit(2);
} catch (e, s) {
stdout.writeln(e.toString());
locator<AnalyticsService>().logExceptionEvent(
locator<PosthogService>().logExceptionEvent(
runtimeType: e.runtimeType.toString(),
message: e.toString(),
stackTrace: s.toString(),
Expand All @@ -83,12 +83,12 @@ Future<void> _handleVersion() async {
/// Enables or disables sending of analytics data.
bool _handleAnalytics(ArgResults argResults) {
if (argResults[ksEnableAnalytics]) {
locator<AnalyticsService>().enable(true);
locator<PosthogService>().enable(true);
return true;
}

if (argResults[ksDisableAnalytics]) {
locator<AnalyticsService>().enable(false);
locator<PosthogService>().enable(false);
return true;
}

Expand All @@ -97,7 +97,7 @@ bool _handleAnalytics(ArgResults argResults) {

/// Allows user decide to enable or not analytics on first run.
Future<void> _handleFirstRun() async {
final analyticsService = locator<AnalyticsService>();
final analyticsService = locator<PosthogService>();
if (!analyticsService.isFirstRun) return;

stdout.writeln('''
Expand Down
9 changes: 6 additions & 3 deletions lib/src/commands/create/create_app_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/config_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/file_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
import 'package:stacked_cli/src/templates/compiled_constants.dart';
import 'package:stacked_cli/src/templates/template_constants.dart';
import 'package:stacked_cli/src/templates/template_helper.dart';

class CreateAppCommand extends Command {
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();
final _configService = locator<ConfigService>();
final _fileService = locator<FileService>();
final _log = locator<ColorizedLogService>();
Expand Down Expand Up @@ -112,7 +112,10 @@ class CreateAppCommand extends Command {
await _processService.runBuildRunner(workingDirectory: workingDirectory);
await _processService.runFormat(appName: workingDirectory);
await _clean(workingDirectory: workingDirectory);
unawaited(_analyticsService.createAppEvent(name: appName));
await _analyticsService.createAppEvent(
name: appName,
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
Expand Down
9 changes: 5 additions & 4 deletions lib/src/commands/create/create_bottom_sheet_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
Expand All @@ -20,7 +20,7 @@ class CreateBottomSheetCommand extends Command with ProjectStructureValidator {
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();

@override
String get description =>
Expand Down Expand Up @@ -87,8 +87,9 @@ class CreateBottomSheetCommand extends Command with ProjectStructureValidator {
);

await _processService.runBuildRunner(workingDirectory: workingDirectory);
unawaited(
_analyticsService.createBottomSheetEvent(name: bottomSheetName),
await _analyticsService.createBottomSheetEvent(
name: bottomSheetName,
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
Expand Down
9 changes: 6 additions & 3 deletions lib/src/commands/create/create_dialog_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
Expand All @@ -20,7 +20,7 @@ class CreateDialogCommand extends Command with ProjectStructureValidator {
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();

@override
String get description =>
Expand Down Expand Up @@ -87,7 +87,10 @@ class CreateDialogCommand extends Command with ProjectStructureValidator {
);

await _processService.runBuildRunner(workingDirectory: workingDirectory);
unawaited(_analyticsService.createDialogEvent(name: dialogName));
await _analyticsService.createDialogEvent(
name: dialogName,
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
Expand Down
9 changes: 6 additions & 3 deletions lib/src/commands/create/create_service_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
Expand All @@ -20,7 +20,7 @@ class CreateServiceCommand extends Command with ProjectStructureValidator {
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();

@override
String get description =>
Expand Down Expand Up @@ -80,7 +80,10 @@ class CreateServiceCommand extends Command with ProjectStructureValidator {
templateType: templateType,
);
await _processService.runBuildRunner(workingDirectory: workingDirectory);
unawaited(_analyticsService.createServiceEvent(name: serviceName));
await _analyticsService.createServiceEvent(
name: serviceName,
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
Expand Down
9 changes: 6 additions & 3 deletions lib/src/commands/create/create_view_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
Expand All @@ -20,7 +20,7 @@ class CreateViewCommand extends Command with ProjectStructureValidator {
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();

@override
String get description =>
Expand Down Expand Up @@ -93,7 +93,10 @@ class CreateViewCommand extends Command with ProjectStructureValidator {
templateType: templateType,
);
await _processService.runBuildRunner(workingDirectory: workingDirectory);
unawaited(_analyticsService.createViewEvent(name: viewName));
await _analyticsService.createViewEvent(
name: viewName,
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
Expand Down
9 changes: 6 additions & 3 deletions lib/src/commands/create/create_widget_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
Expand All @@ -20,7 +20,7 @@ class CreateWidgetCommand extends Command with ProjectStructureValidator {
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();

@override
String get description => 'Creates a widget with their model file.';
Expand Down Expand Up @@ -89,7 +89,10 @@ class CreateWidgetCommand extends Command with ProjectStructureValidator {
templateType: templateType,
);

unawaited(_analyticsService.createWidgetEvent(name: widgetName));
await _analyticsService.createWidgetEvent(
name: widgetName,
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
Expand Down
9 changes: 5 additions & 4 deletions lib/src/commands/delete/delete_dialog_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/file_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
Expand All @@ -23,7 +23,7 @@ class DeleteDialogCommand extends Command with ProjectStructureValidator {
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();

@override
String get description =>
Expand Down Expand Up @@ -69,8 +69,9 @@ class DeleteDialogCommand extends Command with ProjectStructureValidator {
await _removeDialogFromDependency(
outputPath: workingDirectory, dialogName: dialogName);
await _processService.runBuildRunner(workingDirectory: workingDirectory);
unawaited(
_analyticsService.deleteDialogEvent(name: argResults!.rest.first),
await _analyticsService.deleteDialogEvent(
name: argResults!.rest.first,
arguments: argResults!.arguments,
);
} on PathNotFoundException catch (e) {
_log.error(message: e.toString());
Expand Down
9 changes: 5 additions & 4 deletions lib/src/commands/delete/delete_service_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/file_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
Expand All @@ -22,7 +22,7 @@ class DeleteServiceCommand extends Command with ProjectStructureValidator {
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();

@override
String get description =>
Expand Down Expand Up @@ -71,9 +71,10 @@ class DeleteServiceCommand extends Command with ProjectStructureValidator {
await _removeServiceFromDependency(
outputPath: workingDirectory, serviceName: serviceName);
await _processService.runBuildRunner(workingDirectory: workingDirectory);
unawaited(_analyticsService.deleteServiceEvent(
await _analyticsService.deleteServiceEvent(
name: argResults!.rest.first,
));
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
Expand Down
9 changes: 5 additions & 4 deletions lib/src/commands/delete/delete_view_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/mixins/project_structure_validator_mixin.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/config_service.dart';
import 'package:stacked_cli/src/services/file_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pubspec_service.dart';
import 'package:stacked_cli/src/services/template_service.dart';
Expand All @@ -22,7 +22,7 @@ class DeleteViewCommand extends Command with ProjectStructureValidator {
final _processService = locator<ProcessService>();
final _pubspecService = locator<PubspecService>();
final _templateService = locator<TemplateService>();
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();

@override
String get description =>
Expand Down Expand Up @@ -69,8 +69,9 @@ class DeleteViewCommand extends Command with ProjectStructureValidator {
await _removeViewFromRoute(
outputPath: workingDirectory, viewName: viewName);
await _processService.runBuildRunner(workingDirectory: workingDirectory);
unawaited(
_analyticsService.deleteViewEvent(name: argResults!.rest.first),
await _analyticsService.deleteViewEvent(
name: argResults!.rest.first,
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
Expand Down
8 changes: 5 additions & 3 deletions lib/src/commands/generate/generate_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import 'package:args/command_runner.dart';
import 'package:stacked_cli/src/constants/command_constants.dart';
import 'package:stacked_cli/src/constants/message_constants.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/templates/template_constants.dart';

class GenerateCommand extends Command {
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();
final _log = locator<ColorizedLogService>();
final _processService = locator<ProcessService>();

Expand Down Expand Up @@ -45,7 +45,9 @@ class GenerateCommand extends Command {
shouldDeleteConflictingOutputs: argResults?[ksDeleteConflictOutputs],
shouldWatch: argResults?[ksWatch],
);
unawaited(_analyticsService.generateCodeEvent());
await _analyticsService.generateCodeEvent(
arguments: argResults!.arguments,
);
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
Expand Down
6 changes: 3 additions & 3 deletions lib/src/commands/update/update_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import 'dart:async';

import 'package:args/command_runner.dart';
import 'package:stacked_cli/src/locator.dart';
import 'package:stacked_cli/src/services/analytics_service.dart';
import 'package:stacked_cli/src/services/colorized_log_service.dart';
import 'package:stacked_cli/src/services/posthog_service.dart';
import 'package:stacked_cli/src/services/process_service.dart';
import 'package:stacked_cli/src/services/pub_service.dart';

class UpdateCommand extends Command {
final _analyticsService = locator<AnalyticsService>();
final _analyticsService = locator<PosthogService>();
final _log = locator<ColorizedLogService>();
final _processService = locator<ProcessService>();
final _pubService = locator<PubService>();
Expand All @@ -25,7 +25,7 @@ class UpdateCommand extends Command {
if (await _pubService.hasLatestVersion()) return;

await _processService.runPubGlobalActivate();
unawaited(_analyticsService.updateCliEvent());
await _analyticsService.updateCliEvent();
} catch (e, s) {
_log.error(message: e.toString());
unawaited(_analyticsService.logExceptionEvent(
Expand Down
Loading

0 comments on commit 0b553fc

Please sign in to comment.