diff --git a/aider/analytics.py b/aider/analytics.py index 4e535f62b..dfef61aa9 100644 --- a/aider/analytics.py +++ b/aider/analytics.py @@ -12,16 +12,45 @@ from aider.dump import dump # noqa: F401 class Analytics: - def __init__(self, track, logfile=None): + def __init__(self, track, logfile=None, disable=False): self.logfile = logfile - if not track: + self.disable = disable + if not track or disable: self.mp = None + if disable: + self.mark_as_disabled() return project_token = "6da9a43058a5d1b9f3353153921fb04d" self.mp = Mixpanel(project_token) if project_token else None self.user_id = self.get_or_create_uuid() + def mark_as_disabled(self): + uuid_file = Path.home() / ".aider" / "caches" / "mixpanel.json" + uuid_file.parent.mkdir(parents=True, exist_ok=True) + + data = {"uuid": str(uuid.uuid4()), "disabled": True} + with open(uuid_file, "w") as f: + json.dump(data, f) + + def get_or_create_uuid(self): + uuid_file = Path.home() / ".aider" / "caches" / "mixpanel.json" + uuid_file.parent.mkdir(parents=True, exist_ok=True) + + if uuid_file.exists(): + with open(uuid_file, "r") as f: + data = json.load(f) + if "disabled" in data and data["disabled"]: + self.disable = True + self.mp = None + return data["uuid"] + + new_uuid = str(uuid.uuid4()) + with open(uuid_file, "w") as f: + json.dump({"uuid": new_uuid}, f) + + return new_uuid + def get_system_info(self): return { "python_version": sys.version.split()[0], diff --git a/aider/args.py b/aider/args.py index 05b0073aa..00d5dd0b0 100644 --- a/aider/args.py +++ b/aider/args.py @@ -558,6 +558,12 @@ def get_parser(default_config_files, git_root): metavar="ANALYTICS_LOG_FILE", help="Specify a file to log analytics events", ) + group.add_argument( + "--analytics-disable", + action="store_true", + help="Disable analytics tracking and mark as disabled in mixpanel.json", + default=False, + ) return parser diff --git a/aider/main.py b/aider/main.py index 6c008a3e1..0cd860250 100644 --- a/aider/main.py +++ b/aider/main.py @@ -373,7 +373,7 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F editingmode=editing_mode, ) - analytics = Analytics(args.analytics, logfile=args.analytics_log) + analytics = Analytics(args.analytics, logfile=args.analytics_log, disable=args.analytics_disable) analytics.event("launched") if args.gui and not return_coder: