Class: PDK::Analytics::Client::GoogleAnalytics
- Inherits:
-
Object
- Object
- PDK::Analytics::Client::GoogleAnalytics
- Defined in:
- lib/pdk/analytics/client/google_analytics.rb
Constant Summary collapse
- PROTOCOL_VERSION =
1
- TRACKING_URL =
'https://google-analytics.com/collect'.freeze
- CUSTOM_DIMENSIONS =
{ operating_system: :cd1, output_format: :cd2, ruby_version: :cd3, cli_options: :cd4, env_vars: :cd5, }.freeze
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#app_installer ⇒ Object
readonly
Returns the value of attribute app_installer.
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#app_version ⇒ Object
readonly
Returns the value of attribute app_version.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#base_params ⇒ Object
These parameters have terrible names.
- #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(opts) ⇒ GoogleAnalytics
constructor
A new instance of GoogleAnalytics.
- #screen_view(screen, **kwargs) ⇒ Object
- #submit(params) ⇒ Object
Constructor Details
#initialize(opts) ⇒ GoogleAnalytics
Returns a new instance of GoogleAnalytics.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 24 def initialize(opts) # lazy-load expensive gem code require 'concurrent/configuration' require 'concurrent/future' require 'httpclient' require 'locale' require 'pdk/analytics/util' @http = HTTPClient.new @user_id = opts[:user_id] @executor = Concurrent.global_io_executor @os = PDK::Analytics::Util.fetch_os_async @logger = opts[:logger] @app_name = opts[:app_name] @app_id = opts[:app_id] @app_version = opts[:app_version] @app_installer = opts[:app_installer] end |
Instance Attribute Details
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
20 21 22 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 20 def app_id @app_id end |
#app_installer ⇒ Object (readonly)
Returns the value of attribute app_installer.
22 23 24 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 22 def app_installer @app_installer end |
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
19 20 21 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 19 def app_name @app_name end |
#app_version ⇒ Object (readonly)
Returns the value of attribute app_version.
21 22 23 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 21 def app_version @app_version end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
18 19 20 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 18 def logger @logger end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
17 18 19 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 17 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 94 def base_params { v: PROTOCOL_VERSION, # Client ID cid: user_id, # Tracking ID tid: app_id, # Application Name an: app_name, # Application Version av: app_version, # Application Installer ID aiid: app_installer, # Anonymize IPs aip: true, # User locale ul: Locale.current.to_rfc, # Custom Dimension 1 (Operating System) cd1: @os.value, } end |
#event(category, action, label: nil, value: nil, **kwargs) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 58 def event(category, action, label: nil, value: nil, **kwargs) custom_dimensions = walk_keys(kwargs) do |k| CUSTOM_DIMENSIONS[k] || raise(_("Unknown analytics key '%{key}'") % { 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”.
121 122 123 124 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 121 def finish @executor.shutdown @executor.wait_for_termination(0.25) end |
#screen_view(screen, **kwargs) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 43 def screen_view(screen, **kwargs) custom_dimensions = walk_keys(kwargs) do |k| CUSTOM_DIMENSIONS[k] || raise(_("Unknown analytics key '%{key}'") % { 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
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pdk/analytics/client/google_analytics.rb', line 80 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 require 'json' logger.debug "Submitting analytics: #{JSON.pretty_generate(params)}" @http.post(TRACKING_URL, params) logger.debug 'Completed analytics submission' end end |