Skip to content

Commit

Permalink
Fixes for fullTableName error and GA format changes
Browse files Browse the repository at this point in the history
- fixed format changes on accounts
- now using tableId instead of profileId as account identifier
- metrics is now required
  • Loading branch information
msadouni committed Jan 20, 2010
1 parent 15b9079 commit e0cb7f3
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 53 deletions.
10 changes: 5 additions & 5 deletions README.markdown
Expand Up @@ -23,9 +23,9 @@ This will return an array of Accounts :
[id] => account url
[updated] => last update datetime
[title] => account title
[tableId] => ga:profileId
[tableId] => tableId (the id you need to perform searches on)
[accountId] => account id
[profileId] => profile id (the id you need to perform searches next)
[profileId] => profile id
[webPropertyId] => tracker id on your website
[1] =>
[Account] => ...
Expand All @@ -35,15 +35,15 @@ Grab the Account.profileId you need and get the Account data :

$data = $this->GoogleAnalyticsAccount->find('first', array(
'conditions' => array(
'profileId' => $profileId,
'tableId' => $tableId,
'start-date' => 'YYYY-MM-DD',
'end-date' => 'YYYY-MM-DD')));

The `start-date` and `end-date` conditions are mandatory. You can add other conditions to perform searches :

$data = $this->GoogleAnalyticsAccount->find('first', array(
'conditions' => array(
'profileId' => $profileId,
'tableId' => $tableId,
'start-date' => 'YYYY-MM-DD',
'end-date' => 'YYYY-MM-DD'
'dimensions' => 'country',
Expand All @@ -54,7 +54,7 @@ will get you the new visits per country, ordered by descending new visits, for t

$data = $this->GoogleAnalyticsAccount->find('first', array(
'conditions' => array(
'profileId' => $profileId,
'tableId' => $tableId,
'start-date' => 'YYYY-MM-DD',
'end-date' => 'YYYY-MM-DD'
'dimensions' => array('country', 'city'),
Expand Down
30 changes: 15 additions & 15 deletions controllers/google_analytics_controller.php
Expand Up @@ -9,23 +9,23 @@ function index()
$this->set(compact('accounts'));
}

function show($profileId = null)
function show($tableId = null)
{
if (empty($profileId))
if (empty($tableId))
{
if (empty($this->params['named']['profileId']))
if (empty($this->params['named']['tableId']))
{
$this->redirect(array('action' => 'index'));
}
$profileId = $this->params['named']['profileId'];
$tableId = $this->params['named']['tableId'];
}

$params = array(
'start-date' => strftime('%Y-%m-%d'),
'end-date' => strftime('%Y-%m-%d'),
'dimensions' => '',
'metrics' => '',
'sort' => '');
'dimensions' => array(),
'metrics' => array('newVisits'),
'sort' => array());

if (!empty($this->params['named']['start-date']))
{
Expand Down Expand Up @@ -53,32 +53,32 @@ function show($profileId = null)
',', str_replace(' ', '', $this->params['named']['sort']));
}
$conditions['conditions'] = array_merge(
array('profileId' => $profileId), $params);
array('tableId' => $tableId), $params);

$account = $this->GoogleAnalyticsAccount->find('first', $conditions);

$start_date = $params['start-date'];
$end_date = $params['end-date'];
$dimensions = join(',', $params['dimensions']);
$metrics = join(',', $params['metrics']);
$sort = join(',', $params['sort']);
$dimensions = join($params['dimensions'], ',');
$metrics = join($params['metrics'], ',');
$sort = join($params['sort'], ',');
$dimensionsArray = $params['dimensions'];
$metricsArray = $params['metrics'];
$sortArray = $params['sort'];

$this->set(compact(
'account', 'start_date', 'end_date', 'dimensions', 'metrics', 'sort', 'dimensionsArray', 'metricsArray', 'sortArray'));
$this->set('profileId', $profileId);
$this->set('tableId', $tableId);
}

function search()
{
$params = $this->params['form'];
if (empty($params['profileId']))
if (empty($params['tableId']))
{
$this->redirect(array('action' => 'index'));
}
$profileId = $params['profileId'];
$tableId = $params['tableId'];
$start_date = $end_date = $dimensions = $metrics = $sort = '';
if (!empty($params['start-date']))
{
Expand All @@ -102,7 +102,7 @@ function search()
}
$this->redirect(array(
'action' => 'show',
'profileId' => $profileId,
'tableId' => $tableId,
'start-date' => $start_date,
'end-date' => $end_date,
'dimensions' => $dimensions,
Expand Down
54 changes: 38 additions & 16 deletions models/datasources/google_analytics_source.php
Expand Up @@ -10,6 +10,7 @@ class GoogleAnalyticsSource extends DataSource
'datasource' => 'google_analytics',
'Email' => '',
'Passwd' => '');
var $cacheSources = false;

function __construct($config, $autoConnect = true)
{
Expand Down Expand Up @@ -67,7 +68,7 @@ function listSources()

function read(&$model, $queryData)
{
if (!empty($queryData['conditions']['profileId']))
if (!empty($queryData['conditions']['tableId']))
{
return $this->account_data($queryData);
}
Expand All @@ -84,23 +85,23 @@ function accounts()
{
// sometimes the the keys are ucfirst'ed, sometimes not...
$entry = array_change_key_case($entry['Entry'], CASE_LOWER);
$accountId = Set::extract(
'/property[name=ga:accountId]/value', $entry);
$accountName = Set::extract(
'/property[name=ga:accountName]/value', $entry);
$profileId = Set::extract(
'/property[name=ga:profileId]/value', $entry);
$webPropertyId = Set::extract(
'/property[name=ga:webPropertyId]/value', $entry);
$accountId = $this->__extract_property_value(
$entry, 'accountId');
$accountName = $this->__extract_property_value(
$entry, 'accountName');
$profileId = $this->__extract_property_value(
$entry, 'profileId');
$webPropertyId = $this->__extract_property_value(
$entry, 'webPropertyId');
$account = array('Account' => array(
'id' => $entry['id'],
'updated' => $entry['updated'],
'title' => $entry['title']['value'],
'tableId' => $entry['tableid'],
'accountId' => $accountId[0],
'accountName' => $accountName[0],
'profileId' => $profileId[0],
'webPropertyId' => $webPropertyId[0]));
'tableId' => str_replace('ga:', '', $entry['tableid']),
'accountId' => $accountId,
'accountName' => $accountName,
'profileId' => $profileId,
'webPropertyId' => $webPropertyId));
$data[] = $account;
}
return $data;
Expand All @@ -113,7 +114,7 @@ function account_data($queryData)
$conditions = $queryData['conditions'];

$defaultParams = array(
'ids' => 'ga:'.$conditions['profileId'],
'ids' => 'ga:'.$conditions['tableId'],
'start-date' => strftime(
'%Y-%m-%d', strtotime($conditions['start-date'])),
'end-date' => strftime(
Expand All @@ -128,7 +129,7 @@ function account_data($queryData)
// the result must be returned as [0 => Account => [...]]
// because find('first') returns results[0]
$data = array(array('Account' => array(
'profileId' => $feed['DataSource']['tableId'],
'tableId' => $feed['DataSource']['tableId'],
'name' => $feed['DataSource']['tableName'],
'totalResults' => $feed['totalResults'],
'startIndex' => $feed['startIndex'],
Expand All @@ -152,6 +153,11 @@ function __validateQueryData($queryData)
trigger_error(__('end-date is required', true), E_USER_ERROR);
return null;
}
if (empty($conditions['metrics']))
{
trigger_error(__('metrics is required', true), E_USER_ERROR);
return null;
}
if (strtotime($conditions['start-date']) >
strtotime($conditions['end-date']))
{
Expand Down Expand Up @@ -351,4 +357,20 @@ function __to_array($response = '')
unset($xml);
return $array;
}

function __extract_property_value($entry, $property) {
if (empty($entry) || empty($property)) {
return '';
}
if (!is_array($entry) || !is_string($property)) {
return '';
}
$value = Set::extract(
"/property[name=ga:$property]/value", $entry);

if (!empty($value[0])) {
return $value[0];
}
return '';
}
}
93 changes: 78 additions & 15 deletions tests/cases/datasources/google_analytics_source.test.php
Expand Up @@ -73,12 +73,21 @@ function test___validateQueryData()
'start-date' => '2009-01-01')));
$this->assertError('end-date is required');
$this->assertIdentical($result, null,
"should return null when start-date is missing : %s");
"should return null when end-date is missing : %s");

$result = $this->db->__validateQueryData(array(
'conditions' => array(
'start-date' => '2009-01-01',
'end-date' => '2009-02-01')));
$this->assertError('metrics is required');
$this->assertIdentical($result, null,
"should return null when metrics is missing : %s");

$result = $this->db->__validateQueryData(array(
'conditions' => array(
'start-date' => '2009-01-01',
'end-date' => '2009-02-01',
'metrics' => array('a'),
'dimensions' => array('a','b','c','d','e','f','g','h'))));
$this->assertError('too many dimensions, the maximum allowed is 7');
$this->assertIdentical($result, null,
Expand All @@ -97,7 +106,8 @@ function test___validateQueryData()
$result = $this->db->__validateQueryData(array(
'conditions' => array(
'start-date' => '2010-01-01',
'end-date' => '2009-01-01')));
'end-date' => '2009-01-01',
'metrics' => array('a'))));
$this->assertError('date order is reversed');
$this->assertIdentical($result, null,
"should return null when date order is reversed : %s");
Expand Down Expand Up @@ -125,7 +135,7 @@ function test_read()
"should call accounts() when given no parameters : %s");

