feat: support user-configured Google Analytics reporting

Allow users to set a custom Google Analytics tracking ID to report user behaviour
via new environment variable: $HOMEBREW_ADDITIONAL_GOOGLE_ANALYTICS_ID.

If provided, this tracking ID will be used _in addition to_ the default tracking
ID used by <https://brew.sh/analytics/>.
This commit is contained in:
Kyle Smith 2021-10-13 13:40:54 -05:00 committed by Kyle Smith
parent 58662f8da8
commit 7866a4b586
3 changed files with 53 additions and 40 deletions

View File

@ -11,6 +11,10 @@ module Homebrew
module_function
ENVS = {
HOMEBREW_ADDITIONAL_GOOGLE_ANALYTICS_ID: {
description: "Additional Google Analytics tracking ID to emit user behaviour analytics to. " \
"For more information, see: <https://docs.brew.sh/Analytics>",
},
HOMEBREW_ARCH: {
description: "Linux only: Pass this value to a type name representing the compiler's `-march` option.",
default: "native",

View File

@ -20,48 +20,52 @@ module Utils
return if not_this_run?
return if disabled?
args = []
analytics_ids = ENV.fetch("HOMEBREW_ANALYTICS_IDS", "").split(",")
analytics_ids.each do |analytics_id|
args = []
# do not load .curlrc unless requested (must be the first argument)
args << "--disable" unless Homebrew::EnvConfig.curlrc?
# do not load .curlrc unless requested (must be the first argument)
args << "--disable" unless Homebrew::EnvConfig.curlrc?
args += %W[
--max-time 3
--user-agent #{HOMEBREW_USER_AGENT_CURL}
--data v=1
--data aip=1
--data t=#{type}
--data tid=#{ENV["HOMEBREW_ANALYTICS_ID"]}
--data cid=#{ENV["HOMEBREW_ANALYTICS_USER_UUID"]}
--data an=#{HOMEBREW_PRODUCT}
--data av=#{HOMEBREW_VERSION}
]
metadata.each do |key, value|
next unless key
next unless value
args += %W[
--max-time 3
--user-agent #{HOMEBREW_USER_AGENT_CURL}
--data v=1
--data aip=1
--data t=#{type}
--data tid=#{analytics_id}
--data cid=#{ENV["HOMEBREW_ANALYTICS_USER_UUID"]}
--data an=#{HOMEBREW_PRODUCT}
--data av=#{HOMEBREW_VERSION}
]
metadata.each do |key, value|
next unless key
next unless value
key = ERB::Util.url_encode key
value = ERB::Util.url_encode value
args << "--data" << "#{key}=#{value}"
end
# Send analytics. Don't send or store any personally identifiable information.
# https://docs.brew.sh/Analytics
# https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
# https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
if ENV["HOMEBREW_ANALYTICS_DEBUG"]
url = "https://www.google-analytics.com/debug/collect"
puts "#{ENV["HOMEBREW_CURL"]} #{args.join(" ")} #{url}"
puts Utils.popen_read ENV["HOMEBREW_CURL"], *args, url
else
pid = fork do
exec ENV["HOMEBREW_CURL"],
*args,
"--silent", "--output", "/dev/null",
"https://www.google-analytics.com/collect"
key = ERB::Util.url_encode key
value = ERB::Util.url_encode value
args << "--data" << "#{key}=#{value}"
end
# Send analytics. Don't send or store any personally identifiable information.
# https://docs.brew.sh/Analytics
# https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
# https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
if ENV["HOMEBREW_ANALYTICS_DEBUG"]
url = "https://www.google-analytics.com/debug/collect"
puts "#{ENV["HOMEBREW_CURL"]} #{args.join(" ")} #{url}"
puts Utils.popen_read ENV["HOMEBREW_CURL"], *args, url
else
pid = fork do
exec ENV["HOMEBREW_CURL"],
*args,
"--silent", "--output", "/dev/null",
"https://www.google-analytics.com/collect"
end
Process.detach T.must(pid)
end
Process.detach T.must(pid)
end
nil
end
def report_event(category, action, label = os_arch_prefix_ci, value = nil)

View File

@ -64,12 +64,17 @@ setup-analytics() {
if [[ -n "${HOMEBREW_LINUX}" ]]
then
# For Homebrew on Linux's analytics.
HOMEBREW_ANALYTICS_ID="UA-76492262-1"
HOMEBREW_ANALYTICS_IDS="UA-76492262-1"
else
# Otherwise, fall back to Homebrew's analytics.
HOMEBREW_ANALYTICS_ID="UA-76679469-1"
HOMEBREW_ANALYTICS_IDS="UA-76679469-1"
fi
export HOMEBREW_ANALYTICS_ID
if [[ -n "${HOMEBREW_ADDITIONAL_GOOGLE_ANALYTICS_ID}" ]]
then
HOMEBREW_ANALYTICS_IDS="${HOMEBREW_ANALYTICS_IDS},${HOMEBREW_ADDITIONAL_GOOGLE_ANALYTICS_ID}"
fi
export HOMEBREW_ANALYTICS_IDS
export HOMEBREW_ANALYTICS_USER_UUID
}