Last active
November 16, 2020 17:32
-
-
Save LeZuse/9cbe20355a1af7fb7bce8fb4b0817aa8 to your computer and use it in GitHub Desktop.
Bash history logging into Google Analytics
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
#!/bin/bash | |
# Install | |
# Option 1: Install iTerm Bash integration | |
# https://www.iterm2.com/documentation-shell-integration.html | |
# Option 2: bash-preexec | |
# https://github.com/rcaloras/bash-preexec/blob/master/README.md#install | |
# Then source this file into your .bash_profile or equivalent: | |
# source "$HOME/analytics.bash" | |
GAID=UA-XXXXXX-X | |
USID="`/usr/bin/uuidgen`" | |
analytics_track() { | |
`nohup curl \ | |
-q \ | |
--max-time 3 \ | |
--user-agent 'analytics-bash-0.1' \ | |
--data v=1 \ | |
--data aip=1 \ | |
--data t=event \ | |
--data tid="$GAID" \ | |
--data cid="$USID" \ | |
--data an="command" \ | |
--data av="0.1" \ | |
\ | |
--data ec="terminal" \ | |
--data ea="${1-unknown command}" \ | |
--data el="${3-label}" \ | |
--data ev="${2-0}" \ | |
--silent --output /dev/null \ | |
https://www.google-analytics.com/collect > /dev/null 2>&1 &` | |
} | |
# preexec() { echo "before exec "`date +%s`" args:$@ pwd:$PWD"; } | |
# preexec_functions+=(preexec) | |
# preprompt() { echo "after exec "`date +%s`" args:$@"; } | |
# precmd_functions+=(preprompt) | |
# analytics_track | |
preexec() { | |
# echo "preexec $1" | |
# analytics_track "$1" "$time" | |
export BASH_MEASURE_CMD=$1 | |
export BASH_MEASURE_START=`date +%s` | |
# echo cmd:"$BASH_MEASURE_CMD" start:$BASH_MEASURE_START | |
} | |
# precmd_functions+=(analytics_track) | |
precmd() { | |
# analytics_track | |
# echo 1 >/dev/null | |
# echo last status: $? | |
# grab the previous command exit code asap | |
\local status=$? | |
# terminal start | |
[ -z "$BASH_MEASURE_START" ] && return 0 | |
\local time=$((`date +%s` - $BASH_MEASURE_START)) | |
# log for debugging | |
echo cmd:"$BASH_MEASURE_CMD" time:$time status:$status >> ~/.bash_analytics.log | |
# echo $BASH_MEASURE_CMD $time | |
analytics_track "$BASH_MEASURE_CMD" "$time" | |
} |
Author
LeZuse
commented
Nov 16, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment