Class: AnalyticsOps::Clients::Admin

Inherits:
Object
  • Object
show all
Includes:
AdminGovernance, AdminGovernanceNormalization, AdminResourceNormalization
Defined in:
lib/analytics_ops/clients/admin.rb,
sig/analytics_ops.rbs

Overview

Narrow adapter around Google's generated Admin client.

Constant Summary collapse

PACKAGE_REQUIREMENT =
Gem::Requirement.new("~> 0.8.0")
RETENTION_TO_GOOGLE =
{
  "2_months" => :TWO_MONTHS,
  "14_months" => :FOURTEEN_MONTHS,
  "26_months" => :TWENTY_SIX_MONTHS,
  "38_months" => :THIRTY_EIGHT_MONTHS,
  "50_months" => :FIFTY_MONTHS
}.freeze
GOOGLE_TO_RETENTION =
RETENTION_TO_GOOGLE.invert.freeze
STREAM_TYPES =
{
  "WEB_DATA_STREAM" => "web",
  "ANDROID_APP_DATA_STREAM" => "android",
  "IOS_APP_DATA_STREAM" => "ios"
}.freeze
CAPABILITIES =
{
  "account_property_discovery" => :list_account_summaries,
  "data_stream_discovery" => :list_data_streams,
  "data_retention" => :update_data_retention_settings,
  "key_events" => :create_key_event,
  "custom_dimensions" => :create_custom_dimension,
  "custom_metrics" => :create_custom_metric,
  "enhanced_measurement_audit" => :get_enhanced_measurement_settings,
  "data_redaction_audit" => :get_data_redaction_settings,
  "reporting_identity_audit" => :get_reporting_identity_settings,
  "attribution_audit" => :get_attribution_settings,
  "channel_group_audit" => :list_channel_groups,
  "calculated_metric_audit" => :list_calculated_metrics,
  "event_create_rule_audit" => :list_event_create_rules,
  "event_edit_rule_audit" => :list_event_edit_rules,
  "bigquery_link_audit" => :list_big_query_links,
  "change_history" => :search_change_history_events,
  "access_report" => :run_access_report
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, service_account: nil, access: :read, transport: :grpc, timeout: nil, logger: nil) ⇒ Admin

Returns a new instance of Admin.

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/analytics_ops/clients/admin.rb', line 54

def initialize(client: nil, service_account: nil, access: :read, transport: :grpc, timeout: nil, logger: nil)
  unless .nil? || .is_a?(ServiceAccount)
    raise ConfigurationError, "service_account must be an AnalyticsOps::ServiceAccount"
  end
  raise ConfigurationError, "access must be :read or :edit" unless %i[read edit].include?(access)

  @client = client
  @service_account = 
  @access = access
  @transport = validate_transport(transport)
  @timeout = validate_timeout(timeout)
  @logger = logger
end

Instance Method Details

#access_reportObject



705
# File 'sig/analytics_ops.rbs', line 705

def access_report: (

#apply_change(change, property_id:) ⇒ Plan::Change

Parameters:

Returns:

Raises:



161
162
163
164
165
166
167
168
# File 'lib/analytics_ops/clients/admin.rb', line 161

def apply_change(change, property_id:)
  raise InvalidPlanError, "change must be an AnalyticsOps::Plan::Change" unless change.is_a?(Plan::Change)

  property_name(property_id)
  method_name, request = mutation(change, property_id)
  get(method_name, request)
  change
end

#capabilitiesHash[String, bool]

Returns:

  • (Hash[String, bool])


143
144
145
146
# File 'lib/analytics_ops/clients/admin.rb', line 143

def capabilities
  generated_client = translate_errors { client }
  CAPABILITIES.to_h { |name, method| [name, generated_client.respond_to?(method)] }.freeze
end

#change_historyObject



700
# File 'sig/analytics_ops.rbs', line 700

def change_history: (

#compatibilityrecord

Returns:

  • (record)


148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/analytics_ops/clients/admin.rb', line 148

def compatibility
  specification = Gem::Specification.find_by_name("google-analytics-admin")
  Canonical.immutable(
    "package" => specification.name,
    "version" => specification.version.to_s,
    "requirement" => PACKAGE_REQUIREMENT.to_s,
    "supported" => PACKAGE_REQUIREMENT.satisfied_by?(specification.version),
    "transport" => @transport.to_s
  )
rescue Gem::LoadError => error
  raise UnsupportedCapabilityError, Redaction.message(error.message)
end

#discover(include_streams: true) ⇒ Array[Resources::Account]

Parameters:

  • include_streams: (Boolean) (defaults to: true)

Returns:

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/analytics_ops/clients/admin.rb', line 68

def discover(include_streams: true)
  raise ConfigurationError, "include_streams must be true or false" unless [true, false].include?(include_streams)

  list(:list_account_summaries, page_size: 200).map do |summary|
     = remote_string(field(summary, :account), "account name")
    properties = array_field(summary, :property_summaries).map do |property|
      normalized = normalize_property(property, can_edit: field(property, :can_edit))
      next normalized.to_h unless include_streams

      normalized.to_h.merge("streams" => list_streams(normalized.id).map(&:to_h))
    end
    properties.sort_by! { |property| property.fetch("id") }

    Resources::Account.new(
      id: resource_id(),
      name: ,
      display_name: remote_string(field(summary, :display_name), "account display name"),
      properties:
    )
  end.sort_by(&:id).freeze
end

#governance_snapshotGovernance::Snapshot

Parameters:

  • property_id (String)

Returns:



699
# File 'sig/analytics_ops.rbs', line 699

def governance_snapshot: (String property_id) -> Governance::Snapshot

#property_access(property_id) ⇒ Resources::Property

Parameters:

  • property_id (String)

Returns:

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/analytics_ops/clients/admin.rb', line 90

def property_access(property_id)
  expected_name = property_name(property_id)
  id = property_id
  list(:list_account_summaries, page_size: 200).each do |summary|
    array_field(summary, :property_summaries).each do |property|
      next unless resource_id(field(property, :property)) == id

      normalized = normalize_property(property, can_edit: field(property, :can_edit))
      return normalized if normalized.name == expected_name

      raise RemoteError, "Google Admin API returned a property name that does not match the request"
    end
  end

  raise AuthorizationError, "Configured property is not present in accessible account summaries"
end

#snapshot(property_id) ⇒ Snapshot

Parameters:

  • property_id (String)

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/analytics_ops/clients/admin.rb', line 107

def snapshot(property_id)
  property_name = property_name(property_id)
  property = normalize_property(get(:get_property, name: property_name), can_edit: nil)
  unless property.name == property_name
    raise RemoteError, "Google Admin API returned a property name that does not match the request"
  end

  streams = list_streams(property_id)
  retention = normalize_retention(get(:get_data_retention_settings,
                                      name: "#{property_name}/dataRetentionSettings"))
  key_events = list(:list_key_events, parent: property_name, page_size: 200).map do |value|
    normalize_key_event(value)
  end
  custom_dimensions = list(:list_custom_dimensions, parent: property_name, page_size: 200)
                      .map { |value| normalize_custom_dimension(value) }
  custom_metrics = list(:list_custom_metrics, parent: property_name, page_size: 200)
                   .map { |value| normalize_custom_metric(value) }
  validate_snapshot_names!(
    property: property_name,
    streams:,
    retention:,
    key_events:,
    dimensions: custom_dimensions,
    metrics: custom_metrics
  )

  Snapshot.new(
    property:,
    streams:,
    retention:,
    key_events:,
    custom_dimensions:,
    custom_metrics:
  )
end