Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Google Analytics geeklet using OAuth 2.0
- # Author: Jacob Salmela
- # http://jacobsalmela.com
- # 2014-08-15
- # Inspired by http://www.visualab.org/index.php/using-google-rest-api-for-analytics
- # This script is meant to run as a geeklet once you have OAuth 2.0 set up (and you filled in the variables below)
- # By default, it will display today's visits but the metric can be changed
- # Set this geeklet to refresh every 3500 seconds or so (the token is valid for 1 hour, so this gives a little bit of a buffer
- # The biggest caveat is that if the token expires, you have to go through the process of requesting another one
- # That said, this geeklet would work best on a computer that never shuts down
- #---------VARIABLES-------
- # Colors
- # These could be used for as a visual indicator if you want x amount of visitors to be green (good) and y amount to be red (not enough visitors)
- end="\x1b[0m"
- bold="\x1b[001m"
- underscore="\x1b[004m"
- red="\x1b[031m"
- green="\x1b[032m"
- yellow="\x1b[033m"
- # These should be all filled in after OAuth is set up
- # Instructions to do so are here:
- # API credentials
- source ~/oauth_cache.txt
- # Google Analytics ID and desired metric
- # Just the number, do not prefix with ga:
- # profileID="12345678"
- # NOT
- # profileID="ga:12345678"
- #
- profileID=""
- desiredMetric="activeUsers"
- #----------SCRIPT----------
- echo "${bold}Google Analytics${end}"
- printf "site 1: \t"
- curl -s "https://www.googleapis.com/analytics/v3/data/realtime?ids=ga:$profileID&metrics=rt:$desiredMetric&access_token=$access_token" | tr , '\n' | grep "totalsForAllResults" | cut -d'"' -f6
- profileID=""
- printf "site 2: \t"
- curl -s "https://www.googleapis.com/analytics/v3/data/realtime?ids=ga:$profileID&metrics=rt:$desiredMetric&access_token=$access_token" | tr , '\n' | grep "totalsForAllResults" | cut -d'"' -f6
- profileID=""
- printf "site 3: \t"
- curl -s "https://www.googleapis.com/analytics/v3/data/realtime?ids=ga:$profileID&metrics=rt:$desiredMetric&access_token=$access_token" | tr , '\n' | grep "totalsForAllResults" | cut -d'"' -f6
- printf "\n "
- # Refresh OAuth token
- /bin/bash ~/oauth.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement