Skip to content

Instantly share code, notes, and snippets.

@dinigo
Created January 10, 2017 12:05
Show Gist options
  • Save dinigo/a6c2e840071778b23deb934a1bd6ab5b to your computer and use it in GitHub Desktop.
Save dinigo/a6c2e840071778b23deb934a1bd6ab5b to your computer and use it in GitHub Desktop.
# Store and retrieve an uuid as if it was a cookie
function get_uuid() {
local client_id_file=${1:-$HOME/.uuid}
local client_id=""
if [[ -f $client_id_file ]]; then
client_id=$(cat $client_id_file)
else
client_id=$(od -x /dev/urandom | head -1 | awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}')
echo "$client_id" > $client_id_file
fi
echo $client_id
}
# Send analytics to google
function analytics() {
DOMAIN="www.google-analytics.com"
ANALYTICS_PATH="collect"
# ANALYTICS_PATH="debug/collect"
PAYLOAD="v=1&cid=$(get_uuid)"
while [[ $# > 0 ]] ; do
case "$1" in
-t|--t|--type)
shift
PAYLOAD+="&t=$1"
;;
--tid|--tracking-id)
shift
PAYLOAD+="&tid=$1"
;;
--dh|--document-hostname)
shift
PAYLOAD+="&dh=$1"
;;
--dp|--document-path)
shift
PAYLOAD+="&dp=$1"
;;
--dt|--document-title)
shift
PAYLOAD+="&dt=$1"
;;
--ec|--event-category)
shift
PAYLOAD+="&ec=$1"
;;
--ea|--event-action)
shift
PAYLOAD+="&ea=$1"
;;
--el|--event-label)
shift
PAYLOAD+="&el=$1"
;;
esac
shift
done
URL="https://$DOMAIN/$ANALYTICS_PATH?$PAYLOAD"
curl -s -X POST --data "$PAYLOAD" $URL > /dev/null
# curl -v -X POST --data "$PAYLOAD" $URL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment