Tracking active visitors with Google Analytics and Raspberry Pi in real-time
If you haven’t heard of the Raspberry Pi, let me tell you one thing.
It is a wonderful tiny little piece of technology.
Let’s image s full functional computer of the size of credit card. That’s what it is! It was built for educational purposes but because of its size it is now widely used. It is so small that you can hide it very easily. Especially for IoT (Internet of Things) products it is a very popular component.
The Raspberry Pi® is a single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools. (adafruit)
Before diving right into my first projects I needed to get myself familiar with some new basics. I am a web developer and I wasn’t much into electronics at school or university. If you want to use the Pi it is necessary to know about some electronic basics. There are a lot of resources for that out there. Just go and look what fits your needs.
But for some Pi experiments I can totally recommend this article by PubNub. This is where I built my first circuits and where I learned about how to control them over the Internet.
The first idea was to to turn on a LED whenever someone is hitting my blog. Since I already was able to control the light over the Internet I just needed a way to see if someone is on my site right now. I also knew there was a Google Analytics Real Time API which could help with that.
The PubNub Realtime Data Stream Network enables developers build and scale realtime apps for IoT, Mobile, and web applications. (PubNub)
In the meantime I got a little display, so I decided to enhance my first idea and to show some user data too.
So here is a little schema of what wer are trying to achieve:
- Every minute a cronjob will check the Google Real Time API for an active user
- If there is one it will push some data to our PubNub channel
- A python script running on the Raspberry Pi will listen to this channel and taking care of activating the LED and the display.
Step 1: SignUp for a PubNub account
In order to communicate between a PHP script on a server and a program on the Raspberry Pi we are using PubNub. It provides real time communication between different devices. The free plan is enough for our little project. Make sure to create an app after the sign up process. Selecting the app will show you some config data we will need later.
Step 2: SignUp for the Beta Real Time Reporting API
This API is still in BETA and you need to sign up first here. For more information about this API check out the overview. We will use it to receive real-time data from our Google Analytics account which is in this case my blog.
Step 3: Receiving and sending Google Analytics data
get_and_send_ga_realtime_data.php contains the PHP script to get and send the GA data. I have a cronjob that
is calling this file every minute. This is enough to make it feel like real-time. In order to get data from the API we need to provide some information about our GA account:
- Service Account Email
- Key File Location
- GA Profile View ID
Fill them at the right spot labeled with
Also make sure to provide the Google API PHP client library at the correct location.
$optParams we define the additional real-time data we need. Because of that we are able to show the user’s country and city on the display.
After receiving the data we want to send it to the our PubNub app.
We are publishing the data to a PubNub Channel called
analytics-channel. If there is an active user the led value is set
to one and we pass the country and the city. If not, the led value will be zero.
Here you can see the full file:
Step 4: Physical stuff
Ok, this will be the fun part. We will setup a LED and a display for the Raspberry Pi in order to show our Google Analytics data.
This is what I have used.
- Raspberry Pi 2 Model B
- LED display 16x2 (hd44780 chip)
- LED (1.6V)
- Jumper Wires
- Resistor 200Ω
- Resistor 1kΩ
And this is how we connect all the parts.
The LED uses just a small circuit where one side belongs to the ground and the other to a GPIO pin which is sending some power when we want it to. In between we got an 220Ω resistor which depends on the LED you are using.
For the display we need 12 of its 16 connectors in order to use it in a 4-bit mode. Here is a detailed list of the wiring.
- Pin #1 of the LCD goes to ground (black wire)
- Pin #2 of the LCD goes to +5V (red wire)
- Pin #3 (Vo) of the LCD goes to over a transistor to the ground (black wire)
- Pin #4 (RS) connects to GPIO #26 (orange wire)
- Pin #5 (RW) goes to ground (black wire)
- Pin #6 (EN) connects to GPIO #19 (white wire)
- Skip LCD Pins #7, #8, #9 and #10
- Pin #11 (D4) connects to GPIO #13 (purple wire)
- Pin #12 (D5) connects to GPIO #6 (yellow wire)
- Pin #13 (D6) connects to GPIO #5 (gray wire)
- Pin #14 (D7) connects to GPIO #12 (brown wire)
- Pin #15 (LED +) goes to +5V (red wire)
- Pin #16 (LED -) goes to ground (black wire)
For more information about connecting such a display you should take a look at this Adafruit Tutorial (PDF).
Step 5: The python script
The python script is our listener. Again we are using a PubNub library, but this time we are listening to the subscribed channel. In the callback function
I am checking the returned message. The
led key defines if there is an active user. If there is one the main method
will pass the user’s country and city to the display and the led will blink.
The beginning of the
control_led_and_display.py script is all about settings. Afterwards you will find the more
interesting part. See this part below.
So this is how you can track an active an user with Google Analytics and a Raspberry Pi. It was my first article on an IoT project and I hope you find every information you need. Please leave me a comment if you got any further questions or other feedback. Of course this is just a fast prototype and there is enough place for improvements. Have fun!