Physical Analytics Part 1: Tracking Your Home with Google Analytics

You think it's good?

Physical Analytics Part 1: Tracking Your Home with Google Analytics

All those things you couldn’t track, you now can!

No seriously, all those things. * sigh * Yes Internet, you can track your cat.

I tracked trips to the bar at the last Analytics event at SEER. I measured my sleep patterns, temperature in my house, and the amount of times my brothers cat actually uses his scratching post ALL IN GOOGLE ANALYTICS

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!

But how?

With the Universal Analytics Measurement Protocol, some Arduino Boards or Raspberry Pi’s and a couple wires and sensors.

No programming or engineering experience necessary.

What is the Measurement Protocol?

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. UA_pick After your Universal account is setup you can use the measurement protocol.

Measurement Protocol URL Dissected

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:

First part

www.google-analytics.com/collect?v=1 – beginning of the URL it will always be the same.

Second part

&tid=UA-XXXX-Y – This is your UA number 

Third part

&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.

Fourth part

&t=pageview – this tells GA that you are going to fire off a page view when you trigger the URL

Fifth part

&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

This is one for events:

www.google-analytics.com/collect?v=1&tid=UA-XXXX-Y&cid=123&t=event&ec=home%20movement&ea=door%20open&el=bedroom

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.

ga_working

Now that I showed you how it works think about the uses for all the different kinds of sensors out there.

Let’s Build!

raspi all set up

In the next steps we are going to build the device that is going to take in the information from the physical world and push it to Google Analytics using a Raspberry Pi.

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.

In this example we are going to track movement using an Raspberry Pi.

Raspberry Pi Setup

998_MED[1]

Step 1 – Raspberry Pi Necessities

Step 2 – Getting it set up

Once its setup and running and you know your way around your Raspberry Pi it’s time to connect the Goodies. I definitely recommend messing around with the Raspberry Pi and trying a couple other tutorials on learn.adafruit.com.

Step 3 – Connecting the wires to the sensor

Connect 3 of the M-F jumper wires to the GND (Ground), OUT, and +5V 2013-08-22_2251

Step 4 – Connecting the Parts on the Raspberry Pi

Connect the pi cobbler/cable to the Raspberry Pi and bread board

Click the images to expand

raspi and cobblerraspi and cobbler2

    • Connect the Black wire coming from Ground on the PIR sensor to the Ground on the breadboard
    • Connect the red wire coming from the 5v on the PIR sensor to the 5v on the breadboard
    • Connect the yellow wire coming from the OUT on the PIR sensor to port 18 on the breadboard

Click the image to expand

raspi_motion_hook_up

Step 5 – Adding the Code

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")

Step 6 – Running the Program

Then open up terminal cd into your proper directory and run it as sudo

sudo python gamovement.py

This is what you should see: 2013-08-25_2132

Step 7 – Check Real Time Reports in Google Analytics

Now look in the Real Time events to see the actions come through.

2013-08-25_2135

Live Demo Below:

You are now tracking movement in Google Analytics!

Next write-ups:

Using a light sensor to see which lights/rooms are being used the most.

Using a magnetic door sensor to see what times you enter & leave your house.

Also, big shout outs to: @iamchrisle (Chris’ blog@RachaelGerson & @Ziesslerk for help with this idea

Hi, thanks for reading! If you liked this post and saw value in it please consider sharing it. There are share buttons at the top.
Loading Facebook Comments ...