diff --git a/Library/Homebrew/utils/analytics.sh b/Library/Homebrew/utils/analytics.sh index 3c6fc59a08..47716e5e56 100644 --- a/Library/Homebrew/utils/analytics.sh +++ b/Library/Homebrew/utils/analytics.sh @@ -1,30 +1,44 @@ +# Migrate analytics UUID to its new home in Homebrew repo's git config and +# remove the legacy UUID file if detected. +migrate-legacy-uuid-file() { + local legacy_uuid_file="$HOME/.homebrew_analytics_user_uuid" + if [[ -f "$legacy_uuid_file" ]] + then + local analytics_uuid="$(<"$legacy_uuid_file")" + if [[ -n "$analytics_uuid" ]] + then + git config --file="$HOMEBREW_REPOSITORY/.git/config" --replace-all homebrew.analyticsuuid "$analytics_uuid" + fi + rm -f "$legacy_uuid_file" + fi +} + setup-analytics() { - # User UUID file. Used for Homebrew user counting. Can be deleted and - # recreated with no adverse effect (beyond our user counts being inflated). - HOMEBREW_ANALYTICS_USER_UUID_FILE="$HOME/.homebrew_analytics_user_uuid" + local git_config_file="$HOMEBREW_REPOSITORY/.git/config" + + migrate-legacy-uuid-file # Make disabling anlytics sticky if [[ -n "$HOMEBREW_NO_ANALYTICS" ]] then - git config --file="$HOMEBREW_REPOSITORY/.git/config" --replace-all homebrew.analyticsdisabled true - # Internal variable for brew's use, to differentiate from user-supplied setting - export HOMEBREW_NO_ANALYTICS_THIS_RUN="1" + git config --file="$git_config_file" --replace-all homebrew.analyticsdisabled true + git config --file="$git_config_file" --unset-all homebrew.analyticsuuid fi - if [[ "$(git config --file="$HOMEBREW_REPOSITORY/.git/config" --get homebrew.analyticsmessage)" != "true" || - "$(git config --file="$HOMEBREW_REPOSITORY/.git/config" --get homebrew.analyticsdisabled)" = "true" ]] + local message_seen="$(git config --file="$git_config_file" --get homebrew.analyticsmessage)" + local analytics_disabled="$(git config --file="$git_config_file" --get homebrew.analyticsdisabled)" + if [[ "$message_seen" != "true" || "$analytics_disabled" = "true" ]] then - [[ -f "$HOMEBREW_ANALYTICS_USER_UUID_FILE" ]] && rm -f "$HOMEBREW_ANALYTICS_USER_UUID_FILE" + # Internal variable for brew's use, to differentiate from user-supplied setting export HOMEBREW_NO_ANALYTICS_THIS_RUN="1" return fi - if [[ -r "$HOMEBREW_ANALYTICS_USER_UUID_FILE" ]] + HOMEBREW_ANALYTICS_USER_UUID="$(git config --file="$git_config_file" --get homebrew.analyticsuuid)" + if [[ -z "$HOMEBREW_ANALYTICS_USER_UUID" ]] then - HOMEBREW_ANALYTICS_USER_UUID="$(<"$HOMEBREW_ANALYTICS_USER_UUID_FILE")" - else HOMEBREW_ANALYTICS_USER_UUID="$(uuidgen)" - echo "$HOMEBREW_ANALYTICS_USER_UUID" > "$HOMEBREW_ANALYTICS_USER_UUID_FILE" + git config --file="$git_config_file" --replace-all homebrew.analyticsuuid "$HOMEBREW_ANALYTICS_USER_UUID" fi if [[ -n "$HOMEBREW_LINUX" ]] diff --git a/share/doc/homebrew/Analytics.md b/share/doc/homebrew/Analytics.md index e22f11607b..2200335e1b 100644 --- a/share/doc/homebrew/Analytics.md +++ b/share/doc/homebrew/Analytics.md @@ -13,7 +13,7 @@ Homebrew's analytics record some shared information for every event: - The Homebrew user agent e.g. `Homebrew/0.9.9 (Macintosh; Intel Mac OS X 10.11.4) curl/7.43.0` - The Google Analytics version i.e. `1` (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#v) - The Homebrew analytics tracking ID e.g. `UA-75654628-1` (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tid) -- A Homebrew analytics user ID e.g. `1BAB65CC-FE7F-4D8C-AB45-B7DB5A6BA9CB`. This is generated by `uuidgen` and stored in `~/.homebrew_analytics_user_uuid`. This does not allow us to track individual users but does enable us to accurately measure user counts vs. event counts (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cid) +- A Homebrew analytics user ID e.g. `1BAB65CC-FE7F-4D8C-AB45-B7DB5A6BA9CB`. This is generated by `uuidgen` and stored in the repository-specific Git configuration variable `homebrew.analyticsuuid` within `$(brew --repository)/.git/config`. This does not allow us to track individual users but does enable us to accurately measure user counts vs. event counts (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cid) - The Google Analytics anonymous IP setting is enabled i.e. `1` (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#aip) - The Homebrew application name e.g. `Homebrew` (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#an) - The Homebrew application version e.g. `0.9.9` (https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#av)