When we think Google Analytics we think web tracking, we think of page views, events and goals. But have you ever thought about tracking your sleep patterns in GA or the amount of times a door opens, or which lights are on more than others? Do you want to know who’s moving around in your house when you’re not there? Or if your kids used your office while you were gone? Well you can!
The Measurement Protocol is a part of Google Analytics new version called Universal Analytics. Note: I will be going really in depth and technical about all the things it can do in the future but going to hold off right now. These posts are supposed to be FUN and show non-business folks how Google Analytics can help monitor their homes. The measurement protocol is a URL that you can send to Google Analytics that has your information on it and can trigger an action. For example, you can send the URL to GA and say count this as a virtual page view or count this as an event action. You need an Universal Analytics account. Go and register one, it’s free and why not also add it to your website. You can have Universal and Asynchronous (the older version) on your blog without interfering with each other. After your Universal account is setup you can use the measurement protocol.
Here it is: www.google-analytics.com/collect?v=1&tid=UA-XXXX-Y&cid=123&t=pageview&dp=%2Fsubpage
Now I am going to break it out:
www.google-analytics.com/collect?v=1 – beginning of the URL it will always be the same.
&tid=UA-XXXX-Y – This is your UA number
&cid=123 – this stands for client ID and for now we can make it anything we want, in the later posts I am going to talk about some more advanced stuff you can do with it.
&t=pageview – this tells GA that you are going to fire off a page view when you trigger the URL
&dp=%2Fanypage – this tells GA that the pageview, as mentioned above is for the deep page called /anypage.
Note: you need to use the URL encoding for spaces and symbols
www.google-analytics.com/collect?v=1&tid=UA-XXXX-Y&cid=123 – the same as I mentioned above
&t=event — GA is going to track an event
&ec=home%20movement — the event category is home movement
&ea=door%20open — the event action is door open.
&el=bedroom — the event label is bedroom
Check out the Measurement Protocol Developer Guide for more ways to write the measurement protocol.
Now try it yourself! Put the your UA number in the URL and put it into a browser window.
Now that I showed you how it works think about the uses for all the different kinds of sensors out there.
They have sensors to detect movement, light, sound, temperature, humidity, and much more. Previously the hard part about gathering all this data was building a way to display it, yeah I can record my sleep movement but then I have to do something visual with the data, now you have Google to visualize it for you.
Now that we can trigger GA from off line features lets hook it up to a couple sensors and write a small program to send a message when a door gets open or it notices movement.
Connect the pi cobbler/cable to the Raspberry Pi and bread board
Click the images to expand
Click the image to expand
Remember – you need the Rpi.GPIO package installed Now boot up the Raspberry Pi and add this python code:
import time import urllib2 import RPi.GPIO as io io.setmode(io.BCM) pir_pin = 18 io.setup(pir_pin, io.IN) # activate input def hitGA(): print("sent to GA") urllib2.urlopen("http://www.google-analytics.com/collect?v=1&tid=UA-XXXXXX-Y&cid=1111&t=event&ec=Movement&ea=livingRoom&el=desk").close while True: if io.input(pir_pin): hitGA() print("Movement")
Then open up terminal cd into your proper directory and run it as sudo
sudo python gamovement.py
Now look in the Real Time events to see the actions come through.
Live Demo Below: