Intro: :: Arduinolitics :: Google Analytics on Arduino.
This is actually a revenge project, earlier this week Google "tricked" me into joining an online course, which because of "Google Analytics" they are well aware I'm a sucker for online courses. So I signed up straight away, but looking at the reply email later that day I started to feel a little regret "Google Analytics" is mostly a marketing tool.
"but," I say to myself " an API's an API and I'll find something to do with it".
The course was so informative I thought I'd lost IQ by taking it.
So I set to searching for the API, which wasn't covered at all in the alleged "course", and reclaimed a few braincells by using it to cram Analytics into Arduino and so; "Arduinolitics" was born.
There are certainly limitations to the platform, but also advantages. So far I have been unable to retrieve individual event values, the closest I got was a daily average, daily count or a count of current users.
The main advantage is you need no extra hardware, no server and Google hosts the data for you.
"Daily Average" might be good for barometric data? - Tested (example query in code comments).
"Daily Count" could be a good pedometer?
"Current Users" could have an alert set if the "current visitors" count drops below a specified value indicating one of your Arduinos has gone down. - Tested (I'm using it for a reboot counter as I type).
Step 1: Hardware and Accounts
If you already have a network enabled Arduino and Google Analytics account, that you know how to use you can skip ahead to Step 1 and start setting up.
I'm using an EtherMega and there are a cornucopia of networked Arduinos to choose from, otherwise if you need to connect an ethernet shield or wifi shield I'm sure you'll find some way to figure that out.
A Google Analytics account with at least one property set up with Universal Analytics enabled will be handy here. Mine was by default, but your mileage may vary.
Also handy will be the Tracking code for that property. Note however, the property does not seem to be required, I pointed mine to a null.html just in case, however it does not seem to be needed, the events I'm using here function independently.
If you wish to you can leave my tracking code in as I love data, please send your data to me. I'll see if I can make a report for it public somehow. :D
I do however, recommend using your own tracking codes and categories after saying hi to ArduinoNet (It's kind of like SkyNet but friendlier, and hosted on MySQL).
The real fun is watching realtime hits on the live events viewer and basking in the glory of subverting a user tracking system and putting it to real use!
OK, so we've got network connectivity and our accounts sorted, lets upload.
Step 2: Upload and Enjoy.
The attached sketch is quite simple and just pings the Google servers with a tracking event approximately every 10 seconds.You might also need floatToString.h for double/floating point sensor values.
There is one line which is essential for you to change:
String trackingID = "UA-21694160-2";
Here you should insert your tracking ID maybe after pinging ArduinoNet to say hi.
The next line could be important to you:
int deviceID = 666;
You will need to change this to a static device ID if you want one. I'm actually assigning a random device ID in setup:
deviceID = random(1000); //line 50
So you will need to remove that line too to get a static ID. Personally I like the dynamic ID to keep track of reboots, but to each their own.
The next three lines are also a bit of fun:
String payloadCategory = "Arduino";
String payloadAction = "Sensor";
String payloadLabel = "Random";
Here I have just mocked up groupings, but use whatever works for you. In my projects I've been using payloadLabel to label the sensor type, like "temp", "humidity" or "button". PayloadAction will have device ID appended on to it during reporting.
Handy debugging info is output on the serial port, so check the payload and response size there if things are going wrong. If however things are going really wrong, you may want to uncomment the response debug also.
To hook up your own sensor you probably just need to replace the line:
sensorValue = map(val, 0, 1023, 1, 100);
and set sensorValue to whatever you want (in addition to whatever setup your).
Upload, sit back and enjoy the statistics!
Don't forget the live events viewer!