Skip to content

Commit

Permalink
Enable user-level tracking if user allow permission (#33)
Browse files Browse the repository at this point in the history
* Enable user-level tracking if user allow permission

* improve install event tracking
  • Loading branch information
james03160927 authored May 8, 2024
1 parent 550f455 commit b980181
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions comfy_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class OS(Enum):
CONFIG_KEY_DEFAULT_LAUNCH_EXTRAS = "default_launch_extras"
CONFIG_KEY_RECENT_WORKSPACE = "recent_workspace"
CONFIG_KEY_ENABLE_TRACKING = "enable_tracking"
CONFIG_KEY_USER_ID = "user_id"
CONFIG_KEY_INSTALL_EVENT_TRIGGERED = "install_event_triggered"
CONFIG_KEY_BACKGROUND = "background"

COMFY_LOCK_YAML_FILE = "comfy.lock.yaml"
Expand Down
35 changes: 32 additions & 3 deletions comfy_cli/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
# Generate a unique tracing ID per command.
config_manager = ConfigManager()
cli_version = config_manager.get_cli_version()

# tracking all events for a single user
user_id = config_manager.get(constants.CONFIG_KEY_USER_ID)
# tracking all events for a single command
tracing_id = str(uuid.uuid4())

app = typer.Typer()
Expand All @@ -27,6 +31,7 @@
def enable():
set_tracking_enabled(True)
typer.echo(f"Tracking is now {'enabled' if enable else 'disabled'}.")
init_tracking(True)


@app.command()
Expand All @@ -35,7 +40,7 @@ def disable():
typer.echo(f"Tracking is now {'enabled' if enable else 'disabled'}.")


def track_event(event_name: str, properties: any = None):
def track_event(event_name: str, properties: any = {}):
logging.debug(
f"tracking event called with event_name: {event_name} and properties: {properties}"
)
Expand All @@ -45,7 +50,8 @@ def track_event(event_name: str, properties: any = None):

try:
properties["cli_version"] = cli_version
mp.track(distinct_id=tracing_id, event_name=event_name, properties=properties)
properties["tracing_id"] = tracing_id
mp.track(distinct_id=user_id, event_name=event_name, properties=properties)
except Exception as e:
logging.warning(f"Failed to track event: {e}") # Log the error but do not raise

Expand Down Expand Up @@ -90,11 +96,34 @@ def prompt_tracking_consent():
enable_tracking = ui.prompt_confirm_action(
"Do you agree to enable tracking to improve the application?"
)
init_tracking(enable_tracking)


def init_tracking(enable_tracking: bool):
"""
Initialize the tracking system by setting the user identifier and tracking enabled status.
"""
logging.debug(f"Initializing tracking with enable_tracking: {enable_tracking}")
config_manager.set(constants.CONFIG_KEY_ENABLE_TRACKING, str(enable_tracking))
if not enable_tracking:
return

user_id = config_manager.get(constants.CONFIG_KEY_USER_ID)
logging.debug(f'User identifier for tracking user_id found: {user_id}."')
if user_id is None:
user_id = str(uuid.uuid4())
config_manager.set(constants.CONFIG_KEY_USER_ID, user_id)
logging.debug(f'Setting user identifier for tracking user_id: {user_id}."')

# Note: only called once when the user interacts with the CLI for the
# first time iff the permission is granted.
track_event("install")
install_event_triggered = config_manager.get(
constants.CONFIG_KEY_INSTALL_EVENT_TRIGGERED
)
if not install_event_triggered:
logging.debug("Tracking install event.")
config_manager.set(constants.CONFIG_KEY_INSTALL_EVENT_TRIGGERED, "True")
track_event("install")


def set_tracking_enabled(enabled: bool):
Expand Down

0 comments on commit b980181

Please sign in to comment.