Home » ARDUINO, FEATURED » Google Analytics LCD im Nutella Glas

Google Analytics LCD im Nutella Glas

[Skip to English]

Google Analytics LCD 1

Seit dem Start dieses Blogs im Juli gucke ich deutlich öfters mal in meinen Google Analytics-Account. Im Gegensatz zu einem klassischen Webshop interessieren mich dabei nicht die Verkäufe, sondern die Anzahl der Besucher, Seitenaufrufe und die Verweildauer. Damit ich diese Zahlen auch ohne ein geöffnetes Google Analytics-Tab sehe, habe ich mir folgendes, kleines Gadget gebaut.

An einen Arduino Ethernet ist ein 16×2 LCD angeschlossen (beide von Watterott, 47,48€+7,75€). Der Arduino-Sketch öffnet alle 5 Minuten auf einem angeschlossen Webserver ein PHP-Skript. Wie ein Webserver-Aufruf vom Arduino funktioniert, ist bei Bildr.org sehr ausführlich beschrieben.

Das von mir verwendete PHP-Skript basiert auf der PHP-Klasse zur Google-Analytics-API. Es loggt sich mit einem eigens dafür angelegten User in meinen Google-Account ein, führt je einen Request zu den heutigen Besuchern und einen zu den gestrigen Besuchern aus und stellt sie in einem folgendem Format dar:
<Daten heute|Daten gestern>

Der Arduino liest diesen String und teilt ihn auf die zwei LCD-Zeilen auf. Da der Platz auf dem LCD begrenzt ist, sieht man jetzt nur Besucher (v), Seitenaufrufe (p), Absprungrate (%) und Verweildauer in Sekunden (s). Sollten es mal mehr Besucher werden, könnte der Platz nicht mehr ausreichen. Entweder verschwindet dann eine Kennzahl vom Display oder es muss ein größeres Display her.

Leider unterstützt mein Router noch kein Power over Ethernet, weshalb ich noch auf eine externe Stromquelle zurückgreifen musste. Und da ich gerade keine bessere Aufbewahrungsmöglichkeit gefunden habe, ist das ganze in einem (gereinigten) Nutella Glas gelandet.

English

Since I started this blog in July, my number of Google Analytics visits per day went up. Compared with an ordinary webshop, I’m not looking for sales, I’m looking for visits, page impressions and time on site. To keep those stats in sight without having a tab for Googly Analytics open, I went on and build some kind of a gadget.

I attached a 16×2 LCD to an Arduino Ethernet, which opens a PHP-script running on a webserver every 5 minutes (a tutorial by Bildr.org helped me a lot). The PHP-script is base on the Google-Analytics-API and is quiet simple. It logs into my account and loads the data of today and yesterday and returns them as the following string:
<data of today|data of yesterday>

The Arduino reads this string and splits it into two seperate strings. Since the LCD has a limited space, it only shows the visits (v), page impressions (p), bounce rate (%) and seconds of the avg. time on site (s). Should the traffic grow, I might need to get rid of a metric or buy a bigger display.

My router doesn’t support power over ethernet yet, so I’m still using a battery for this one. And since I had nothing else to hand, I put it all in a (cleaned) Nutella jar.

Arduino-Sketch
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // MAC-Address
byte ip[] = { 192,168,178,2 }; // IP-Address
byte gateway[] = { 192, 168, 178, 1 }; // Gateway
byte subnet[] = { 255, 255, 255, 0 }; // Subnet
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
boolean partOne = false;
String pageValue1;
String pageValue2;
byte server[] = {192,168,178,2}; //ip address of the php-server
Client client(server, 80); // port 80 is typical www page
String location = "/ga/index.php"; // Location of the PHP-Script on the Server
LiquidCrystal lcd(9, 8, 7, 6, 5, 3); // numbers of the interface pins

void setup() {
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  lcd.begin(16, 2); // set up the LCD's number of columns and rows:
  lcd.setCursor(0, 1);
  lcd.print("Setup...");  // Print a Setup-message to the LCD.
  Serial.print("Setup...");
}
void loop(){
  String pageValue = connectAndRead(); //connect to the server and read the output
  String splitString = pageValue;
  int splitAt = splitString.indexOf('|');
  pageValue1 = splitString.substring(0,splitAt);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(pageValue1); //print out the findings.
  pageValue2 = splitString.substring(splitAt+1);
  lcd.setCursor(0, 1);
  lcd.print(pageValue2);
  Serial.println(pageValue); //print out the findings.
  delay(300000); //wait 5 minutes before connecting again
}

