Last active
December 23, 2015 05:29
-
-
Save wesalvaro/6587106 to your computer and use it in GitHub Desktop.
Track shell stuff with Universal 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
export UA_PROP='UA-XXXXX-Y' # Your UA property ID | |
export UA_CLIENT='wesalvaro' # A unique client (i.e. user) ID | |
alias ua_collect="curl www.google-analytics.com/collect -s -o /tmp/ua_collect --data v=1 --data tid=$UA_PROP --data cid=$UA_CLIENT" | |
# Tracks any "event" type. | |
function track_evt { | |
local category=$1 | |
local action=$2 | |
local value=$3 # integer | |
local label=$4 | |
ua_collect --data t=event \ | |
--data-urlencode "ec=$category" --data-urlencode "ea=$action" \ | |
--data-urlencode "el=$label" --data ev=$value | |
} | |
# Tracks an exception event. | |
function track_exc { | |
local desc=$1 | |
local fatal=$2 # boolean 0|1 | |
ua_collect --data t=exception \ | |
--data-urlencode "exd=$desc" --data exf=$fatal | |
} | |
# Tracks a user time event. | |
function track_time { | |
local category=$1 | |
local variable=$2 | |
local label=$3 | |
local time_ms=$4 # integer (ms) | |
ua_collect --data t=timing \ | |
--data-urlencode "utc=$category" --data-urlencode "utv=$variable" \ | |
--data utt=$time_ms --data-urlencode "utl=$label" | |
} | |
# Tracks a program invocation as an event. | |
# Use this for programs that have the interesting part as a different argument. | |
# e.g. git clone ... | |
# function g { # record a call to git (aliased as function "g") using the 1st argument. | |
# local start=$(date +%s) | |
# git $* | |
# track_program "git $1" $start | |
# } | |
function track_program { | |
local program=$1 | |
local start=$2 | |
local end=$(date +%s) | |
local ms="$(( $end - $start ))000" | |
track_time shell runtime $program $ms | |
track_evt shell run 1 $program | |
} | |
# Tracks a program invocation as an event. | |
# Use this for most programs (caveat above, see track_program) you want tracked. | |
# e.g. alias g="track_run git" # record all "git" events | |
function track_run { | |
local program=$1 | |
local start=$(date +%s) | |
$* | |
track_program $program $start | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment