Skip to content
This repository has been archived by the owner on Dec 6, 2019. It is now read-only.

Commit

Permalink
Merge branch 'gdata-analytics' of https://github.com/danielmitd/ZendG…
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Aug 21, 2012
2 parents 87fcf9b + 5e2f22b commit 17e6ae3
Show file tree
Hide file tree
Showing 13 changed files with 511 additions and 229 deletions.
42 changes: 27 additions & 15 deletions library/ZendGData/Analytics.php
Expand Up @@ -20,51 +20,53 @@
class Analytics extends GData
{
const AUTH_SERVICE_NAME = 'analytics';
const ANALYTICS_FEED_URI = 'https://www.google.com/analytics/feeds';
const ANALYTICS_ACCOUNT_FEED_URI = 'https://www.google.com/analytics/feeds/accounts';
const ANALYTICS_FEED_URI = 'https://www.googleapis.com/analytics/v2.4/data';
const ANALYTICS_ACCOUNT_FEED_URI = 'https://www.googleapis.com/analytics/v2.4/management/accounts';

public static $namespaces = array(
array('ga', 'http://schemas.google.com/analytics/2009', 1, 0)
array('analytics', 'http://schemas.google.com/analytics/2009', 1, 0),
array('ga', 'http://schemas.google.com/ga/2009', 1, 0)
);

/**
* Create Gdata object
*
* @param Client $client
* @param \Zend\Http\Client $client
* @param string $applicationId The identity of the app in the form of
* Company-AppName-Version
*/
public function __construct(Client $client = null, $applicationId = 'MyCompany-MyApp-1.0')
{
$this->registerPackage('Zend_Gdata_Analytics');
$this->registerPackage('Zend_Gdata_Analytics_Extension');
$this->registerPackage('ZendGdata\Analytics');
$this->registerPackage('ZendGdata\Analytics\Extension');
parent::__construct($client, $applicationId);
$this->_httpClient->setParameterPost(array('service' => self::AUTH_SERVICE_NAME));
}

/**
* Retrieve account feed object
*
* @param string|\Zend\Uri\Uri $uri
* @return Analytics\AccountFeed
*/
public function getAccountFeed()
public function getAccountFeed($uri = self::ANALYTICS_ACCOUNT_FEED_URI)
{
$uri = self::ANALYTICS_ACCOUNT_FEED_URI . '/default?prettyprint=true';
if ($uri instanceof Query) {
$uri = $uri->getQueryUrl();
}
return parent::getFeed($uri, 'ZendGData\Analytics\AccountFeed');
}

/**
* Retrieve data feed object
*
* @param mixed $location
* @param string|\Zend\Uri\Uri $uri
* @return Analytics\DataFeed
*/
public function getDataFeed($location = self::ANALYTICS_FEED_URI)
public function getDataFeed($uri = self::ANALYTICS_FEED_URI)
{
if ($location instanceof Query) {
$uri = $location->getQueryUrl();
} else {
$uri = $location;
if ($uri instanceof Query) {
$uri = $uri->getQueryUrl();
}
return parent::getFeed($uri, 'ZendGData\Analytics\DataFeed');
}
Expand All @@ -78,4 +80,14 @@ public function newDataQuery()
{
return new Analytics\DataQuery();
}

/**
* Returns a new AccountQuery object.
*
* @return Analytics\AccountQuery
*/
public function newAccountQuery()
{
return new Analytics\AccountQuery();
}
}
13 changes: 10 additions & 3 deletions library/ZendGData/Analytics/AccountEntry.php
Expand Up @@ -26,9 +26,11 @@ class AccountEntry extends ZendGData\Entry
protected $_currency;
protected $_timezone;
protected $_tableId;
protected $_profileName;
protected $_goal;

/**
* @see Zend_Gdata_Entry::__construct()
* @see ZendGData\Entry::__construct()
*/
public function __construct($element = null)
{
Expand All @@ -44,16 +46,21 @@ protected function takeChildFromDOM($child)
{
$absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
switch ($absoluteNodeName){
case $this->lookupNamespace('ga') . ':' . 'property';
case $this->lookupNamespace('analytics') . ':' . 'property';
$property = new Extension\Property();
$property->transferFromDOM($child);
$this->{$property->getName()} = $property;
break;
case $this->lookupNamespace('ga') . ':' . 'tableId';
case $this->lookupNamespace('analytics') . ':' . 'tableId';
$tableId = new Extension\TableId();
$tableId->transferFromDOM($child);
$this->_tableId = $tableId;
break;
case $this->lookupNamespace('ga') . ':' . 'goal';
$goal = new Extension\Goal();
$goal->transferFromDOM($child);
$this->_goal = $goal;
break;
default:
parent::takeChildFromDOM($child);
break;
Expand Down
177 changes: 177 additions & 0 deletions library/ZendGData/Analytics/AccountQuery.php
@@ -0,0 +1,177 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_GData
*/

namespace ZendGData\Analytics;

use ZendGData;

/**
* @category Zend
* @package Zend_Gdata
* @subpackage Analytics
*/
class AccountQuery extends ZendGData\Query
{
const ANALYTICS_FEED_URI = 'https://www.googleapis.com/analytics/v2.4/management/accounts';

/**
* The default URI used for feeds.
*/
protected $_defaultFeedUri = self::ANALYTICS_FEED_URI;

/**
* @var string
*/
protected $_accountId = '~all';
/**
* @var string
*/
protected $_webpropertyId = '~all';
/**
* @var string
*/
protected $_profileId = '~all';

/**
* @var bool
*/
protected $_webproperties = false;
/**
* @var bool
*/
protected $_profiles = false;
/**
* @var bool
*/
protected $_goals = false;

/**
* @param string $accountId
* @return AccountQuery
*/
public function setAccountId($accountId)
{
$this->_accountId = $accountId;
return $this;
}

/**
* @return string
*/
public function getAccountId()
{
return $this->_accountId;
}

/**
* @param string $webpropertyId
* @return AccountQuery
*/
public function setWebpropertyId($webpropertyId)
{
$this->_webpropertyId = $webpropertyId;
return $this;
}

/**
* @return string
*/
public function getWebpropertyId()
{
return $this->_webpropertyId;
}

/**
* @param string $profileId
* @return AccountQuery
*/
public function setProfileId($profileId)
{
$this->_profileId = $profileId;
return $this;
}

/**
* @return string
*/
public function getProfileId()
{
return $this->_profileId;
}

/**
* @param string $accountId
* @return AccountQuery
*/
public function webproperties($accountId = '~all')
{
$this->_webproperties = true;
$this->setAccountId($accountId);
return $this;
}

/**
* @param string $webpropertyId
* @param string $accountId
* @return AccountQuery
*/
public function profiles($webpropertyId = '~all', $accountId = '~all')
{
$this->_profiles = true;
if (null !== $accountId) {
$this->setAccountId($accountId);
}
$this->setWebpropertyId($webpropertyId);
return $this;
}

/**
* @param string $webpropertyId
* @param string $accountId
* @param string $accountId
* @return AccountQuery
*/
public function goals($profileId = '~all', $webpropertyId = '~all', $accountId = '~all')
{
$this->_goals = true;
if (null !== $accountId) {
$this->setAccountId($accountId);
}
if (null !== $webpropertyId) {
$this->setWebpropertyId($webpropertyId);
}
$this->setProfileId($profileId);
return $this;
}

/**
* @return string url
*/
public function getQueryUrl()
{
$url = $this->_defaultFeedUri;

// add account id
if ($this->_webproperties or $this->_profiles or $this->_goals) {
$url .= '/' . $this->_accountId . '/webproperties';
}

if ($this->_profiles or $this->_goals) {
$url .= '/' . $this->_webpropertyId . '/profiles';
}

if ($this->_goals) {
$url .= '/' . $this->_profileId . '/goals';
}

$url .= $this->getQueryString();
return $url;
}
}
4 changes: 2 additions & 2 deletions library/ZendGData/Analytics/DataEntry.php
Expand Up @@ -45,12 +45,12 @@ protected function takeChildFromDOM($child)
{
$absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
switch ($absoluteNodeName) {
case $this->lookupNamespace('ga') . ':' . 'dimension';
case $this->lookupNamespace('analytics') . ':' . 'dimension';
$dimension = new Extension\Dimension();
$dimension->transferFromDOM($child);
$this->_dimensions[] = $dimension;
break;
case $this->lookupNamespace('ga') . ':' . 'metric';
case $this->lookupNamespace('analytics') . ':' . 'metric';
$metric = new Extension\Metric();
$metric->transferFromDOM($child);
$this->_metrics[] = $metric;
Expand Down
2 changes: 1 addition & 1 deletion library/ZendGData/Analytics/DataQuery.php
Expand Up @@ -19,7 +19,7 @@
*/
class DataQuery extends ZendGData\Query
{
const ANALYTICS_FEED_URI = 'https://www.google.com/analytics/feeds/data';
const ANALYTICS_FEED_URI = 'https://www.googleapis.com/analytics/v2.4/data';

/**
* The default URI used for feeds.
Expand Down
39 changes: 39 additions & 0 deletions library/ZendGData/Analytics/Extension/Goal.php
@@ -0,0 +1,39 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_GData
*/

namespace ZendGData\Analytics\Extension;

use ZendGData;

/**
* @category Zend
* @package Zend_Gdata
* @subpackage Analytics
*/
class Goal extends ZendGData\Extension
{
protected $_rootNamespace = 'ga';
protected $_rootElement = 'goal';

public function __construct()
{
$this->registerAllNamespaces(ZendGData\Analytics::$namespaces);
parent::__construct();
}

/**
* @return string
*/
public function __toString()
{
$attribs = $this->getExtensionAttributes();
return $attribs['name']['value'];
}
}
3 changes: 2 additions & 1 deletion library/ZendGData/Analytics/Extension/Property.php
Expand Up @@ -47,7 +47,8 @@ protected function takeAttributeFromDOM($attribute)
{
switch ($attribute->localName) {
case 'name':
$this->_name = substr($attribute->nodeValue, 3);
$name = explode(':', $attribute->nodeValue);
$this->_name = end($name);
break;
case 'value':
$this->_value = $attribute->nodeValue;
Expand Down

0 comments on commit 17e6ae3

Please sign in to comment.