String connectAndRead(){
 //connect to the server
  Serial.println("connecting...");
  if (client.connect()) {
   Serial.println("connected");
   client.print("GET ");
   client.println(location);
   client.println();
  //Connected - Read the page
   return readPage(); //go and read the output
  }else{
   return "connection failed";
  }
}

String readPage(){
//read the page, and capture & return everything between '<' and '>'
  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory
  while(true){
    if (client.available()) {
     char c = client.read();
     if (c == '<' ) { //'<' is our begining character
       startRead = true; //Ready to start reading the part
     }else if(startRead){
        if(c != '>'){ //'>' is our ending character
         inString[stringPos] = c;
         stringPos ++;
       }else{
         //got what we need here! We can disconnect now
         startRead = false;
         client.stop();
         client.flush();
         Serial.println("disconnecting.");
         return inString;
        }
      }
    }
  }
}
PHP-Script
<?php
include('gapi.class.php');
$ga = new gapi('EMAIL','PASSWORD'); // your acoount mail-address and password
$gaID = 47110815; // your Google Analytics ID
$today=date('Y-m-d');
$yesterday = mktime(0, 0, 0, date("m"), date("d")-1, date("y"));
$yesterday = date('Y-m-d', $yesterday);
$ga->requestReportData($gaID,array('date'),
    array('pageviews','visits','entranceBounceRate','avgTimeOnSite'),
    array('-visits'),
    '',
    $today,
    $today);
echo '<v' .
    $ga->getVisits() .
    ' p' .
    $ga->getPageviews() .
    ' %' .
    intval($ga->getEntranceBounceRate()) .
    ' s' .
    intval($ga->getAvgTimeOnSite()) .
    '|';
$ga->requestReportData($gaID,
    array('date'),
    array('pageviews','visits','entranceBounceRate','avgTimeOnSite'),
    array('-visits'),
    '',
    $yesterday,
    $yesterday);
echo 'v' .
    $ga->getVisits() .
    ' p' .
    $ga->getPageviews() .
    ' %' .
    intval($ga->getEntranceBounceRate()) .
    ' s' .
    intval($ga->getAvgTimeOnSite()) .
    '>';
Tags: , , , ,

12 Kommentare

  1. George sagt:

    Ha! It’s nice idea wuth jar =) Can I use it too? 🙂
    Really, it’s interesting project for SEO. I want to make a something like this, but want to show more info.

    Thanks for sharing!

  2. Hall lieber, could you please help me more about the php script. Where i must put it?

  3. nikolas sagt:

    Is there a way to connect it to another pc with another router and different ip address (the router) and make it work? How can i change the ip on the sketch without reprogramming.

  4. Steven sagt:

    Hi,
    Very nice project but still I cant make it work 🙁
    Questions:
    1) byte ip[]={192,168,178,2} refers to ip address of ethernet-shield?
    2) byte gateway[]={****} refers to ip address of Router?
    3)byte server[]={*****} refers to ip address of the pc in which xampp server is installed?

    4) index.php file goes under xampp directory /ga/index.php ?

    Is there something else in which i have to pay attention?

    A lot of questions but i really need to make it. I am waiting for you reply.

    best regards

  5. Steven sagt:

    Ok I make it to connect it to the server but after connection the LCD screen says:
    br/
    br/
    Have you any idea about this?

    • Jan sagt:

      Sounds like your connection to Analytics isn’t working probably. Did you try to open the php-file in your browser and took a look at the page source?

  6. steven sagt:

    Alles Gut. The problem was the google analytics id which is a little difficult to find it. Danke fur diese perfekt artikel!

  7. stefano sagt:

    Hi, thank you for the guide!
    I’ve found some problem with the function

    Client client(server, 80); // port 80 is typical www page

    maybe it was out of date due to the new Ethernet library released wich declare an EthernetClient function instead of Client…

    s.

  8. helloand,there are like your document very much:-) percentage it looks like communicate opposite approximately literally post on AOL? appears need a general practitioner in this spromoting to solve my problem. ask you to that’s need to have to! meals a look in advance of when to peer you! ! !

Hinterlasse einen Kommentar

Designed by Theme Junkie · Powered by WordPress
Lesen Sie den vorherigen Eintrag:
Verkehrsampel meldet Status des Webservers

Mit einer Verkehrsampel haben die Mitarbeiter aus mkanoaps Büro eine schöne Umsetzung für die Visualisierung des Webserver-Status gefunden. Die kritischen...

Schließen