Class: Bolt::Analytics::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/analytics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id) ⇒ Client

Returns a new instance of Client.



64
65
66
67
68
69
70
# File 'lib/bolt/analytics.rb', line 64

def initialize(user_id)
  @logger = Logging.logger[self]
  @http = HTTPClient.new
  @user_id = user_id
  @executor = Concurrent.global_io_executor
  @os = compute_os
end

Instance Attribute Details

#user_idObject (readonly)

Returns the value of attribute user_id.



62
63
64
# File 'lib/bolt/analytics.rb', line 62

def user_id
  @user_id
end

Instance Method Details

#base_paramsObject

These parameters have terrible names. See this page for complete documentation: developers.google.com/analytics/devguides/collection/protocol/v1/parameters



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/bolt/analytics.rb', line 121

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.value
  }
end

#compute_osObject



141
142
143
144
145
146
147
# File 'lib/bolt/analytics.rb', line 141

def compute_os
  Concurrent::Future.execute(executor: @executor) do
    require 'facter'
    os = Facter.value('os')
    "#{os['name']} #{os.dig('release', 'major')}"
  end
end

#event(category, action, label: nil, value: nil, **kwargs) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bolt/analytics.rb', line 87

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

#finishObject

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”.



154
155
156
157
# File 'lib/bolt/analytics.rb', line 154

def finish
  @executor.shutdown
  @executor.wait_for_termination(0.25)
end

#screen_view(screen, **kwargs) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bolt/analytics.rb', line 72

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



109
110
111
112
113
114
115
116
117
# File 'lib/bolt/analytics.rb', line 109

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