• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Article / PHP / PHP class for using the Google Analytics API

PHP class for using the Google Analytics API

Just quickly following up my post from earlier on today where I looked at how to log into the Google Analytics API using PHP and CURL using Username/Password Authentication I wrapped the login code up into a class with methods for logging in and then calling API functions. I’ll look at parsing the XML result data at a later stage.

Update April 28th 2009: I’ve started a series about this. The full class, which I will continue to update over the next few days, can be downloaded here. The class presented below is an earlier version of it. Original post continues…

Here’s the class:

class analytics_api {
 
 protected $auth;
 
 public function login($email, $password) {
 
 $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_POST, true);
        
         $data = array(
             'accountType' => 'GOOGLE',
             'Email' => $email,
             'Passwd' => $password,
             'service' => 'analytics',
             'source' => ''
         );
        
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         $output = curl_exec($ch);
         $info = curl_getinfo($ch);
         curl_close($ch);
        
         $this->auth = '';
         if($info['http_code'] == 200) {
             preg_match('/Auth=(.*)/', $output, $matches);
             if(isset($matches[1])) {
                 $this->auth = $matches[1];
             }
         }
        
         return $this->auth != '';
    
     }
    
     public function call($url) {
 
         $headers = array("Authorization: GoogleLogin auth=$this->auth");
        
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         $output = curl_exec($ch);
         $info = curl_getinfo($ch);
         curl_close($ch);
 
         if($info['http_code'] == 200) {
             return $output;
         }
         else {
             return false;
         }
        
     }
    
 }

And here’s an example using it, calling https://www.google.com/analytics/feeds/accounts/default which gets a list of all the accounts that are available to view data for.

$api = new analytics_api();
 if($api->login('email@example.com', 'my-secret-password')) {
 $xml = $api->call('https://www.google.com/analytics/feeds/accounts/default');
 // ... process the xml ...
 }
 else {
 // login failed
 }

The class could easily be extended to parse the XML for common functions and have a function for getting the list of accounts into an array (or similar) and so on for other functions.

Update April 28th 2009: I’ve started a series about this and created a PHP class. The full class, which I will continue to update over the next few days, can be downloaded here.

Check Out These Related posts:

  1. Log into the Google Analytics API using PHP and CURL using Username/Password Authentication
  2. The Google Analytics API and PHP: A series
  3. Load the Google Analytics API accounts list with PHP
  4. Parsing Google Analytics data with PHP: A Series

Filed Under: PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap