Skip to content

Commit

Permalink
Source formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Roderik van der Veer committed Apr 28, 2014
1 parent e46cce3 commit 6fede2e
Show file tree
Hide file tree
Showing 25 changed files with 358 additions and 330 deletions.
7 changes: 4 additions & 3 deletions Command/DashboardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
namespace Kunstmaan\DashboardBundle\Command;


use Kunstmaan\DashboardBundle\Widget\DashboardWidget;
use Kunstmaan\DashboardBundle\Manager\WidgetManager;
use Kunstmaan\DashboardBundle\Widget\DashboardWidget;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DashboardCommand extends ContainerAwareCommand {
class DashboardCommand extends ContainerAwareCommand
{

protected function configure()
{
Expand All @@ -24,7 +25,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

/** @var DashboardWidget[] $widgets */
$widgets = $widgetManager->getWidgets();
foreach($widgets as $widget){
foreach ($widgets as $widget) {
/** @var DashboardWidget $widget */
$widget->getCommand()->execute($input, $output);
}
Expand Down
23 changes: 11 additions & 12 deletions Command/GoogleAnalyticsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
namespace Kunstmaan\DashboardBundle\Command;

use Doctrine\ORM\EntityManager;
use Kunstmaan\DashboardBundle\Command\Helper\Analytics\ChartDataCommandHelper;
use Kunstmaan\DashboardBundle\Command\Helper\Analytics\GoalCommandHelper;
use Kunstmaan\DashboardBundle\Command\Helper\Analytics\MetricsCommandHelper;
use Kunstmaan\DashboardBundle\Command\Helper\Analytics\UsersCommandHelper;
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
use Kunstmaan\DashboardBundle\Helper\GoogleAnalyticsHelper;
use Kunstmaan\DashboardBundle\Helper\GoogleClientHelper;
use Kunstmaan\DashboardBundle\Repository\AnalyticsConfigRepository;
use Kunstmaan\DashboardBundle\Repository\AnalyticsOverviewRepository;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Kunstmaan\DashboardBundle\Helper\GoogleAnalyticsHelper;
use Kunstmaan\DashboardBundle\Helper\GoogleClientHelper;

use Kunstmaan\DashboardBundle\Command\Helper\Analytics\MetricsCommandHelper;
use Kunstmaan\DashboardBundle\Command\Helper\Analytics\ChartDataCommandHelper;
use Kunstmaan\DashboardBundle\Command\Helper\Analytics\GoalCommandHelper;
use Kunstmaan\DashboardBundle\Command\Helper\Analytics\UsersCommandHelper;

class GoogleAnalyticsCommand extends ContainerAwareCommand {
class GoogleAnalyticsCommand extends ContainerAwareCommand
{
/** @var GoogleClientHelper $googleClientHelper */
private $googleClientHelper;
/** @var GoogleAnalyticsHelper $analyticsHelper */
Expand Down Expand Up @@ -51,12 +50,12 @@ private function init($output)

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->init($output);
$this->init($output);

// if no token set yet
if (!$this->googleClientHelper->tokenIsSet()) {
$this->output->writeln('You haven\'t configured a Google account yet');
return;
$this->output->writeln('You haven\'t configured a Google account yet');
return;
}

// create API Analytics helper to execute queries
Expand Down
6 changes: 4 additions & 2 deletions Command/Helper/Analytics/AbstractAnalyticsCommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Kunstmaan\DashboardBundle\Helper\GoogleAnalyticsHelper;
use Symfony\Component\Console\Output\OutputInterface;

abstract class AbstractAnalyticsCommandHelper {
abstract class AbstractAnalyticsCommandHelper
{

/** @var GoogleAnalyticsHelper $analyticsHelper */
protected $analyticsHelper;
Expand All @@ -21,7 +22,8 @@ abstract class AbstractAnalyticsCommandHelper {
* @param $output
* @param $em
*/
public function __construct($analyticsHelper, $output, $em) {
public function __construct($analyticsHelper, $output, $em)
{
$this->analyticsHelper = $analyticsHelper;
$this->output = $output;
$this->em = $em;
Expand Down
42 changes: 22 additions & 20 deletions Command/Helper/Analytics/ChartDataCommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,42 @@

use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;

class ChartDataCommandHelper extends AbstractAnalyticsCommandHelper {
class ChartDataCommandHelper extends AbstractAnalyticsCommandHelper
{

/**
* get data and save it for the overview
*
* @param AnalyticsOverview $overview The overview
*/
public function getData(&$overview) {
public function getData(&$overview)
{
$this->output->writeln("\t" . 'Fetching chart data..');

// create the right timespan
$timespan = $overview->getTimespan() - $overview->getStartOffset();
if ($timespan <= 1) {
$extra = array(
'dimensions' => 'ga:date,ga:hour'
);
);
} else if ($timespan <= 7) {
$extra = array(
'dimensions' => 'ga:date,ga:hour'
);
);
} else if ($timespan <= 31) {
$extra = array(
'dimensions' => 'ga:week,ga:day,ga:date'
);
);
} else {
$extra = array(
'dimensions' => 'ga:isoYearIsoWeek'
);
);
}

// get visits & visitors
if ($overview->getUseYear()) {
$begin = date('Y-m-d', mktime(0,0,0,1,1,date('Y')));
$end = date('Y-m-d', mktime(0,0,0,1,1,date('Y', strtotime('+1 year'))));
$begin = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y')));
$end = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y', strtotime('+1 year'))));
$results = $this->analyticsHelper->getResultsByDate(
$begin,
$end,
Expand All @@ -53,7 +55,7 @@ public function getData(&$overview) {
);
}

$rows = $results->getRows();
$rows = $results->getRows();

$chartData = array();
$totalUsers = 0;
Expand All @@ -63,10 +65,10 @@ public function getData(&$overview) {

foreach ($rows as $row) {
// metrics
$sessions = $row[sizeof($row)-4];
$users = $row[sizeof($row)-3];
$newusers = $row[sizeof($row)-2];
$pageviews = $row[sizeof($row)-1];
$sessions = $row[sizeof($row) - 4];
$users = $row[sizeof($row) - 3];
$newusers = $row[sizeof($row) - 2];
$pageviews = $row[sizeof($row) - 1];
$totalSessions += $sessions;
$totalUsers += $users;
$totalPageviews += $pageviews;
Expand All @@ -87,19 +89,19 @@ public function getData(&$overview) {
$timestamp = mktime(0, 0, 0, substr($row[2], 4, 2), substr($row[2], 6, 2), substr($row[2], 0, 4));
$timestamp = date('Y-m-d H:00', $timestamp);
} else {
$timestamp = strtotime(substr($row[0], 0, 4).'W'.substr($row[0], 4, 2));
$timestamp = strtotime(substr($row[0], 0, 4) . 'W' . substr($row[0], 4, 2));
$timestamp = date('Y-m-d H:00', $timestamp);
}

// add to chart array
$chartEntry = array(
'timestamp' => $timestamp,
'sessions' => $sessions,
'users' => $users,
'newusers' => $newusers,
'pageviews' => $pageviews
'timestamp' => $timestamp,
'sessions' => $sessions,
'users' => $users,
'newusers' => $newusers,
'pageviews' => $pageviews

);
);
$chartData[] = $chartEntry;
}

Expand Down
65 changes: 33 additions & 32 deletions Command/Helper/Analytics/GoalCommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class GoalCommandHelper extends AbstractAnalyticsCommandHelper
/**
* Constructor
*
* @param GoogleClientHelper $googleClientHelper
* @param GoogleAnalyticsHelper $analyticsHelper
* @param OutputInterface $output
* @param EntityManager $em
* @param GoogleClientHelper $googleClientHelper
* @param GoogleAnalyticsHelper $analyticsHelper
* @param OutputInterface $output
* @param EntityManager $em
*/
public function __construct($googleClientHelper, $analyticsHelper, $output, $em)
{
Expand All @@ -39,39 +39,39 @@ public function getData(&$overview)
// calculate timespan
$timespan = $overview->getTimespan() - $overview->getStartOffset();
if ($timespan <= 1) {
$extra = array( 'dimensions' => 'ga:date,ga:hour' );
$extra = array('dimensions' => 'ga:date,ga:hour');
$start = 2;
} else if ($timespan <= 7) {
$extra = array( 'dimensions' => 'ga:date,ga:hour' );
$extra = array('dimensions' => 'ga:date,ga:hour');
$start = 2;
} else if ($timespan <= 31) {
$extra = array( 'dimensions' => 'ga:week,ga:day,ga:date' );
$extra = array('dimensions' => 'ga:week,ga:day,ga:date');
$start = 3;
} else {
$extra = array( 'dimensions' => 'ga:isoYearIsoWeek' );
$extra = array('dimensions' => 'ga:isoYearIsoWeek');
$start = 1;
}

// Get the goals from the saved profile. These are a maximum of 20 goals.
$goals = $this->analyticsHelper->getAnalytics()
->management_goals
->listManagementGoals($this->googleClientHelper->getAccountId(), $this->googleClientHelper->getPropertyId(), $this->googleClientHelper->getProfileId())
->items;
->management_goals
->listManagementGoals($this->googleClientHelper->getAccountId(), $this->googleClientHelper->getPropertyId(), $this->googleClientHelper->getProfileId())
->items;

$goaldata =array();
$goaldata = array();

if (is_array($goals)) {
$metrics = array();

// Create an array with for each goal an entry to create the metric parameter.
foreach ($goals as $key=>$value) {
foreach ($goals as $key => $value) {
$key++;
$metrics[] = 'ga:goal'.$key.'Completions';
$goaldata[] = array('position'=>$key, 'name'=>$value->name);
$metrics[] = 'ga:goal' . $key . 'Completions';
$goaldata[] = array('position' => $key, 'name' => $value->name);
}

// Create the metric parameter string, there is a limit of 10 metrics per query, and a max of 20 goals available.
if (count($metrics)<=10) {
if (count($metrics) <= 10) {
$part1 = implode(',', $metrics);
$part2 = false;
} else {
Expand All @@ -81,8 +81,8 @@ public function getData(&$overview)

// Execute query
if ($overview->getUseYear()) {
$begin = date('Y-m-d', mktime(0,0,0,1,1,date('Y')));
$end = date('Y-m-d', mktime(0,0,0,1,1,date('Y', strtotime('+1 year'))));
$begin = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y')));
$end = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y', strtotime('+1 year'))));

$results = $this->analyticsHelper->getResultsByDate(
$begin,
Expand All @@ -103,8 +103,8 @@ public function getData(&$overview)
// Execute an extra query if there are more than 10 goals to query
if ($part2) {
if ($overview->getUseYear()) {
$begin = date('Y-m-d', mktime(0,0,0,1,1,date('Y')));
$end = date('Y-m-d', mktime(0,0,0,1,1,date('Y', strtotime('+1 year'))));
$begin = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y')));
$end = date('Y-m-d', mktime(0, 0, 0, 1, 1, date('Y', strtotime('+1 year'))));
$results = $this->analyticsHelper->getResultsByDate(
$begin,
$end,
Expand All @@ -123,15 +123,15 @@ public function getData(&$overview)
$rows2 = $results->getRows();
for ($i = 0; $i < sizeof($rows2); $i++) {
// Merge the results of the extra query data with the previous query data.
$rows[$i] = array_merge($rows[$i], array_slice($rows2[$i], $start, sizeof($rows2)-$start));
$rows[$i] = array_merge($rows[$i], array_slice($rows2[$i], $start, sizeof($rows2) - $start));
}
}

// Create a result array to be parsed and create Goal objects from
$goalCollection = array();
for ($i = 0; $i < sizeof($goaldata); $i++) {
$goalEntry = array();
foreach($rows as $row) {
foreach ($rows as $row) {
// Create a timestamp for each goal visit (this depends on the timespan of the overview: split per hour, day, week, month)
if ($timespan <= 1) {
$timestamp = mktime($row[1], 0, 0, substr($row[0], 4, 2), substr($row[0], 6, 2), substr($row[0], 0, 4));
Expand All @@ -143,14 +143,14 @@ public function getData(&$overview)
$timestamp = mktime(0, 0, 0, substr($row[0], 4, 2), substr($row[2], 6, 2), substr($row[2], 0, 4));
$timestamp = date('Y-m-d H:00', $timestamp);
} else {
$timestamp = strtotime(substr($row[0], 0, 4).'W'.substr($row[0], 4, 2));
$timestamp = strtotime(substr($row[0], 0, 4) . 'W' . substr($row[0], 4, 2));
$timestamp = date('Y-m-d H:00', $timestamp);
}
$goalEntry[$timestamp] = $row[$i+$start];
$goalEntry[$timestamp] = $row[$i + $start];
}
$goalCollection['goal'.$goaldata[$i]['position']]['name'] = $goaldata[$i]['name'];
$goalCollection['goal'.$goaldata[$i]['position']]['position'] = $goaldata[$i]['position'];
$goalCollection['goal'.$goaldata[$i]['position']]['visits'] = $goalEntry;
$goalCollection['goal' . $goaldata[$i]['position']]['name'] = $goaldata[$i]['name'];
$goalCollection['goal' . $goaldata[$i]['position']]['position'] = $goaldata[$i]['position'];
$goalCollection['goal' . $goaldata[$i]['position']]['visits'] = $goalEntry;
}

// Parse the goals and append them to the overview.
Expand All @@ -165,34 +165,35 @@ public function getData(&$overview)
* @param AnalyticsOverview $overview The overview
* @param $goalCollection
*/
private function parseGoals(&$overview, $goalCollection) {
private function parseGoals(&$overview, $goalCollection)
{

// delete existing entries
foreach ($overview->getGoals() as $goal) {
$this->em->remove($goal);
}
$this->em->flush();

foreach($goalCollection as $goalEntry) {
foreach ($goalCollection as $goalEntry) {
// create a new goal
$goal = new AnalyticsGoal();
$goal->setOverview($overview);
$goal->setName($goalEntry['name']);
$goal->setPosition($goalEntry['position']);
$this->output->writeln("\t\t" . 'Fetching goal '.$goal->getPosition().': "'.$goal->getName().'"');
$this->output->writeln("\t\t" . 'Fetching goal ' . $goal->getPosition() . ': "' . $goal->getName() . '"');

$count = 0;
$chartData = array();
$totalVisits = 0;
$steps = ceil(sizeof($goalEntry['visits'])/10);
$steps = ceil(sizeof($goalEntry['visits']) / 10);
$conversions = 0;
// Fill the chartdata array
foreach ($goalEntry['visits'] as $timestamp => $visits) {
$count++;
$totalVisits += $visits;
$conversions += $visits;

if ($count%$steps == 0) {
if ($count % $steps == 0) {
$chartData[] = array('timestamp' => $timestamp, 'conversions' => $conversions);
$count = 0;
$conversions = 0;
Expand Down
Loading

0 comments on commit 6fede2e

Please sign in to comment.