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
- Download the statusboard.zip file.
- Expand it in the root of your web server. It creates a
statusboard
folder. - New: Register your application with Google Developers Console and get your Service Account email and P12 certificate file.
- New: Determine your profile ID (its the number after the ‘p’ in the URL generated for your analytics)
- **New:**Replace the 3 instances of <—-> in each PHP file with your service account, P12 file name and profile ID.
- 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 ofviews
(graph),hourly
(graph) orpages
(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.
- Click Create Project
- Give the Project a Name, such as “My-GAPI-Project”
- Wait while Google does its thing and moves you to the Project Dashboard
- Click on “APIs & Auth”
- Click “APIs” and search for “Analytics”
- Click on “Analytics API” then click “Enable API”
- Click on “Credentials” to create a service account
- Click on “Create new Client ID”
- Choose “Service account” and click “Create Client ID”
- Wait for it…
- Google will download a JSON file to you. So nice of them.
- 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.
- Copy the “Email address” which ends with “@developer.gserviceaccount.com” and replace the “<Email address @developer.gserviceaccount.com>” in each analytics PHP file.
- Click “Generate new P12 Key”. Google will send you a “.p12” file.
- Copy this file into the SAME folder as the analytics PHP files.
- Replace the “<P-File Name.p12>” in each Analytics PHP file with the execat name of the P12 file (including extension, no path needed)
- Go to Google Analytics and bring up one of the reports for the site you want to track.
- 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/"
- 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.