From 539a6cde634780e690cc02e20f7ef4c65082bac9 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 20 Nov 2024 16:22:01 -0800 Subject: [PATCH] feat: Enhance analytics opt-in logic with user sampling and explicit control --- aider/analytics.py | 17 +++++++++++++++-- aider/args.py | 4 ++-- aider/main.py | 4 ++-- tests/basic/test_analytics.py | 13 ++++++++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/aider/analytics.py b/aider/analytics.py index 5a0ace4b3..464f98f57 100644 --- a/aider/analytics.py +++ b/aider/analytics.py @@ -62,8 +62,21 @@ class Analytics: self.permanently_disable = True self.save_data() - def need_to_ask(self): - return not self.asked_opt_in and not self.permanently_disable + def need_to_ask(self, args_analytics): + if args_analytics is False: + return False + + could_ask = not self.asked_opt_in and not self.permanently_disable + if not could_ask: + return False + + if args_analytics is True: + return True + + assert args_analytics is None, args_analytics + + # ask 1/16 of the users + return self.user_id < "1" def get_data_file_path(self): data_file = Path.home() / ".aider" / "analytics.json" diff --git a/aider/args.py b/aider/args.py index 2a266f303..ec2545c8c 100644 --- a/aider/args.py +++ b/aider/args.py @@ -562,8 +562,8 @@ def get_parser(default_config_files, git_root): group.add_argument( "--analytics", action=argparse.BooleanOptionalAction, - default=False, - help="Enable/disable analytics for one session (default: False)", + default=None, + help="Enable/disable analytics for current session (default: random)", ) group.add_argument( "--analytics-log", diff --git a/aider/main.py b/aider/main.py index 89d01d72c..d38f3cc20 100644 --- a/aider/main.py +++ b/aider/main.py @@ -506,8 +506,8 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F io.tool_warning("Terminal does not support pretty output (UnicodeDecodeError)") analytics = Analytics(logfile=args.analytics_log, permanently_disable=args.analytics_disable) - if args.analytics: - if analytics.need_to_ask(): + if args.analytics is not False: + if analytics.need_to_ask(args.analytics): io.tool_output( "Aider respects your privacy and never collects your code, chat messages, keys or" " personal info." diff --git a/tests/basic/test_analytics.py b/tests/basic/test_analytics.py index 11071cf50..baeb21ddc 100644 --- a/tests/basic/test_analytics.py +++ b/tests/basic/test_analytics.py @@ -91,10 +91,17 @@ def test_system_info(temp_data_dir): def test_need_to_ask(temp_data_dir): analytics = Analytics() - assert analytics.need_to_ask() is True + assert analytics.need_to_ask(True) is True + assert analytics.need_to_ask(False) is False + + analytics.user_id = "111" + assert analytics.need_to_ask(None) is False + + analytics.user_id = "000" + assert analytics.need_to_ask(None) is True analytics.asked_opt_in = True - assert analytics.need_to_ask() is False + assert analytics.need_to_ask(True) is False analytics.permanently_disable = True - assert analytics.need_to_ask() is False + assert analytics.need_to_ask(True) is False