最近公司執行一個專案,是需要使用CodeIgniter搭配Google Api的,其中GA是主要功能之一,順手整理一下目前用到的
1、先把Google Lib載回來,放在目錄「application/third_party」下
2、建立controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * GAnalytics - Google Analytics PHP Interface for CodeIgniter * Uses google php api https://github.com/google/google-api-php-client * * Support material: * How to set-up the app and configure the analytics profile http://stackoverflow.com/a/10089698/1666071 * Analytics query explorer http://ga-dev-tools.appspot.com/explorer/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * @author Iacami Gevaerd <enapupe@gmail.com> * @version 1 * */ class GAnalytics extends CI_Controller { private $client; private $service; public function __construct(){ set_include_path(get_include_path() . PATH_SEPARATOR . APPPATH.'third_party/'); require_once(APPPATH . 'third_party/Google/Client.php'); require_once(APPPATH . 'third_party/Google/Service/Analytics.php'); } public function setup($app_name, $credentials){ // create client object and set app name $this->client = new Google_Client(); $this->client->setApplicationName($app_name); // name of your app // set assertion credentials $this->client->setAssertionCredentials( new Google_Auth_AssertionCredentials( $credentials['email'], // email you added to GA array('https://www.googleapis.com/auth/analytics.readonly'), $credentials['key'] // keyfile you downloaded )); // other settings $this->client->setClientId($credentials['client_id']);// from API console $this->client->setAccessType('offline_access');// this may be unnecessary? // create service and get data $this->service = new Google_Service_Analytics($this->client); } public function data($ids, $startDate, $endDate, $metrics, $optParams = array()){ return $this->service->data_ga->get($ids, $startDate, $endDate, $metrics, $optParams); } } |
3、demo php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $credentials['key'] = file_get_contents('path/to/keyfile-privatekey.p12'); // keyfile you downloaded $credentials['client_id'] = ''; // from API console $credentials['email'] = ''; // email you added to GA $this->load->library("GAnalytics", array('appname', $credentials)); $this->ganalytics->setup('appname', $credentials); //query: amount of visits/newvisits/pageviews by day $g = $this->ganalytics->data('ga:10025405', date('Y-d-m', strtotime('now - 30 days')), date('Y-d-m', strtotime('now - 1 days')), 'ga:visits,ga:newVisits,ga:pageviews' , array( 'dimensions' => "ga:date" ) ); |