|
| 1 | +"""Enhanced measurement settings commands for a data stream.""" |
| 2 | + |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import typer |
| 6 | + |
| 7 | +from ..api.client import get_admin_alpha_client |
| 8 | +from ..config.store import get_effective_value |
| 9 | +from ..utils import ( |
| 10 | + handle_dry_run, |
| 11 | + handle_error, |
| 12 | + output, |
| 13 | + require_options, |
| 14 | + resolve_output_format, |
| 15 | + success, |
| 16 | +) |
| 17 | + |
| 18 | +enhanced_measurement_app = typer.Typer( |
| 19 | + name="enhanced-measurement", |
| 20 | + help="Manage enhanced measurement settings for a data stream", |
| 21 | + no_args_is_help=True, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def _resource_name(property_id: str, stream_id: str) -> str: |
| 26 | + return ( |
| 27 | + f"properties/{property_id}/dataStreams/{stream_id}" |
| 28 | + "/enhancedMeasurementSettings" |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@enhanced_measurement_app.command("get") |
| 33 | +def get_cmd( |
| 34 | + property_id: Optional[str] = typer.Option( |
| 35 | + None, "--property-id", "-p", help="Property ID (numeric)" |
| 36 | + ), |
| 37 | + stream_id: str = typer.Option( |
| 38 | + ..., "--stream-id", "-s", help="Data stream ID" |
| 39 | + ), |
| 40 | + output_format: Optional[str] = typer.Option( |
| 41 | + None, "--output", "-o", help="Output format (json, table, compact)" |
| 42 | + ), |
| 43 | +): |
| 44 | + """Get enhanced measurement settings for a data stream.""" |
| 45 | + try: |
| 46 | + effective_property = get_effective_value(property_id, "default_property_id") |
| 47 | + require_options({"property_id": effective_property}, ["property_id"]) |
| 48 | + effective_format = resolve_output_format(output_format) |
| 49 | + |
| 50 | + admin = get_admin_alpha_client() |
| 51 | + settings = ( |
| 52 | + admin.properties() |
| 53 | + .dataStreams() |
| 54 | + .getEnhancedMeasurementSettings( |
| 55 | + name=_resource_name(effective_property, stream_id) |
| 56 | + ) |
| 57 | + .execute() |
| 58 | + ) |
| 59 | + output(settings, effective_format) |
| 60 | + except Exception as e: |
| 61 | + handle_error(e) |
| 62 | + |
| 63 | + |
| 64 | +@enhanced_measurement_app.command("update") |
| 65 | +def update_cmd( |
| 66 | + property_id: Optional[str] = typer.Option( |
| 67 | + None, "--property-id", "-p", help="Property ID (numeric)" |
| 68 | + ), |
| 69 | + stream_id: str = typer.Option( |
| 70 | + ..., "--stream-id", "-s", help="Data stream ID" |
| 71 | + ), |
| 72 | + stream_enabled: Optional[bool] = typer.Option( |
| 73 | + None, |
| 74 | + "--stream-enabled/--no-stream-enabled", |
| 75 | + help="Enable/disable enhanced measurement for this stream", |
| 76 | + ), |
| 77 | + scrolls: Optional[bool] = typer.Option( |
| 78 | + None, "--scrolls/--no-scrolls", help="Capture scroll events" |
| 79 | + ), |
| 80 | + outbound_clicks: Optional[bool] = typer.Option( |
| 81 | + None, "--outbound-clicks/--no-outbound-clicks", help="Capture outbound click events" |
| 82 | + ), |
| 83 | + site_search: Optional[bool] = typer.Option( |
| 84 | + None, "--site-search/--no-site-search", help="Capture site search events" |
| 85 | + ), |
| 86 | + video_engagement: Optional[bool] = typer.Option( |
| 87 | + None, "--video-engagement/--no-video-engagement", help="Capture video engagement events" |
| 88 | + ), |
| 89 | + file_downloads: Optional[bool] = typer.Option( |
| 90 | + None, "--file-downloads/--no-file-downloads", help="Capture file download events" |
| 91 | + ), |
| 92 | + page_changes: Optional[bool] = typer.Option( |
| 93 | + None, "--page-changes/--no-page-changes", help="Capture page change (history) events" |
| 94 | + ), |
| 95 | + form_interactions: Optional[bool] = typer.Option( |
| 96 | + None, "--form-interactions/--no-form-interactions", help="Capture form interaction events" |
| 97 | + ), |
| 98 | + search_query_parameter: Optional[str] = typer.Option( |
| 99 | + None, "--search-query-parameter", help="URL query parameters for site search" |
| 100 | + ), |
| 101 | + uri_query_parameter: Optional[str] = typer.Option( |
| 102 | + None, "--uri-query-parameter", help="Additional URL query parameters" |
| 103 | + ), |
| 104 | + dry_run: bool = typer.Option( |
| 105 | + False, "--dry-run", help="Preview the request without executing" |
| 106 | + ), |
| 107 | + output_format: Optional[str] = typer.Option( |
| 108 | + None, "--output", "-o", help="Output format (json, table, compact)" |
| 109 | + ), |
| 110 | +): |
| 111 | + """Update enhanced measurement settings for a data stream.""" |
| 112 | + try: |
| 113 | + effective_property = get_effective_value(property_id, "default_property_id") |
| 114 | + require_options({"property_id": effective_property}, ["property_id"]) |
| 115 | + effective_format = resolve_output_format(output_format) |
| 116 | + |
| 117 | + field_map = { |
| 118 | + "streamEnabled": stream_enabled, |
| 119 | + "scrollsEnabled": scrolls, |
| 120 | + "outboundClicksEnabled": outbound_clicks, |
| 121 | + "siteSearchEnabled": site_search, |
| 122 | + "videoEngagementEnabled": video_engagement, |
| 123 | + "fileDownloadsEnabled": file_downloads, |
| 124 | + "pageChangesEnabled": page_changes, |
| 125 | + "formInteractionsEnabled": form_interactions, |
| 126 | + "searchQueryParameter": search_query_parameter, |
| 127 | + "uriQueryParameter": uri_query_parameter, |
| 128 | + } |
| 129 | + body = {k: v for k, v in field_map.items() if v is not None} |
| 130 | + |
| 131 | + if not body: |
| 132 | + raise typer.BadParameter( |
| 133 | + "At least one update flag must be specified (e.g. --scrolls, " |
| 134 | + "--stream-enabled, --search-query-parameter)." |
| 135 | + ) |
| 136 | + |
| 137 | + update_mask = ",".join(body.keys()) |
| 138 | + resource = _resource_name(effective_property, stream_id) |
| 139 | + |
| 140 | + if dry_run: |
| 141 | + handle_dry_run("update", "PATCH", resource, body, update_mask=update_mask) |
| 142 | + |
| 143 | + admin = get_admin_alpha_client() |
| 144 | + settings = ( |
| 145 | + admin.properties() |
| 146 | + .dataStreams() |
| 147 | + .updateEnhancedMeasurementSettings( |
| 148 | + name=resource, updateMask=update_mask, body=body |
| 149 | + ) |
| 150 | + .execute() |
| 151 | + ) |
| 152 | + success("Enhanced measurement settings updated.") |
| 153 | + output(settings, effective_format) |
| 154 | + except (typer.BadParameter, typer.Exit): |
| 155 | + raise |
| 156 | + except Exception as e: |
| 157 | + handle_error(e) |
0 commit comments