aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils/analytics.rb
blob: 32ea22f89d06cf33c93750233caf3f4da3a286b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
def analytics_label
  @analytics_anonymous_prefix_and_os ||= begin
    os = OS_VERSION
    prefix = ", non-/usr/local" if HOMEBREW_PREFIX.to_s != "/usr/local"
    ci = ", CI" if ENV["CI"]
    "#{os}#{prefix}#{ci}"
  end
end

def report_analytics(type, metadata = {})
  return if ENV["HOMEBREW_NO_ANALYTICS"] || ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"]

  args = %W[
    --max-time 3
    --user-agent #{HOMEBREW_USER_AGENT_CURL}
    -d v=1
    -d tid=#{ENV["HOMEBREW_ANALYTICS_ID"]}
    -d cid=#{ENV["HOMEBREW_ANALYTICS_USER_UUID"]}
    -d aip=1
    -d an=#{HOMEBREW_PRODUCT}
    -d av=#{HOMEBREW_VERSION}
    -d t=#{type}
  ]
  metadata.each { |k, v| args << "-d" << "#{k}=#{v}" if k && v }

  # Send analytics. Don't send or store any personally identifiable information.
  # https://github.com/Homebrew/brew/blob/master/share/doc/homebrew/Analytics.md
  # 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"]
    puts Utils.popen_read ENV["HOMEBREW_CURL"],
      "https://www.google-analytics.com/debug/collect",
      *args
  else
    pid = fork do
      exec ENV["HOMEBREW_CURL"],
        "https://www.google-analytics.com/collect",
        "--silent", "--output", "/dev/null",
        *args
    end
    Process.detach pid
  end
end

def report_analytics_event(category, action, label = analytics_label, 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) &&
     exception.formula.tap && !exception.formula.tap.private?
    report_analytics_event("BuildError", exception.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