Physical Analytics Part 2: Tracking Doors, Windows, & More with Google Analytics

You think it's good?

Physical Analytics Part 2: Tracking Doors, Windows, & More with Google Analytics

See how often people come into your house or store with Google Analytics.

In my last post I went over all how I use the measurement protocol and Universal Analytics to track movement, well this time the Door Tracker is up! It can also work with most things that close like windows, desk drawers and

First, lets recap how we are doing this:

The new version of Google Analytics allows you to push data to it with a new feature called the measurement protocol. The measurement protocol is basically a URL here is an example:

To learn more about Universal Analytics, the new version of Google Analytics, and the parts of the measurement protocol (URL) see my introductory post: Tracking Your Home in Google Analytics

Ok now we are back!

Tracking movement was cool but I want more exact information. I want to know if people are coming and going in my house when I’m not there. I want to know what time my house-keeper leaves or if anyone goes into my room.

Using an magnetic door sensor can give you a better reading of people coming in and out of your home, or business. The PIR sensor just senses change in inferred activity so if you’re moving around a lot  it is going to to go off a lot.

This is a little bit more of a permanent fix since you install it in your door frame but there is a ton of other fun hacks you can use with it to make it worth it. Also, if you have an alarm system or any kind of magnetic locks you can relativity wire it into that but I recommend learning about electrical engineering before you’re ripping apart your wires.

Step 1 – Get the Parts:

1 Raspberry Pi
1 Door sensor (magnetic contact switch) or here is one that comes with sticky pads so you dont need to drill into your door.
1 Pi Cobbler
1 Breadboard
Optional: 22 AWG hookup wire – you need this is you really want to hook it up to your door.

If you don’t know about RasPi’s you also need to install the OS (tutorial) way to connect the Pi to a network either with a Ethernet cord or wifi dongle, a SD card as the hard drive, and micro USB charger for power

About The Sensor:

The Door sensor uses a thing called a reed switch. This basically triggers when it notices a magnetic force near it. So technically you can connect this to anything that you want to track when the part with the wires separates from the part by itself. This will work if you put it on windows, fridges, drawers!



It can me installed on to anything with a door or drawer.

Technical Information

Here is more about the reed switch as explained (really well) by

The Door Switch, uses what is called a reed switch. These are two contacts inside a glass tube, that is then encased in plastic. When a magnet (the other white block) is placed near the reed switch, the contacts are drawn together and the switch closes. Since this is just a switch, the leads can be connected either way around.
We will use the Pi’s ability to create an internal pull-up resistor on the reed-switch pin, so we don’t need an external pull-up resistor.

The reed switch is pretty common for door sensors so thats why I said you can wire your home security system into Google Analytics by splitting the wires into a Raspberry Pi or Arduino Board.

Step 2 – Connect all the Parts


Connect the PiCobbler to the Pi and Breadboard
raspi and cobblerraspi and cobbler2

Connect the Door Sensor to the port in front of pin 18 and GND (Ground)

IMG_20130919_220335 (1)

Step 3 – Fire up the Raspberry Pi and Add this Python Code

Remember – you need to install Rpi.GPIO (tutorial) for this code to work.
import urllib2
import time
import RPi.GPIO as io

door_sensor = 18
sensorTrigger = True

io.setup(door_sensor, io.IN, pull_up_down=io.PUD_UP)

# function for the door opening
def door_open():
    print("Door Open")

# function for the door closing
def door_close():
    print("Door Close")

while True:
    if io.input(door_sensor): # if door is opened
        if (sensorTrigger):
            door_open() # fire GA code
            sensorTrigger = False # make sure it doesn't fire again
    if not io.input(door_sensor): # if door is closed
        if not (sensorTrigger):
            door_close() # fire GA code
            sensorTrigger = True # make sure it doesn't fire again

Step 4 – Run it in Terminal

$ sudo python

It’s coming through.

Make sure its working in the terminal and then go to Google Analtyics real-time events.

You should see the category as doors and action as open and close.

Note: Open is when the magnet is not touching and Closed is when it is touching. It will only fire once even if the door is left open.

Boom, You’re Done!

You can add this to fridges, doors, liquor cabinets, lockers, windows, office desk drawers and anything else that you want to be aware of when it opens.

Have any cool ideas? Leave it in the comments below!

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