How to Pull Custom Google Analytics Data with PHP

How to Pull Custom Google Analytics Data with PHP

Google runs a very tight Internet media company offering outstanding resources to their users. Google Docs is perfect for online collaboration and document sharing. Gmail is a pristine example of how a Web e-mail interface should look. And their home page search functionality clearly wins out over all rival competition.

And when it comes to tracking website analytics and visitor stats it seems Google yet again offers some incredible functionality. Google Analytics is a free tool which automatically tracks visitor data from any website you choose. And with their backend API it’s possible to pull data directly from your account into a new web application. It seems the features are endless!

How to Pull Custom Google Analytics Data with PHP

[tut demo=”https://onextrapixel.com/examples/google-analytics-data/” download=”https://onextrapixel.com/examples/google-analytics-data/google-analytics-data.zip”]

We’ll be constructing a small data table pulling page views from a website. The table will sort visitors based on browser type (Firefox/Safari) and Operating System (Windows/Macintosh/iPhone). Within the script there are config options to change if you wish to view older dates, skip by 1 week at a time, or change domain names. Understand that API development is fairly simple, but you should have a rudimentary understanding of PHP development techniques.

Final Result

Note: The loading for the demo will be slightly slow due to pulling of data from Google Analytics.

Setting Up our Testing Server

First off we’ll be working with a basic PHP wrapper library. This simply means instead of coding our own library to connect into Google Analytics and pull account data, we can use a library written by somebody else. The Electric Toolbox is a development blog which features a reliable PHP wrapper for pulling data out of Analytics. If you want to skip the reading just grab the direct download instead.

Now before we go unpacking files it’s important to also ensure your development environment can handle cURL events. Put simply, cURL is a plugin addition to the PHP library which features a set of functions used to fetch remote data. In other words cURL will grab HTTP data from the Analytics API without any custom work required. As the old saying goes “Why reinvent the wheel?“.

I haven’t used a MAMP setup so I’m not sure the exact steps to enable cURL. But if you are running Windows I recommend using WAMP as your all-in-one server package, and the odds are good MAMP will behave in a similar fashion. After install startup the server and you’ll notice a small meter icon in your taskbar. Left-click the icon and select PHP -> PHP Extensions -> php_curl which should be checked. If it’s not click it now and restart your server to apply changes. Using a prepackaged server environment will save you a lot of time testing local scripts.

WAMP

If all of this works then congratulations! You should be all set to move along and we can start coding in PHP. So unzip the archived set of PHP wrapper libraries we just downloaded and open the folder. Copy the analytics_api.php file and move this into a new directory in your server root. I have named this folder analytics.

Install the PHP Wrapper

Open whatever text editor you’ll be using to code this script. Create a new PHP page and save it into the same directory you just made, alongside the copied analytics_api.php file. You can name this new file whatever you’d like. But I have chosen index.php to keep up with standards.

You can download the entire project source code to work with if that’s easier. I’ll be breaking down my example code piece-by-piece so we may understand it a little better. To begin we have 2 lines setting an include and Content-Type header.

require_once('analytics_api.php');
header('Content-Type: text/html');

We need to include our analytics_api.php file so we have access to the API library. Inside this file are all the functions needed to pull out page views, settings, account information, etc. The Content-Type header just lets the browser know we are looking to output text in plain HTML. This part isn’t too exciting so let’s move onto the configuration area.

Config Variables

Most of the configuration settings I have placed above all page output. These are used to control the settings for your Analytics account and what type of information you wish to pull out. First off you need to include 3 important data settings. $login should contain your Google E-mail account login, along with your password right below it.

// Config info
$login = ''; // youraccount@gmail.com
$password = '';
$id = ''; // example - ga:1234567

$startdate = strtotime('2011-06-28');
$enddate = strtotime('-1 day');

$filters = array();
$filters['Firefox'] = 'ga:browser=@Firefox';
$filters['Safari'] = 'ga:browser=@Safari';
$filters['Windows'] = 'ga:operatingSystem=@Windows';
$filters['Macintosh'] = 'ga:operatingSystem=@Macintosh';
$filters['iPhone'] = 'ga:operatingSystem=@iPhone';
$metric = 'ga:pageviews'; // alternatively use 'ga:visits' for uniques

Next you’ll see a variable labeled as $id. This will contain your Google Analytics web ID set to the syntax of ga:xxxxxxxxxx without the quotes. To find this number you’ll need to access your Analytics account and find the website you wish to pull data from. Click View Report and in the top URL address bar you should notice an ID number. Add this after the ga: prefix and all of this goes into the $id variable for later.

Google Analytics GA ID

The next 2 settings hold the start and end dates. Starting date may be anything you’d like, while the ending date is always set to one day before the most current. We do this since Google doesn’t have a full data report for 24 hours until the day has concluded, so this shaves off a bit of loading time.

Now our filters will be used to pull out unique data from within Google’s servers. The list is quite expansive and includes a lot more data than just browsers and OS. But for the scope of this tutorial we’ll be testing with the 5 array variables set in my example. You can read up a bit more on the data API from Google Code docs online.

And finally the $metric setting will set which type of numbers we’re pulling for traffic data. Options include page views, uniques, bounces, clicks, time on site… anything used to measure statistics. Check out Google’s full metrics chart to get an idea of the options. We’re sticking with page views since these are solid numbers which can give us an idea of the popularity ratio for browsers and OS’.

Looping Through Data

If you have taken a look at the example code you’ll notice there’s some basic CSS and other styling options. But we’ll be skipping ahead right into the core logic of our PHP application. In this next segment of code we’re looking to create a new instance of the Analytics API and pull account data, then output what we need into HTML.

$api = new analytics_api();
if($api->login($login, $password)) {
    if (strlen($id) == 0) {
	$api->load_accounts();
	exit;
    } else { 
        $api->load_accounts();	
        $last = count($api->accounts) - 1;
	foreach ($api->accounts as $i => $row) {
    	$isFirst = ($i == 0);
    	$isLast = ($i == $last);
			
	if($id == $row['tableId']) { echo '<h3>Account Name: ' . $row['title'] . '</h3>'; }
    }
}

The first operation we perform is to set $api as a new object. This initiates our Analytics class and we now have access to all internal functions. Going with this, we can use an if/else clause to check if the login information provided will work. We call the $api->login() function and pass in the e-mail username and password from above. Assuming this passes we now move into another set of if/else logic.

Google Analytics Training

If we can login but can’t find any ID (meaning you have no Analytics data) we simply exit the app with your account data loaded. Otherwise we still load your account data, but now we can loop through your Analytics profiles to find which ID matches the one given in your config settings. Once we meet a matchup the title data is echoed onto the page in a set of h3 tags.

If you were paying close attention you may notice our original if{} conditional hasn’t been closed yet. This is because we’re still processing the logic from the account login. If we succeed with logging in first we output the analytics profile title. But next we need to output a small table containing our page views for each filter. The code below is the complete finished block of code from above, resting on a final else{} clause to output an error message if login authentication fails.

$from = $startdate;
$n = 0;
echo '<table id="analytics">';
while (true) {
	$till = strtotime('+2 days', $from); // update number here for amount of days skipped for each pull
	if ($till > $enddate) break;
	$n++;

	if($n%2 == 0) { echo '<tr class="first">'; }
	else { echo '<tr class="second">'; }
	echo '<td class="strong">' . date('Y-m-d', $from) . '</td>';
	foreach ($filters as $filter) {
	    $data = $api->data($id, '', $metric, false, date('Y-m-d', $from), date('Y-m-d', $till), 10, 1, urlencode($filter));
	    $mainfilteritem = explode("=@", $filter);
			
	    echo '<td>';
	    echo $mainfilteritem[1] . ' - ';
	    echo ((int)$data[$metric]);
			
	    echo '</td>';
	}
		echo "</tr>";
		$from = strtotime('+1 day', $till);
    }
	echo '</table>';
} else {
	echo "login failed.";

Our $from variable is set to the starting date from earlier. $n is set to 0 so we may pull a while{} loop resulting in each table row, moving forward by 3 days each time. This can be seen directly after our while command where the strtotime() function is set. You may update the value from +2 days to whatever you’d like. If you want to include data from every single day just hide this line from the code, since every run through we also call $n++ by 1 degree.

Just keep in mind that Google can only handle so many server calls. It’s wise to cache your data or store it in a database if you plan on pulling analytics for every single day in a row. But for our small application jumping every 3 days works perfectly. Now we can move on to the next segment of logic which picks apart the metrics data.

First we are checking if $n is divisible by two(2). This is solely for aesthetic feel, since I have designed a zebra stripe pattern on the table layout with CSS. But moving past this we are first outputting page content into a portion of our table holding each date. Running a foreach() loop we can use another function from our library called $api->data(). Inside we pass a few parameters such as Analytics ID and metrics data, and we get out a small array of information.

In our next variable we set another array $mainfilteritem. This can be used to store information for all of our filters which we output in every table row. If this concept seems confusing just check our live demo example below to see how this table comes to form. The foreach loop inside the while loop creates dynamic data on each row many times over. And we close off the loops by ending each table row and proceeding forward by 1 day until we hit the most current data.

[tut demo=”https://onextrapixel.com/examples/google-analytics-data/” download=”https://onextrapixel.com/examples/google-analytics-data/google-analytics-data.zip”]

Conclusion

There are so many types of applications you can build upon with Google Analytics. Their API has been carefully crafted to run over many programming languages, and PHP is just one out of the group. And with PHP we can build fully-scaled web applications to display all kinds of visitor tracking data.

This tutorial should get you a bit more comfortable working with APIs in PHP. The wrapper code we downloaded features a lot of documentation, so it’s simple to look through their library and play around with some more functions. PHP is versatile enough and using the cURL plugins will protect your data transfers between your site and Google’s servers.

Deals

Iconfinder Coupon Code and Review

Iconfinder offers over 1.5 million beautiful icons for creative professionals to use in websites, apps, and printed publications. Whatever your project, you’re sure to find an icon or icon…

WP Engine Coupon

Considered by many to be the best managed hosting for WordPress out there, WP Engine offers superior technology and customer support in order to keep your WordPress sites secure…

InMotion Hosting Coupon Code

InMotion Hosting has been a top rated CNET hosting company for over 14 years so you know you’ll be getting good service and won’t be risking your hosting company…

SiteGround Coupon: 60% OFF

SiteGround offers a number of hosting solutions and services for including shared hosting, cloud hosting, dedicated servers, reseller hosting, enterprise hosting, and WordPress and Joomla specific hosting.