Class: FastlaneCore::AnalyticsSession

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(analytics_ingester_client: AnalyticsIngesterClient.new) ⇒ AnalyticsSession

Returns a new instance of AnalyticsSession.



19
20
21
22
23
24
25
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 19

def initialize(analytics_ingester_client: AnalyticsIngesterClient.new)
  require 'securerandom'
  @session_id = SecureRandom.uuid
  @client = analytics_ingester_client
  @events = []
  @is_fastfile = false
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 8

def client
  @client
end

#eventsObject

Returns the value of attribute events.



9
10
11
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 9

def events
  @events
end

#fastfile_idObject

Returns the value of attribute fastfile_id.



10
11
12
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 10

def fastfile_id
  @fastfile_id
end

#is_fastfileObject Also known as: fastfile?

Returns the value of attribute is_fastfile.



11
12
13
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 11

def is_fastfile
  @is_fastfile
end

#session_idObject

Returns the value of attribute session_id.



7
8
9
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 7

def session_id
  @session_id
end

Instance Method Details

#action_completed(completion_context: nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 137

def action_completed(completion_context: nil)
  backfill_p_hashes(p_hash: completion_context.p_hash)

  builder = AnalyticsEventBuilder.new(
    oauth_app_name: oauth_app_name,
    p_hash: completion_context.p_hash,
    session_id: session_id,
    action_name: completion_context.action_name
  )

  @events << builder.completed_event(
    primary_target_hash: {
      name: 'status',
      detail: completion_context.status
    }
  )
end

#action_launched(launch_context: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 42

def action_launched(launch_context: nil)
  backfill_p_hashes(p_hash: launch_context.p_hash)

  builder = AnalyticsEventBuilder.new(
    oauth_app_name: oauth_app_name,
    p_hash: launch_context.p_hash,
    session_id: session_id,
    action_name: launch_context.action_name
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'fastlane_version',
      detail: fastlane_version
    }
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'configuration_language',
      detail: launch_context.configuration_language
    }
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'install_method',
      detail: install_method
    }
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'operating_system',
      detail: operating_system
    },
    secondary_target_hash: {
      name: 'version',
      detail: operating_system_version
    }
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'build_tool_version',
      detail: launch_context.build_tool_version
    }
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'ci',
      detail: ci?.to_s
    }
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'fastfile',
      detail: fastfile?.to_s
    },
    secondary_target_hash: {
      name: 'fastfile_id',
      detail: fastfile_id.to_s
    }
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'platform',
      detail: launch_context.platform.to_s
    }
  )

  @events << builder.launched_event(
    primary_target_hash: {
      name: 'ruby_version',
      detail: ruby_version
    }
  )
end

#backfill_p_hashes(p_hash: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 27

def backfill_p_hashes(p_hash: nil)
  return if p_hash.nil? || p_hash == ActionLaunchContext::UNKNOWN_P_HASH || @events.count == 0
  @events.reverse_each do |event|
    # event[:actor][:name] is the field in which we store the p_hash
    # to be sent to analytics ingester.
    # If they are nil, we want to fill them in until we reach
    # an event that already has a p_hash.
    if event[:actor][:name].nil? || event[:actor][:name] == ActionLaunchContext::UNKNOWN_P_HASH
      event[:actor][:name] = p_hash
    else
      break
    end
  end
end

#ci?Boolean

Returns:



192
193
194
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 192

def ci?
  return Helper.ci?
end

#fastlane_versionObject



163
164
165
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 163

def fastlane_version
  return Fastlane::VERSION
end

#finalize_sessionObject



155
156
157
158
159
160
161
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 155

def finalize_session
  # If our users want to opt out of usage metrics, don't post the events.
  # Learn more at https://docs.fastlane.tools/#metrics
  return if FastlaneCore::Env.truthy?("FASTLANE_OPT_OUT_USAGE")

  client.post_events(@events)
end

#install_methodObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 176

def install_method
  if Helper.rubygems?
    return 'gem'
  elsif Helper.bundler?
    return 'bundler'
  elsif Helper.mac_app?
    return 'mac_app'
  elsif Helper.contained_fastlane?
    return 'standalone'
  elsif Helper.homebrew?
    return 'homebrew'
  else
    return 'unknown'
  end
end

#oauth_app_nameObject

make this a method so that we can override it in monkey patches



15
16
17
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 15

def oauth_app_name
  return 'fastlane_v2'
end

#operating_systemObject



172
173
174
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 172

def operating_system
  return Helper.operating_system
end

#operating_system_versionObject



196
197
198
199
200
201
202
203
204
205
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 196

def operating_system_version
  os = self.operating_system
  case os
  when "macOS"
    return system('sw_vers', out: File::NULL) ? `sw_vers -productVersion`.strip : 'unknown'
  else
    # Need to test in Windows and Linux... not sure this is enough
    return Gem::Platform.local.version
  end
end

#ruby_versionObject



167
168
169
170
# File 'fastlane_core/lib/fastlane_core/analytics/analytics_session.rb', line 167

def ruby_version
  patch_level = RUBY_PATCHLEVEL == 0 ? nil : "p#{RUBY_PATCHLEVEL}"
  return "#{RUBY_VERSION}#{patch_level}"
end