..


Sponsored Links

Use Google Analytics API with PHP

Article written by Ciro Cardone
Page 1 of 4

Google Analytics is the most popular service to monitor the performance of a website.
Google Analytics provides detailed statistics - packaged in an elegant user interface that makes extensive use of AJAX technology - thanks to which we can access a range of data about who visits our websites. This is supported also by a huge number of search options and filters through which traffic analysis of our web-site will be incredibly accurate and detailed.

What are the Google Analytics API

In 2009, Google has decided to provide users of the API to query, through external applications, the statistics of a given account, with great joy of the developers, since then, have a powerful tool for integrating data within Analytics their applications.
In order to use APIs Analytics are required:
  • A Google Account
  • An active profile on Analytics
The mechanism for retrieving data using the API works through several stages:
  1. Google Account Authentication (using email and password);
  2. Identification of the Analytics profile from which to extract the data;
  3. Query (query) to obtain the data required;
  4. Retrieve the result.

The requests are fulfilled using REST services, then just implement a simple HTTP call to receive a response as XML.
One way to make requests to the API is to use the PHP cURL extension and then parse the resulting XML with the most useful and powerful class SimpleXML xpath syntax.

For those who did not have the time or inclination to implement a customized class that can authenticate and parse the data, I report the existence of an open-source can do all the work for us.
The project is called GAPI (which stands for Google Analytics PHP interface) and you can get, with lots of documentation and usage examples to this page on Google Code.

What GAPI

GAPI is a great class, thanks to which we astrarci the atomic operations required to connect, authenticate and retrieve data from our Google Analytics profile.

The core of the package - you can download from here - GAPI is the class that takes as good practice, we find in the file gapi.class.php. Complete the endowment of this version (1.3 at the time of writing) 4 sample files and a readme file.

The code is well written, robust and ordered as required. We see, briefly, its main features:

  1. Support for cURL and fopen HTTP as the connection method (the selection is made ​​of real bees dynamically at runtime)
  2. Using PHP array to specify the metrics and dimensions of Google Analytics that we will see later
  3. Account mapping available in objects and getters for parameters
  4. Reporting mapping data objects - getters for metrics and parameters
  5. Filtering mechanism implemented with GAPI query language
  6. PHP5 Object Oriented code full robust and easy to integrate into our applications

Use GAPI

Now that we have a rough idea of ​​what it offers, let us integrate the class in a GAPI our simple application.

The structure that I propose is quite simple: our application is composed of only three files:

  1. conf.ini
  2. gapi.class.php
  3. process.php

Let's see the contents of our configuration file:






 ; Application properties







 google_username = "google_username"







 google_password = "google_password"







 ga_id_profilo = "id_profilo"



It 'important to remember to protect the file so that it is conf.ini accessed from the Web (eg using a htaccess file)

Let us analyze the file process.php (which contains the actual instructions of our web application):






 require_once 'gapi.class.php';









 $ Properties = parse_ini_file ("conf.ini");







 $ GapiObj = new GAPI ($ properties ["google_username"], $ properties ["google_password"]);



Currently we have done is include the class GAPI, load our properties in an array variable and instantiate our "clients". The second part of our small application that will, I assure you, much more interesting ...

In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
MySQL (Course) MySQL (Course)
Management of open-source database. From 39 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Sponsored Links