Google AnalyticsのAPIをたたいてみたのでメモメモ
google-api-php-client を使うと
あっというまに実装できちゃう
便利ですなぁ~
require_once APP . 'vendor/google-api-php-client/src/Google/autoload.php'; $email = 'サービスアカウントのメールアドレス'; $key = '秘密キーファイル(P12キー)の読み込み'; $veiwId = 'プロファイル(ビュー)ID'; // Googleクライアントのインスタンスを作成 $client = new Google_Client(); // クレデンシャルの作成 $cred = new Google_Auth_AssertionCredentials( $email, array(Google_Service_Analytics::ANALYTICS_READONLY), $key ); $client->setAssertionCredentials($cred); if($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $analytics = new Google_Service_Analytics($client); $result = $analytics->data_ga->get( 'ga:' . $veiwId, // アナリティクス ビュー ID '2017-01-01', // 日付FROM '2017-02-10', // 日付FTO 'ga:sessions,ga:percentNewSessions,ga:newUsers,ga:visitBounceRate,ga:pageviewsPerVisit,ga:avgSessionDuration,ga:goalConversionRateAll,ga:goalCompletionsAll' ); // 結果を出力 var_dump($result->rows);
コメント