$result = $mock->read($model, array(
'conditions' => array('profileId' => 123456)));
'conditions' => array('tableId' => 123456)));
$this->assertEqual($result, 'account_data',
"should call account_data() when given a profileId : %s");

Expand Down Expand Up @@ -411,20 +421,20 @@ function test_accounts()
'id' => 'http://google.com/123',
'updated' => 'updated',
'title' => 'account1',
'tableId' => 'ga:123',
'accountId' => 456,
'accountName' => 'main account',
'profileId' => 123,
'tableId' => '123',
'accountId' => '',
'accountName' => '',
'profileId' => '',
'webPropertyId' => 'UA1')),
array(
'Account' => array(
'id' => 'http://google.com/321',
'updated' => 'updated',
'title' => 'account2',
'tableId' => 'ga:321',
'accountId' => 456,
'accountName' => 'main account',
'profileId' => 321,
'tableId' => '321',
'accountId' => '',
'accountName' => '',
'profileId' => '',
'webPropertyId' => 'UA2')));

$this->assertEqual($mock->accounts(), $expected,
Expand All @@ -451,13 +461,66 @@ function test_accounts()
'id' => 'http://google.com/123',
'updated' => 'updated',
'title' => 'account1',
'tableId' => 'ga:123',
'accountId' => 456,
'accountName' => 'main account',
'profileId' => 123,
'tableId' => '123',
'accountId' => '',
'accountName' => '',
'profileId' => '',
'webPropertyId' => 'UA1')));

