Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zendgdata.analytics.html

Size:14598
Storage flags:no_autoload,compress/gzip (25%)

Using Google Analytics — Zend Framework 2 2.4.2 documentation

Using Google Analytics

The Google Analytics API allows client applications to request data, saved in the analytics accounts.

See http://code.google.com/apis/analytics/docs/gdata/v2/gdataOverview.html for more information about the Google Analytics API.

Retrieving account data

Using the account feed, you are able to retrieve a list of all the accounts available to a specified user.

1
2
3
4
5
6
7
8
$service = ZendGData\Analytics::AUTH_SERVICE_NAME;
$client = ZendGData\ClientLogin::getHttpClient($email, $password, $service);
$analytics = new ZendGData\Analytics($client);
$accounts = $analytics->getAccountFeed();

foreach ($accounts as $account) {
  echo "\n{$account->title}\n";
}

The $analytics->getAccountFeed() call, results in a ZendGData\Analytics\AccountFeed object that contains ZendGData\Analytics\AccountEntry objects. Each of this objects represent a google analytics account.

Retrieving report data

Besides the account feed, google offers a data feed, to retrieve report data using the Google Analytics API. To easily request for these reports, ZendGDataAnalytics offers a simple query construction interface. You can use all the metrics and dimensions specified by the API. Additionally you can apply some filters to retrieve some common data or even complex results.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$query = $service->newDataQuery()->setProfileId($profileId)
  ->addMetric(ZendGData\Analytics\DataQuery::METRIC_BOUNCES)
  ->addMetric(ZendGData\Analytics\DataQuery::METRIC_VISITS)
  ->addDimension(ZendGData\Analytics\DataQuery::DIMENSION_MEDIUM)
  ->addDimension(ZendGData\Analytics\DataQuery::DIMENSION_SOURCE)
  ->addFilter("ga:browser==Firefox")
  ->setStartDate('2011-05-01')
  ->setEndDate('2011-05-31')
  ->addSort(ZendGData\Analytics\DataQuery::METRIC_VISITS, true)
  ->addSort(ZendGData\Analytics\DataQuery::METRIC_BOUNCES, false)
  ->setMaxResults(50);

$result = $analytics->getDataFeed($query);
foreach ($result as $row) {
  echo $row->getMetric('ga:visits')."\t";
  echo $row->getValue('ga:bounces')."\n";
}

Table Of Contents

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Using Google Analytics on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net