Google Analytics w/ PHP & Flash
Credits
First of all, the credits: about a day and a half in the process of building a Google Analytics script API script myself, I found a awesome script written by Vincent Kleijnendorst (http://www.swis.nl/ga/), which made me abandon my own script and pick up where his script left off.
I did some minor modifications to Vincents script. He visualized all the data with (fast!) HTML-based graphs (see function simpleGraph in the file “analytics.class.php”). I decided to have the data portrayed with the Open Flash Chart functionality (http://teethgrinder.co.uk/open-flash-chart-2/tutorial.php), which uses Flash and JSON for its showcasing. Don’t worry: you don’t need to know neither Flash nor JSON to get this working.
The scripts are provided in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Use them at your own risk and responsibility.
Purpose
I was looking for a way to store my statistics outside of my own webspace (for storage/bandwidth reasons), but still be able to format the output to my own likings with PHP. Instead of collecting the stats with the use of some obscure external MySQL database provider, I choose Google Analytics. Hey, it’s Google, so it must be okay!
Two issues I have with GA though:
- GA cannot collect data based on single querystrings (so no individual pageviews for “index.php?pageID=19″). Luckily, this can be circumvented with mod_rewrites.
- GA does not collect visits from robots. The “search engines” entry you find in GA refers to visits THROUGH search engines, not visits BY search engines. Fortunately, the whole robot thing was not one of my clients requirements, but you should check with yours, before proceeding!
Requirements
First off: you need a Google Analytics account with at least 1 profile. Assuming you know what Google Analytics is, sign up here: http://www.google.com/analytics/.
You’ll need a PHP enabled webserver. As for as I can tell, the scripts don’t use any specific PHP 5 functions or OO-functionalities, so they should run on PHP version 4.
To actually show the graphs, you need a Flash enabled browser. Thanks to the Youtube crowd, we may assume that the major part of all Internet users have one, right? The scripts were tested in Internet Explorer 7 and Firefox 3.
Configuration
Download the ZIP file here and unpack. There should be a subfolder called named “graph” now.
- Now open the file “analytics_data.php”. On line 5 and 6 you enter the credentials that you use to login to Google Analytics online.
- You can have more than one profile in your GA account. For example: a profile for http://www.yoursite.com and a second profile for http://blog.yoursite.com.
These are retrieved sequential, so www.yoursite.com is profile 0 (the first profile you added to your GA account) and blog.yoursite.com is profile 1 (the
second profile you added to your GA account) and so on and so on…(see my profiles below)
BEWARE: every new profile creates a socalled webPropertyId, something like UA-1592971-9. You should NOT use the number behind the last dash (9) as any profile ID whatsoever!!
- On line 16 in “analytics_data.php” you can see that the right profile Id can be retrieved from $_GET["profileId"]. If this querystring is empty, the profile Id is set to 0 (meaning your first (and only?) profile).
- One line 27, the period applicable to showing your stats, is retrieved from $_GET["startdate"] and $_GET["enddate"] (both formatted as: YYYY-MM-DD). If (one of) these are empty, the script automatically reverts to picking last months stats.
- Upload the whole lot to your webserver and point your browser to http://www.yourwebsite.com/yourpathname/analytics_data.php?profileId=0&startdate=2009-05-01&enddate=2009-05-30 (or whatever profile and daterange you prefer of course)…
Output
Remember, since the scripts first address GA and then output ten Flash based graphs, the speed may not be what you’re looking for. It takes a few seconds before the whole page is loaded. Maybe you can make some adjustments to the way Flash caches its output….
I made functions and graphs for 10 key metrics, cuz that’s what my client wanted. Of course, the scripts can be extended with even more (complex) functions or timeranges (try “ga:hour” instead of “ga:date”, that should be funny….)
Main colors I used are (check “analytics.class.php” and “analytics.css” to change these):
#a60c30 : dark reddish
#d78807 : yellowish
#a2acba : grey
For dimension and metrics used in retrieving Google Analytics data, check: http://code.google.com/intl/nl/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
Graphs
graph Visitors
Shows two lines: one is the total visitors in the chosen period, two is the new visitors. So substracting the new visitors from the total will lead to the returning visitors.
graph Pageviews
A single line graph showing the pageviews in the chosen period.
graph Visits
Shows two lines: one is the total number of visits in the chosen period, two is the single-page visits (or “bounce rate” as Google calls it). So substracting the
the second from the first will lead to the those visits that addressed more than one single page.
graph Average time on site
A bar graph showing the total seconds on your website divided by the total visits.
graph Referrals
A bar graph showing the domains (e.g. google.com) of the source referring the visitor to your website.
graph Searches
A bar graph showing the keywords used by visitors to reach your site, via both paid ads and through search engine results.
graph Countries
A pie chart showing the countries of site visitors, derived from IP addresses.
graph Browsers
A pie chart showing the names and versions of browsers used by visitors to your website.
graph Operating Systems
A pie chart showing the names and versions of operating systems used by visitors to your website.
graph Screen resolutions
A pie chart showing the resolutions and color depths used by visitors to your website.
Demo
Click this link to see this blogs stats for the month of May….(temporarily disabled)p>