diff options
| author | Mike McQuaid | 2016-03-28 09:16:40 +0100 |
|---|---|---|
| committer | Mike McQuaid | 2016-04-04 12:27:47 +0100 |
| commit | 1238c65ba14831bc571cf01aead8fd10b916c837 (patch) | |
| tree | 0cb62fe4a8a5d352a3ee63b7d25a2daf82275389 | |
| parent | 0b79f48aad33a0b89fd6dc07d0103040449361b5 (diff) | |
| download | brew-1238c65ba14831bc571cf01aead8fd10b916c837.tar.bz2 | |
utils: add analytics functions.
| -rw-r--r-- | Library/Homebrew/utils.rb | 1 | ||||
| -rw-r--r-- | Library/Homebrew/utils/analytics.rb | 54 |
2 files changed, 55 insertions, 0 deletions
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index f5fb738e7..4b1f6e944 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -5,6 +5,7 @@ require "utils/inreplace" require "utils/popen" require "utils/fork" require "utils/git" +require "utils/analytics" require "open-uri" class Tty diff --git a/Library/Homebrew/utils/analytics.rb b/Library/Homebrew/utils/analytics.rb new file mode 100644 index 000000000..cdf2c4eb5 --- /dev/null +++ b/Library/Homebrew/utils/analytics.rb @@ -0,0 +1,54 @@ + +def analytics_anonymous_prefix_and_os + @analytics_anonymous_prefix_and_os ||= begin + "#{OS_VERSION}, #{HOMEBREW_PREFIX.to_s.gsub(ENV["HOME"], "~")}" + end +end + +def report_analytics(type, metadata={}) + return unless ENV["HOMEBREW_ANALYTICS"] + + metadata_args = metadata.map do |key, value| + ["-d", "#{key}=#{value}"] if key && value + end.compact.flatten + + # Send analytics. Anonymise the IP address (aip=1) and don't send or store + # any personally identifiable information. + # https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide + # https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters + system "curl", "https://www.google-analytics.com/collect", "-d", "v=1", + "--silent", "--max-time", "3", "--output", "/dev/null", + "--user-agent", "#{HOMEBREW_USER_AGENT_CURL}", + "-d", "tid=#{ENV["HOMEBREW_ANALYTICS_ID"]}", + "-d", "cid=#{ENV["HOMEBREW_ANALYTICS_USER_UUID"]}", + "-d", "aip=1", + "-d", "an=Homebrew", + "-d", "av=#{HOMEBREW_VERSION}", + "-d", "t=#{type}", + *metadata_args +end + +def report_analytics_event(category, action, label=analytics_anonymous_prefix_and_os, value=nil) + report_analytics(:event, { + :ec => category, + :ea => action, + :el => label, + :ev => value, + }) +end + +def report_analytics_exception(exception, options={}) + if exception.is_a? BuildError + report_analytics_event("BuildError", e.formula.full_name) + end + + fatal = options.fetch(:fatal, true) ? "1" : "0" + report_analytics(:exception, { + :exd => exception.class.name, + :exf => fatal, + }) +end + +def report_analytics_screenview(screen_name) + report_analytics(:screenview, :cd => screen_name) +end |
