New Google Analytics for Status Board Server Edition

Last week, Google finally deprecated their non-Oauth APIs, which means that the Google Analytics for Status Board code I have been publishing stopped working. Fortunately for us, Github user erebusnz updated the PHP API to work with OAuth2 and we can access Google again. I have included it in a new version of the Server Edition Package.

Quick Install Instructions

  1. Download the statusboard.zip file.
  2. Expand it in the root of your web server. It creates a statusboard folder.
  3. New: Register your application with Google Developers Console and get your Service Account email and P12 certificate file.
  4. New: Determine your profile ID (its the number after the ‘p’ in the URL generated for your analytics)
  5. **New:**Replace the 3 instances of <—-> in each PHP file with your service account, P12 file name and profile ID.
  6. The URL to use in Status Board is http://<your-domain>/statusboard/analytics_<file>.php, replacing <your-domain> with your server domain name, and <file> with one of views (graph), hourly (graph) or pages (table).

You should get something like this (Yes, my follower count is still tiny. Yes, I live in New York, but I still use Celsius for weather. And awesome Inbox Zero!):

Details

There are four files in the archive:

  • analytics_views.php to present a graph of Page views, Visitors and New Visits for the past week for a site.
  • analytics_hourly.php to present the same data over the last 24 hours.
  • analytics_pages.php to show the top pages today.
  • gapi.class.php is required to access Google Analytics

Registering your API Access

You need to create a new Project in the Google Developers Console in order to permit your web application to access Google’s data.

  1. Click Create Project
  1. Give the Project a Name, such as “My-GAPI-Project”
  2. Wait while Google does its thing and moves you to the Project Dashboard
  3. Click on “APIs & Auth”
  4. Click “APIs” and search for “Analytics”
  1. Click on “Analytics API” then click “Enable API”
  1. Click on “Credentials” to create a service account
  1. Click on “Create new Client ID”
  2. Choose “Service account” and click “Create Client ID”
  3. Wait for it…
  4. Google will download a JSON file to you. So nice of them.
  5. Click “OK, got it”

Fill in the Details

You will now create, copy and paste the three elements needed for the Analytics PHP files to access Googles Analytics API.

  1. Copy the “Email address” which ends with “@developer.gserviceaccount.com” and replace the “<Email address @developer.gserviceaccount.com>” in each analytics PHP file.
  2. Click “Generate new P12 Key”. Google will send you a “.p12” file.
  3. Copy this file into the SAME folder as the analytics PHP files.
  4. Replace the “<P-File Name.p12>” in each Analytics PHP file with the execat name of the P12 file (including extension, no path needed)
  5. Go to Google Analytics and bring up one of the reports for the site you want to track.
  6. Copy and paste the URL into a text editor, we’re going spelunking for the profile ID. E.g. httpz://www.google.com/analytics/web/?hl=en#realtime/rt-overview/a1234567w1234567p9999999/"
  7. Your profile ID is the set of digits after the ‘p’ at the end of the URL (in this case a fake 9999999). Paste that into the “<Use Yours>” in each analytics PHP file.

The top of each file should look something like this (codes are fake):

define('ga_profile_id','9999999');

require 'gapi.class.php';

date_default_timezone_set('America/New_York');

$ga = new gapi('729847129341293-938825he75l8mdabcxsh72hdqkdmpfobq273@developer.gserviceaccount.com', 
  'My-GAPI-Project-384idsucy44f.p12');

Accessing from Status Board

For the ‘hourly’ “and ‘views’ graphs, create a new Status Board chart panel and set the URL to “<your domain>/status_board/analytics_hourly.php” and “<your domain>/status_board/analytics_views.php” respectively. For the Top pages list, create a new Status Board table and set the URL to “<your domain>/status_board/analytics_pages.php”.

Enjoy.

Follow the author as @hiltmon on Twitter.

Posted By Hilton Lipschitz · Jun 7, 2015 7:47 PM