Catalytics: Track your cat’s eating habits using Google Analytics 8

Universal Analytics, Google’s new statistics tool, let’s you use Google Analytics without Javascript (in Norwegian).

With Universal Analytics and the Measurement Protocol, you can send analytics data to Google using GET- and POST-requests, without Javascript. I used it to track how often my cat eats.
With Universal Analytics and the Measurement Protocol, you can send analytics data to Google using GET- and POST-requests, without Javascript. I used it to track how often my cat eats.

Measurement Protocol, which is part of Universal Analytics, allows you to send page views and events to Google Analytics from any program, and just a few lines of code are needed to track anything from the command line.

When we don’t need Javascript for our tracking anymore, we can track whatever we want.

Like this guy:

Valdemar is a 7-year-old male Abyssinian who loves food.
Valdemar is a 7-year-old male Abyssinian who loves food.

How I’m tracking my cat using Google Analytics

There’s an RFID reader under Valdemar’s food bowl, a device that goes «beep» whenever an RFID chip comes close.

Valdemar has an RFID chip attached to his collar.

When he approaches his food and starts eating, the RFID chip is scanned and tracked in Google Analytics using a simple PHP script running on a Raspberry Pi.

See how it works in this video:

Results

Here are the results from a couple of hours of tracking:

Valdemar ate in the afternoon and in the morning the next day.
Valdemar ate in the afternoon and in the morning the next day.

More research is needed, as I couldn’t get him to wear the collar for a very long period of time. To be fair, you could say wearing the collar changes the results of the experiment, but that’s not unusual for experiments involving cats.

Try this at home

If you want to try this yourself, you’ll need an RFID reader, a computer, and my universalAnalytics PHP class, which you’ll find on Github.

The RFID reader I use is connected to a Raspberry Pi. You can use any computer you want, I chose the Pi because it’s small and easy to place.

This program is run from the command line, and waits for the RFID reader to scan the chip.

/*
* Name: Catalytics
* Description: Track cat eating habits using an RFID reader and Universal Analytics
* Developer: Audun Rundberg
* Date: August 2, 2013
*/

// Usage:
// $ cat /dev/ttyUSB0 | php example-catalytics.php
// Replace ttyUSB0 with your device

require ('class.universalAnalytics.php');

// Tracking ID
$tid = 'UA-12345-4';

// Event to track
$ec = 'Cat';

// Action to track
$ea = 'Foodbowl';

/*
* timestamp is used to save the time each RFID tag was last scanned.
* Since the RFID reader continues to scan the RFID tag as long as it's within
* range, we have to
*/
$timestamp = array ();

// Wait this long between tracking each RFID tag
$wait = 60*5;

// Read RFID tag from STDIN and track the pageview
while ($cid = trim (fgets (STDIN))) {

        // Only track the pageview if the timestamp for the $cid has expired
        if (!isset ($timestamp[$cid]) or $timestamp[$cid] + $wait < time ()) {                 
        // Track the pageview
        $ua = new universalAnalytics ($tid, $cid);
        $ua->track (array (
                        't' => 'event',
                        'ec' => $ec,
                        'ea' => $ea));

                $timestamp[$cid] = time ();

                echo "Tracked event for: $cid\n";

        }
        else {

                echo "RFID scanned, but not tracked. Timeout expires " . date ('r', $timestamp[$cid] + $wait) . "\n";

        }

}