dev-master
9999999-devSimple class to query the Google Analytics API v3 with PHP
GPLv3
The Requires
- ext-openssl *
- ext-curl *
The Development Requires
by Stefan Wanzenried
Simple class to query the Google Analytics API v3 with PHP
This is a simple class to use OAuth 2.0 with Google and query the Google Analytics API v3 with PHP. cURL and OpenSSL are required!, (*1)
The class supports getting the access tokens for either web applications and service accounts registered in the Google APIs console., (*2)
See the Google OAuth2 documentation for further information., (*3)
Despite my best efforts, I still have not reached a state of perfection. Feel free to either: * Add new issues to the bug tracker * Add a pull request, (*4)
This is a refactor of wanze/Google-Analytics-API-PHP
. You should add this repository into your composer.json file!, (*5)
Depending on the chosen application type, the setup is slightly different. This section describes both ways independently., (*6)
include('vendor/autoload.php'); use timgws\GoogleAnalytics\API as Analytics; $ga = new Analytics(); $ga->auth->setClientId('your_client_id'); // From the APIs console $ga->auth->setClientSecret('your_client_secret'); // From the APIs console $ga->auth->setRedirectUri('redirect_uri'); // Url to your app, must match one in the APIs console // Get the Auth-Url $url = $ga->auth->buildAuthUrl();
Provide a link to the Auth-Url. The user has to log in with his Google Account, accept that your App will access the Analytics Data. After completing this steps, the user will be redirected back to the redirect-uri along with a code. This code is needed to get the tokens., (*7)
$code = $_GET['code']; $auth = $ga->auth->getAccessToken($code); // Try to get the AccessToken if ($auth['http_code'] == 200) { $accessToken = $auth['access_token']; $refreshToken = $auth['refresh_token']; $tokenExpires = $auth['expires_in']; $tokenCreated = time(); } else { // error... }
With the accessToken you can query the API for the given time (seconds) in $tokenExpires. If you need to query the API beyond this time, you should store the refreshToken along with a timestamp in the Database / Session. If the accessToken expires, you can get a new one with the refreshToken., (*8)
// Check if the accessToken is expired if ((time() - $tokenCreated) >= $tokenExpires) { $auth = $ga->auth->refreshAccessToken($refreshToken); // Get the accessToken as above and save it into the Database / Session }
Copy the email address from the APIs console (xxxxxxxx@developer.gserviceaccount.com). Visit your GA admin and add this email as a user to your properties., (*9)
include('vendor/autoload.php'); use timgws\GoogleAnalytics\API as Analytics; $ga = new Analytics('service'); $ga->auth->setClientId('your_client_id'); // From the APIs console $ga->auth->setEmail('your_email_addy'); // From the APIs console $ga->auth->setPrivateKey('/super/secure/path/to/your/privatekey.p12'); // Path to the .p12 file
To query the API, you need to obtain an access token. This token is valid one hour, afterwards you'll need to get a new token. You can store the token in the database/session., (*10)
$auth = $ga->auth->getAccessToken(); // Try to get the AccessToken if ($auth['http_code'] == 200) { $accessToken = $auth['access_token']; $tokenExpires = $auth['expires_in']; $tokenCreated = time(); } else { // error... }
Before you can query the API, you need the ID of the Account you want to query the data. The ID can be found like this:, (*11)
// Set the accessToken and Account-Id $ga->setAccessToken($accessToken); $ga->setAccountId('ga:xxxxxxx'); // Load profiles $profiles = $ga->getProfiles(); $accounts = array(); foreach ($profiles['items'] as $item) { $id = "ga:{$item['id']}"; $name = $item['name']; $accounts[$id] = $name; } // Print out the Accounts with Id => Name. Save the Id (array-key) of the account you want to query data. // See next chapter how to set the account-id. print_r($accounts);
Once you have a valid accessToken and an Account-ID, you can query the Google Analytics API. You can set some default Query Parameters that will be executed with every query., (*12)
// Set the accessToken and Account-Id $ga->setAccessToken($accessToken); $ga->setAccountId('ga:xxxxxxx'); // Set the default params. For example the start/end dates and max-results $defaults = array( 'start-date' => date('Y-m-d', strtotime('-1 month')), 'end-date' => date('Y-m-d'), ); $ga->setDefaultQueryParams($defaults); // Example1: Get visits by date $params = array( 'metrics' => 'ga:visits', 'dimensions' => 'ga:date', ); $visits = $ga->query($params); // Example2: Get visits by country $params = array( 'metrics' => 'ga:visits', 'dimensions' => 'ga:country', 'sort' => '-ga:visits', 'max-results' => 30, 'start-date' => '2013-01-01' //Overwrite this from the defaultQueryParams ); $visitsByCountry = $ga->query($params); // Example3: Same data as Example1 but with the built in method: $visits = $ga->getVisitsByDate(); // Example4: Get visits by Operating Systems and return max. 100 results $visitsByOs = $ga->getVisitsBySystemOs(array('max-results' => 100)); // Example5: Get referral traffic $referralTraffic = $ga->getReferralTraffic(); // Example6: Get visits by languages $visitsByLanguages = $ga->getVisitsByLanguages();
A dusty GAPI wrapper is included in gapi_wrapper/
, (*13)
Simple class to query the Google Analytics API v3 with PHP
GPLv3