Skip to content

Instantly share code, notes, and snippets.

@leinardi
Created March 8, 2018 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leinardi/56c9c23d7d4cc97e818d6db2bc32da7d to your computer and use it in GitHub Desktop.
Save leinardi/56c9c23d7d4cc97e818d6db2bc32da7d to your computer and use it in GitHub Desktop.
Track an Event from command line to Google Analytics
#!/usr/bin/env bash
TID="UA-XXXX-Y" # The tracking ID / web property ID. The format is UA-XXXX-Y. All collected data is associated by this ID.
while getopts ":a:c:l:" opt; do
case $opt in
a)
ACTION="$OPTARG"
;;
c)
CATEGORY="$OPTARG"
;;
l)
LABEL="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
#echo "Action = ${ACTION}"
#echo "Category = ${CATEGORY}"
#echo "Label = ${LABEL}"
CID=$(uuidgen);
curl \
--silent \
--data-urlencode "v=1" \
--data-urlencode "t=event" \
--data-urlencode "tid=${TID}" \
--data-urlencode "cid=${CID}" \
--data-urlencode "ec=${CATEGORY}" \
--data-urlencode "ea=${ACTION}" \
--data-urlencode "el=${LABEL}" \
http://www.google-analytics.com/collect > /dev/null
@hannesa2
Copy link

hannesa2 commented Mar 8, 2018

nice, but how do we fire it ./analytics.sh -a action -c myCategory -l Label ?
How is the link to an analytic account ?

@leinardi
Copy link
Author

leinardi commented Mar 8, 2018

Yep, that's how you use it, you just need to change the TID="UA-XXXX-Y" with the one you want to use for the tracking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment