A. Google Analytics

To access analytics services you need to grab an access token by authenticating first. The analytics scope when generating a URL can be passed by simply setting 'analytics' to getLoginURL(). Figure 1 shows how we can obtain an access token for Google Analytics.

Figure 1. Authentication
session_start();

$auth = eden('google')->auth('[CLIENT_ID]', '[CLIENT_SECRET]', 'http://yourwebsite.com/auth', '[API_KEY]');

//if no code and no session
if(!isset($_GET['code']) && !isset($_SESSION['token'])) {
    //redirect to login
    $login = $auth->getLoginUrl('analytics');
    header('Location: '.$login);
    exit;
}

//Code is returned back from google
if(isset($_GET['code'])) {
    //save it to session
	$access = $auth->getAccess($_GET['code']);
    $_SESSION['token'] = $access['access_token'];
	
}

Please keep in mind that your rights are limited based on the privilages set in your actual analytics admin. When we have our token we can then access the different service levels of Google Analytics.

Figure 2. Accessing Analytics Services
$reporting 	= eden('google')->analytics($_SESSION['token'])->reporting();
$channel 	= eden('google')->analytics($_SESSION['token'])->multichanel();
$management = eden('google')->analytics($_SESSION['token'])->management();

© 2012 Openovate Labs. All rights reserved.