This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
forked from freddi-kit/fastlane
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
13 changed files
with
95 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 10 additions & 39 deletions
49
fastlane_core/lib/fastlane_core/analytics/analytics_event_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
64 changes: 34 additions & 30 deletions
64
fastlane_core/lib/fastlane_core/analytics/analytics_ingester_client.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.