Class: PDK::Analytics::Client::GoogleAnalytics

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ GoogleAnalytics

Returns a new instance of GoogleAnalytics.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pdk/analytics/client/google_analytics.rb', line 19

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_idObject (readonly)

Returns the value of attribute app_id.



17
18
19
# File 'lib/pdk/analytics/client/google_analytics.rb', line 17

def app_id
  @app_id
end

#app_installerObject (readonly)

Returns the value of attribute app_installer.



17
18
19
# File 'lib/pdk/analytics/client/google_analytics.rb', line 17

def app_installer
  @app_installer
end

#app_nameObject (readonly)

Returns the value of attribute app_name.



17
18
19
# File 'lib/pdk/analytics/client/google_analytics.rb', line 17

def app_name
  @app_name
end

#app_versionObject (readonly)

Returns the value of attribute app_version.



17
18
19
# File 'lib/pdk/analytics/client/google_analytics.rb', line 17

def app_version
  @app_version
end

#loggerObject (readonly)

Returns the value of attribute logger.



17
18
19
# File 'lib/pdk/analytics/client/google_analytics.rb', line 17

def logger
  @logger
end

#user_idObject (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_paramsObject

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/pdk/analytics/client/google_analytics.rb', line 89

def base_params
  require 'locale'

  {
    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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pdk/analytics/client/google_analytics.rb', line 53

def event(category, action, label: nil, value: nil, **kwargs)
  custom_dimensions = walk_keys(kwargs) do |k|
    CUSTOM_DIMENSIONS[k] || raise(format("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

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



118
119
120
121
# File 'lib/pdk/analytics/client/google_analytics.rb', line 118

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

#screen_view(screen, **kwargs) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pdk/analytics/client/google_analytics.rb', line 38

def screen_view(screen, **kwargs)
  custom_dimensions = walk_keys(kwargs) do |k|
    CUSTOM_DIMENSIONS[k] || raise(format("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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pdk/analytics/client/google_analytics.rb', line 75

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