$this->assertEqual($mock->accounts(), $expected,
"should work with only one account : %s");
}

function test_extract_property_value() {
$entry = array(
array(
'id' => 'http://google.com/123',
'updated' => 'updated',
'title' => array('value' => 'account1'),
'tableId' => '123',
'property' => array(
array(
'name' => 'ga:accountId',
'value' => ''),
array(
'name' => 'ga:accountName',
'value' => ''),
array(
'name' => 'ga:profileId',
'value' => ''),
array(
'name' => 'ga:webPropertyId',
'value' => 'UA1'))));

$this->assertEqual(
$this->db->__extract_property_value($entry, 'webPropertyId'),
'UA1',
"should extract the value of a filled-in property : %s");
$this->assertEqual(
$this->db->__extract_property_value($entry, 'accountId'),
'',
"should assign an empty string for an empty property : %s");
$this->assertEqual(
$this->db->__extract_property_value(array(), 'accountId'),
'',
"should assign an empty string for an empty entry : %s");
$this->assertEqual(
$this->db->__extract_property_value($entry, ''),
'',
"should assign an empty string for an empty property : %s");
$this->assertEqual(
$this->db->__extract_property_value($entry, 'non existent'),
'',
"should assign an empty string for a non existent property : %s");
$this->assertEqual(
$this->db->__extract_property_value(
'wrong entry format', 'accountId'),
'',
"should assign an empty string for a wrong entry format : %s");
$this->assertEqual(
$this->db->__extract_property_value(
$entry, array('wrong property format')),
'',
"should assign an empty string for a wrong property format : %s");
}
}
2 changes: 1 addition & 1 deletion views/google_analytics/index.ctp
Expand Up @@ -4,7 +4,7 @@
<li>
<?php
echo $html->link($account['Account']['title'], array(
'action' => 'show', $account['Account']['profileId']));
'action' => 'show', $account['Account']['tableId']));
?>
</li>
<?php endforeach ?>
Expand Down
2 changes: 1 addition & 1 deletion views/google_analytics/show.ctp
Expand Up @@ -51,7 +51,7 @@
</table>
<h3>Search</h3>
<form action="<?php echo $html->url(array('action'=>'search')) ?>" method="post">
<input type="hidden" name="profileId" value="<?php echo $profileId ?>">
<input type="hidden" name="tableId" value="<?php echo $tableId ?>">
<div>
<label for="start-date">Start date (YYY-MM-DD)</label>
<input id="start-date" name="start-date" value="<?php echo $startDate ?>">
Expand Down

0 comments on commit e0cb7f3

Please sign in to comment.