PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Hillebrand Fernando   Analytics API   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: the example to use de de api class
Class: Analytics API
Retrieve site statistics from Google Analytics
Author: By
Last change:
Date: 12 years ago
Size: 3,369 bytes
 

Contents

Class file image Download
<?php

// this example.php file was last update May 28th 2009, 10:55am NZST

require_once('analytics_api.php');

// enter your login, password and id into the variables below to try it out

$login = 'email@mysite.com';
$password = 'password';

$api = new analytics_api();

if(
$api->login($login, $password)) {

    echo
"login success<br/><br/><pre>";
       
       
$api->load_accounts();
   
   
$tableId = $api->getAccountsTableIdFromName('mysite.com');
   
   
$data = $api->data($tableId, 'ga:date', 'ga:visits','ga:date',date('Y-m-d', strtotime('1 month ago')),date('Y-m-d'),33);
   
    foreach(
$data as $i => $value ) {
           
$fecha = substr($i,6,2) .'/'.substr($i,4,2).'/'.substr($i,0,4);
            echo
$fecha.'de '.$i.'<br>';
        echo(
$value['ga:visits'].'<br><br>');
    }
       
   
// get some account summary information without a dimension
    // change to if(true) to echo the example
   
if(false) {
       
$data = $api->data($tableId, '', 'ga:bounces,ga:newVisits,ga:visits,ga:pageviews,ga:uniquePageviews');
        echo
'<pre>';
        foreach(
$data as $metric => $count) {
            echo
"$metric: $count\n";
        }
    }

   
// get the pagePath vs pageviews & unique pageviews
    // change to if(true) to echo the example
   
if(false) {
       
       
$data = $api->data($tableId, 'ga:pagePath', 'ga:pageviews,ga:uniquePageviews');
       
//print_r($data);
       
        // how to loop through the data
       
foreach($data as $dimension => $metrics) {
            echo
"$dimension pageviews: {$metrics['ga:pageviews']} unique pageviews: {$metrics['ga:uniquePageviews']}\n";
        }
       
    }

   
// get the browser vs visits & pageviews
    // change to if(true) to echo the example
   
if(false) {
   
       
$data = $api->data($tableId, 'ga:browser,ga:browserVersion', 'ga:visits,ga:pageviews', false, false, false, 100);
       
print_r($data);
               
/*
        // you can then access the metrics for a specific dimension vs metric like e.g.
        echo $data['Internet Explorer']['8.0']['ga:pageviews'], "\n";
        // or loop through the data
        foreach($data as $dimension1 => $array) {
            foreach($array as $dimension2 => $metrics) {
                echo "$dimension1 $dimension2 visits: {$metrics['ga:visits']} pageviews: {$metrics['ga:pageviews']}\n";
            }
        }*/
       
   
}
   
    if(
false) {
       
// get data filtered by canada, using a string as the filters parameter
       
$data = $api->data($tableId, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, 'ga:country%3d%3dCanada');
       
print_r($data);
       
// get data filtered by canada and firefox browser, using a string as the filters parameter
       
$data = $api->data($tableId, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, 'ga:country%3d%3dCanada;ga:browser%3d@Firefox');
       
print_r($data);
       
// same as the second example above but using the filtering class
       
$filters = new analytics_filters('ga:country', '==', 'Canada');
       
$filters->add_and('ga:browser', '=@', 'Firefox');
       
$data = $api->data($tableId, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, $filters);
       
print_r($data);
       
// using the filtering class to filter where USA or Canada
       
$filters = new analytics_filters('ga:country', '==', 'Canada');
       
$filters->add_or('ga:country', '==', 'United States');
       
$data = $api->data($tableId, '', 'ga:visits,ga:pageviews', false, false, false, 10, 1, $filters);
       
print_r($data);
    }
   
}
else {

    echo
"login failed\n";
   
}