Created
December 27, 2014 11:49
-
-
Save rakuishi/f06f4cbca1e320fd6821 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(dirname(__FILE__) . '/google-api-php-client/autoload.php'); | |
define('APPLICATION_NAME', '*'); | |
define('SERVICE_ACCOUNT_NAME', '*@developer.gserviceaccount.com'); | |
define('KEY_PATH', dirname(__FILE__) . '/p12/*.p12'); | |
date_default_timezone_set('Asia/Tokyo'); | |
class Analytics { | |
private $service; | |
function __construct($applicationName, $serviceAccountName, $keyPath) { | |
$creds = new Google_Auth_AssertionCredentials( | |
$serviceAccountName, | |
array('https://www.googleapis.com/auth/analytics'), | |
file_get_contents($keyPath) | |
); | |
$client = new Google_Client(); | |
$client->setApplicationName($applicationName); | |
$client->setAssertionCredentials($creds); | |
$this->service = new Google_Service_Analytics($client); | |
} | |
function getReport($viewId, $startDate, $endDate) { | |
$result = $this->service->data_ga->get( | |
'ga:' . $viewId, | |
$startDate, | |
$endDate, | |
'ga:pageviews,ga:users,ga:screenviews' | |
); | |
return $result['rows']; | |
} | |
} | |
$analytics = new Analytics(APPLICATION_NAME, SERVICE_ACCOUNT_NAME, KEY_PATH); | |
$viewId = '*'; | |
$yesterday = @date('Y-m-d', strtotime('-1 day')); | |
var_dump($analytics->getReport($viewId, $yesterday, $yesterday)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
イベントを取得する場合は、以下のように書く。