Log real life events in Google Analytics
Google Analytics is a great tool, among others, to monitor what happens on your website or your mobile applications. But what about real life?
Today, real life tracking is everywhere: GPS tracker for jogging, wristband and apps to track how you sleep, move and eat, Smart Body Analyzer in our scales, automatic wiper coupled to a rainwater sensor in our cars… Everywhere we can put a sensor, monitor and act accordingly – from our kitchen to our bike, web server or interior plants.
What I want to show you today is how we track visitors here, in the JoliCode office! Using a simple pressure sensor, an Arduino board, some electronic skills and the Google Analytics Event API, we now have an average idea of how many people came in to say hi everyday (you are free to come by !).
Section intitulée building-http-requests-on-arduinoBuilding HTTP requests on Arduino
The most fun part of this project was to make the HTTP request using Ethernet library of Arduino. This couple of lines did the trick for the request part:
String eventData = "v=1&tid=UA-0000000-0&cid=1337&t=event&ec=irl&ea=visitor&el=IRL Visit&ev=1";
client.println("POST /collect HTTP/1.1");
client.println("Host: www.google-analytics.com");
client.print("Content-Length: ");
client.println(eventData.length());
client.println("Content-Type:application/x-www-form-urlencoded");
client.println();
client.println(eventData);
But getting the HTTP response code is a bit slippery. You have to manually read and parse the response as you receive it: byte by byte. This is where I had to search for some basic C functions I haven’t seen in ages!
Here is how I do it:
while (responseCode < 100 && responseSize < responseSizeMax) {
response[responseSize] = client.read(); // Add one byte to the response
responseSize++;
response[responseSize] = '\0'; // Set manually the end of string char
// Here is the magic!
sscanf (response, "HTTP/1.%*d %d", &responseCode);
}
I run sscanf
on the partial response and as soon as a valid HTTP response code is found, I leave the loop and close the connection. I expect to have the status code before hitting a limit of 1024 char, but that’s just a security.
The whole code is available in this gist, and has no dependencies (there is some HttpClient out there for Arduino).
As you can see, we hit Google Analytics on the google-analytics.com/collect
endpoint; while real-time datas are available for everyone with this API, you will need a Universal Analytics property to benefit reporting of your custom events.
Section intitulée wiring-and-polishingWiring and polishing
The board is designed to be hidden, and the pressure sensor put under a carpet right after our front door. No computer connection is needed, but to check if the sensor and the HTTP request worked we have added two LED (red and green). We also wanted to be able to adjust the captor sensibility without any computer and compiling involved, so we added a potentiometer.
Here is the complete wiring of the project:
It’s not shown on the schema, but obviously we use an Ethernet Shield (the one from Arduino).
Section intitulée results-and-conclusionResults and conclusion
Here we go: every foot crossing our carpet is now logged (even the NSA is aware of it) and displayed in our Analytics dashboard:
Placing a pressure sensor under a carpet is not the best way to monitor actual visitors – but this shows us how to do basic event monitoring. We can now use this base to add more sensors and statistics:
- humidity ;
- temperature ;
- noise (especially when Bastien is talking!) ;
- luminosity (and turn the lights on automatically) ;
- etc! Sky is the limit.
If you are interested in Arduino and DIY stuffs, I recommend the awesome Bildr blog (even if there is no recent articles ☹) and Makezine which is also a reference on the subject.