Class: Bolt::Analytics::Client
- Inherits:
-
Object
- Object
- Bolt::Analytics::Client
- Defined in:
- lib/bolt/analytics.rb
Instance Attribute Summary collapse
-
#bundled_content ⇒ Object
Returns the value of attribute bundled_content.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#base_params ⇒ Object
These parameters have terrible names.
- #compute_os ⇒ Object
- #event(category, action, label: nil, value: nil, **kwargs) ⇒ Object
-
#finish ⇒ Object
If the user is running a very fast command, there may not be time for analytics submission to complete before the command is finished.
-
#initialize(user_id) ⇒ Client
constructor
A new instance of Client.
- #report_bundled_content(mode, name) ⇒ Object
- #screen_view(screen, **kwargs) ⇒ Object
- #submit(params) ⇒ Object
Constructor Details
#initialize(user_id) ⇒ Client
Returns a new instance of Client.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/bolt/analytics.rb', line 102 def initialize(user_id) # lazy-load expensive gem code require 'concurrent/configuration' require 'concurrent/future' require 'httpclient' require 'locale' @logger = Logging.logger[self] @http = HTTPClient.new @user_id = user_id @executor = Concurrent.global_io_executor @os = compute_os @bundled_content = {} end |
Instance Attribute Details
#bundled_content ⇒ Object
Returns the value of attribute bundled_content.
100 101 102 |
# File 'lib/bolt/analytics.rb', line 100 def bundled_content @bundled_content end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
99 100 101 |
# File 'lib/bolt/analytics.rb', line 99 def user_id @user_id end |
Instance Method Details
#base_params ⇒ Object
These parameters have terrible names. See this page for complete documentation: developers.google.com/analytics/devguides/collection/protocol/v1/parameters
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/bolt/analytics.rb', line 172 def base_params { v: PROTOCOL_VERSION, # Client ID cid: @user_id, # Tracking ID tid: TRACKING_ID, # Application Name an: APPLICATION_NAME, # Application Version av: Bolt::VERSION, # Anonymize IPs aip: true, # User locale ul: Locale.current.to_rfc, # Custom Dimension 1 (Operating System) cd1: @os } end |
#compute_os ⇒ Object
192 193 194 195 196 |
# File 'lib/bolt/analytics.rb', line 192 def compute_os require 'facter' os = Facter.value('os') "#{os['name']} #{os.dig('release', 'major')}" end |
#event(category, action, label: nil, value: nil, **kwargs) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/bolt/analytics.rb', line 138 def event(category, action, label: nil, value: nil, **kwargs) custom_dimensions = Bolt::Util.walk_keys(kwargs) do |k| CUSTOM_DIMENSIONS[k] || raise("Unknown analytics key '#{k}'") end event_params = { # Type t: 'event', # Event Category ec: category, # Event Action ea: action }.merge(custom_dimensions) # Event Label event_params[:el] = label if label # Event Value event_params[:ev] = value if value submit(base_params.merge(event_params)) end |
#finish ⇒ Object
If the user is running a very fast command, there may not be time for analytics submission to complete before the command is finished. In that case, we give a little buffer for any stragglers to finish up. 250ms strikes a balance between accomodating slower networks while not introducing a noticeable “hang”.
203 204 205 206 |
# File 'lib/bolt/analytics.rb', line 203 def finish @executor.shutdown @executor.wait_for_termination(0.25) end |
#report_bundled_content(mode, name) ⇒ Object
132 133 134 135 136 |
# File 'lib/bolt/analytics.rb', line 132 def report_bundled_content(mode, name) if bundled_content[mode.split(' ').first]&.include?(name) event('Bundled Content', mode, label: name) end end |
#screen_view(screen, **kwargs) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/bolt/analytics.rb', line 117 def screen_view(screen, **kwargs) custom_dimensions = Bolt::Util.walk_keys(kwargs) do |k| CUSTOM_DIMENSIONS[k] || raise("Unknown analytics key '#{k}'") end screen_view_params = { # Type t: 'screenview', # Screen Name cd: screen }.merge(custom_dimensions) submit(base_params.merge(screen_view_params)) end |
#submit(params) ⇒ Object
160 161 162 163 164 165 166 167 168 |
# File 'lib/bolt/analytics.rb', line 160 def submit(params) # Handle analytics submission in the background to avoid blocking the # app or polluting the log with errors Concurrent::Future.execute(executor: @executor) do @logger.debug "Submitting analytics: #{JSON.pretty_generate(params)}" @http.post(TRACKING_URL, params) @logger.debug "Completed analytics submission" end end |