Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Commit

Permalink
Re-add metrics (fastlane#12781)
Browse files Browse the repository at this point in the history
* Send launch events to Google Analytics

* Address various comments

* More comments

* Various improvements

* Fix build issues

* Delay sending metrics if app identifier not found
  • Loading branch information
KrauseFx authored and Josh Holtz committed Jun 30, 2018
1 parent 69312fe commit 1adfd91
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 538 deletions.
14 changes: 8 additions & 6 deletions fastlane/lib/fastlane/fast_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,17 @@ def test(params = {})
end

def action_launched(action_name)
# https://github.com/fastlane/fastlane/issues/11913
# action_launch_context = FastlaneCore::ActionLaunchContext.context_for_action_name(action_name, configuration_language: "ruby", args: ARGV)
# FastlaneCore.session.action_launched(launch_context: action_launch_context)
action_launch_context = FastlaneCore::ActionLaunchContext.context_for_action_name(action_name,
configuration_language: "ruby",
args: ARGV)
FastlaneCore.session.action_launched(launch_context: action_launch_context)
end

def action_completed(action_name, status: nil)
# https://github.com/fastlane/fastlane/issues/11913
# completion_context = FastlaneCore::ActionCompletionContext.context_for_action_name(action_name, args: ARGV, status: status)
# FastlaneCore.session.action_completed(completion_context: completion_context)
completion_context = FastlaneCore::ActionCompletionContext.context_for_action_name(action_name,
args: ARGV,
status: status)
FastlaneCore.session.action_completed(completion_context: completion_context)
end
end
end
3 changes: 0 additions & 3 deletions fastlane/lib/fastlane/lane_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ def self.cruise_lane(platform, lane, parameters = nil, env = nil, fastfile_path

platform, lane = choose_lane(ff, platform) unless lane

# https://github.com/fastlane/fastlane/issues/11913
# FastlaneCore.session.is_fastfile = true

# xcodeproj has a bug in certain versions that causes it to change directories
# and not return to the original working directory
# https://github.com/CocoaPods/Xcodeproj/issues/426
Expand Down
10 changes: 1 addition & 9 deletions fastlane/lib/fastlane/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,6 @@ def execute_action(method_sym, class_ref, arguments, custom_dir: nil, from_actio
verify_supported_os(method_sym, class_ref)

begin
# https://github.com/fastlane/fastlane/issues/11913
# launch_context = FastlaneCore::ActionLaunchContext.context_for_action_name(method_sym.to_s, configuration_language: configuration_language, args: ARGV)
# FastlaneCore.session.action_launched(launch_context: launch_context)

Dir.chdir(custom_dir) do # go up from the fastlane folder, to the project folder
# If another action is calling this action, we shouldn't show it in the summary
# (see https://github.com/fastlane/fastlane/issues/4546)
Expand All @@ -254,11 +250,7 @@ def execute_action(method_sym, class_ref, arguments, custom_dir: nil, from_actio
puts("==========================================\n".deprecated)
end
class_ref.runner = self # needed to call another action form an action
return_value = class_ref.run(arguments)

action_completed(method_sym.to_s, status: FastlaneCore::ActionCompletionStatus::SUCCESS)

return return_value
return class_ref.run(arguments)
end
end
rescue Interrupt => e
Expand Down
2 changes: 0 additions & 2 deletions fastlane/lib/fastlane/swift_lane_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def self.cruise_lane(lane, parameters = nil, env = nil, disable_runner_upgrades:
# https://github.com/CocoaPods/Xcodeproj/issues/426
# Setting this environment variable causes xcodeproj to work around the problem
ENV["FORK_XCODE_WRITING"] = "true"
# https://github.com/fastlane/fastlane/issues/11913
# FastlaneCore.session.is_fastfile = true

Fastlane::Helper::DotenvHelper.load_dot_env(env)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,21 @@
module FastlaneCore
class AnalyticsEventBuilder
attr_accessor :base_hash
attr_accessor :action_name

def initialize(oauth_app_name: nil, p_hash: nil, session_id: nil, action_name: nil, timestamp_millis: (Time.now.to_f * 1000).to_i)
def initialize(p_hash: nil, session_id: nil, action_name: nil)
@p_hash = p_hash
@session_id = session_id
@action_name = action_name
@base_hash = {
event_source: {
oauth_app_name: oauth_app_name,
product: 'fastlane'
},
actor: {
name: p_hash,
detail: session_id
},
millis_since_epoch: timestamp_millis,
version: 1
}
end

def launched_event(primary_target_hash: nil, secondary_target_hash: nil)
return new_event(
stage: 'launched',
primary_target_hash: primary_target_hash,
secondary_target_hash: secondary_target_hash
)
end

def completed_event(primary_target_hash: nil, secondary_target_hash: nil)
return new_event(
stage: 'completed',
primary_target_hash: primary_target_hash,
secondary_target_hash: secondary_target_hash
)
end

def new_event(stage: nil, primary_target_hash: nil, secondary_target_hash: nil)
raise 'Need at least a primary_target_hash' if primary_target_hash.nil?
event = base_hash.dup
event[:action] = {
name: stage,
detail: action_name
def new_event(action_stage)
{
client_id: @p_hash,
category: :undefined,
action: action_stage,
label: action_name,
value: nil
}
event[:primary_target] = primary_target_hash
event[:secondary_target] = secondary_target_hash unless secondary_target_hash.nil?
return event
end
end
end
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
require 'faraday'
require 'openssl'
require 'json'

require_relative '../helper'

module FastlaneCore
class AnalyticsIngesterClient
def post_events(events)
unless Helper.test?
Thread.new do
send_request(json: { analytics: events }.to_json)
end
GA_URL = "https://www.google-analytics.com"

private_constant :GA_URL

def initialize(ga_tracking)
@ga_tracking = ga_tracking
end

def post_event(event)
# If our users want to opt out of usage metrics, don't post the events.
# Learn more at https://docs.fastlane.tools/#metrics
if Helper.test? || FastlaneCore::Env.truthy?("FASTLANE_OPT_OUT_USAGE")
return nil
end
return Thread.new do
send_request(event)
end
return true
end

def send_request(json: nil, retries: 2)
post_request(body: json)
def send_request(event, retries: 2)
post_request(event)
rescue
retries -= 1
retry if retries >= 0
end

def post_request(body: nil)
if ENV['METRICS_DEBUG']
write_json(body)
end
url = ENV["FASTLANE_METRICS_URL"] || "https://fastlane-metrics.fabric.io"

require 'faraday'
connection = Faraday.new(url) do |conn|
def post_request(event)
connection = Faraday.new(GA_URL) do |conn|
conn.request(:url_encoded)
conn.adapter(Faraday.default_adapter)
if ENV['METRICS_DEBUG']
conn.proxy = "https://127.0.0.1:8888"
conn.ssl[:verify_mode] = OpenSSL::SSL::VERIFY_NONE
end
end
connection.post do |req|
req.url('/public')
req.headers['Content-Type'] = 'application/json'
req.body = body
end
end

# This method is only for debugging purposes
def write_json(body)
File.write("#{ENV['HOME']}/Desktop/mock_analytics-#{Time.now.to_i}.json", body)
connection.headers[:user_agent] = 'fastlane/' + Fastlane::VERSION
connection.post("/collect", {
v: "1", # API Version
tid: @ga_tracking, # Tracking ID / Property ID
cid: event[:client_id], # Client ID
t: "event", # Event hit type
ec: event[:category], # Event category
ea: event[:action], # Event action
el: event[:label] || "na", # Event label
ev: event[:value] || "0" # Event value
})
end
end
end
Loading

0 comments on commit 1adfd91

Please sign in to comment.