Advertisement
Guest User

GA GeekTool

a guest
Feb 9th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.10 KB | None | 0 0
  1. # Google Analytics geeklet using OAuth 2.0
  2. # Author: Jacob Salmela
  3. # http://jacobsalmela.com
  4. # 2014-08-15
  5. # Inspired by http://www.visualab.org/index.php/using-google-rest-api-for-analytics
  6. # This script is meant to run as a geeklet once you have OAuth 2.0 set up (and you filled in the variables below)
  7. # By default, it will display today's visits but the metric can be changed
  8. # 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
  9. # The biggest caveat is that if the token expires, you have to go through the process of requesting another one
  10. # That said, this geeklet would work best on a computer that never shuts down
  11.  
  12. #---------VARIABLES-------
  13. # Colors
  14. # 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)
  15. end="\x1b[0m"
  16. bold="\x1b[001m"
  17. underscore="\x1b[004m"
  18. red="\x1b[031m"
  19. green="\x1b[032m"
  20. yellow="\x1b[033m"
  21.  
  22. # These should be all filled in after OAuth is set up
  23. # Instructions to do so are here:
  24.  
  25. # API credentials
  26. source ~/oauth_cache.txt
  27.  
  28.  
  29. # Google Analytics ID and desired metric
  30. # Just the number, do not prefix with ga:
  31. #   profileID="12345678"
  32. #       NOT
  33. #   profileID="ga:12345678"
  34. #
  35. profileID=""
  36. desiredMetric="activeUsers"
  37.  
  38.  
  39. #----------SCRIPT----------
  40. echo "${bold}Google Analytics${end}"
  41. printf "site 1: \t"
  42. 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
  43.  
  44. profileID=""
  45. printf "site 2: \t"
  46. 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
  47.  
  48. profileID=""
  49. printf "site 3: \t"
  50. 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
  51.  
  52. printf "\n "
  53.  
  54.  
  55. # Refresh OAuth token
  56. /bin/bash ~/oauth.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement