Skip to content

Commit

Permalink
Merge pull request #5 from Kunstmaan/ga-dashboard-bugfix-ratelimit
Browse files Browse the repository at this point in the history
fixed ratelimit bug
  • Loading branch information
Roderik van der Veer committed Apr 25, 2014
2 parents 971a3c0 + 15e8c0a commit b2d9cc0
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function indexAction(Request $request)
return $this->render('KunstmaanDashboardBundle:GoogleAnalytics:connect.html.twig', $params);
}

// if propertyId not set
if (!$googleClientHelper->accountIsSet()) {
return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_AccountSelection'));
}

// if propertyId not set
if (!$googleClientHelper->propertyIsSet()) {
return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection'));
Expand Down
47 changes: 42 additions & 5 deletions Controller/GoogleAnalyticsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,51 @@ public function setTokenAction(Request $request)
$googleClientHelper->getClient()->authenticate($code);
$googleClientHelper->saveToken($googleClientHelper->getClient()->getAccessToken());

return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection'));
return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_AccountSelection'));
}

return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_widget_googleanalytics'));
}



/**
* @Route("/selectAccount", name="KunstmaanDashboardBundle_AccountSelection")
*
* @param Request $request
*
* @return array
*/
public function accountSelectionAction(Request $request)
{
// get API client
try {
$googleClientHelper = $this->container->get('kunstmaan_dashboard.googleclienthelper');
} catch (\Exception $e) {
// catch exception thrown by the googleClientHelper if one or more parameters in parameters.yml is not set
$currentRoute = $request->attributes->get('_route');
$currentUrl = $this->get('router')->generate($currentRoute, array(), true);
$params['url'] = $currentUrl . 'analytics/setToken/';
return $this->render('KunstmaanDashboardBundle:GoogleAnalytics:connect.html.twig', $params);
}

if (null !== $request->request->get('accounts')) {
$googleClientHelper->saveAccountId($request->request->get('accounts'));
return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection'));
}

/** @var GoogleClientHelper $googleClient */
$googleClient = $googleClientHelper->getClient();
$analyticsHelper = $this->container->get('kunstmaan_dashboard.googleanalyticshelper');
$analyticsHelper->init($googleClientHelper);
$accounts = $analyticsHelper->getAccounts();

return $this->render(
'KunstmaanDashboardBundle:GoogleAnalytics:accountSelection.html.twig',
array('accounts' => $accounts)
);
}

/**
* @Route("/selectWebsite", name="KunstmaanDashboardBundle_PropertySelection")
*
Expand All @@ -99,9 +138,7 @@ public function propertySelectionAction(Request $request)
}

if (null !== $request->request->get('properties')) {
$parts = explode("::", $request->request->get('properties'));
$googleClientHelper->saveAccountId($parts[1]);
$googleClientHelper->savePropertyId($parts[0]);
$googleClientHelper->savePropertyId($request->request->get('properties'));
return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_ProfileSelection'));
}

Expand Down Expand Up @@ -227,7 +264,7 @@ public function resetPropertyAction()
{
$em = $this->getDoctrine()->getManager();
$em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->resetPropertyId();
return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection'));
return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_AccountSelection'));
}

/**
Expand Down
41 changes: 29 additions & 12 deletions Helper/GoogleAnalyticsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,48 @@ public function getAnalytics() {
}

/**
* Get a list of all available properties for a Google Account
* Get a list of all available accounts for a Google Analytics Account
*
* @return array $data A list of all properties
*/
public function getProperties()
{
$data = array();
public function getAccounts() {
$accounts = $this->analytics->management_accounts->listManagementAccounts()->getItems();
$data = array();

foreach ($accounts as $account) {
$webproperties = $this->analytics->management_webproperties->listManagementWebproperties($account->getId());
foreach ($webproperties->getItems() as $property) {
$data[] = array(
'propertyId' => $property->getId(),
'propertyName' => $property->getName() . ' (' . $property->getWebsiteUrl() . ')',
'accountId' => $account->getId()
);
}
$data[] = array(
'accountId' => $account->getId(),
'accountName' => $account->getName()
);
}
return $data;
}

/**
* Get a list of all available properties for an account
*
* @return array $data A list of all properties
*/
public function getProperties()
{
if (!$this->clientHelper->getAccountId()) {return false;}

$webproperties = $this->analytics->management_webproperties->listManagementWebproperties($this->clientHelper->getAccountId());
$data = array();

foreach ($webproperties->getItems() as $property) {
$data[] = array(
'propertyId' => $property->getId(),
'propertyName' => $property->getName() . ' (' . $property->getWebsiteUrl() . ')',
);
}
return $data;
}

public function getProfiles()
{
if (!$this->clientHelper->getAccountId() || !$this->clientHelper->getPropertyId()) {return false;}

// get views
$profiles = $this->analytics->management_profiles->listManagementProfiles(
$this->clientHelper->getAccountId(),
Expand Down
11 changes: 11 additions & 0 deletions Helper/GoogleClientHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ public function tokenIsSet()
return $this->getToken() && '' !== $this->getToken();
}


/**
* Check if token is set
*
* @return boolean $result
*/
public function accountIsSet()
{
return $this->getAccountId() && '' !== $this->getAccountId();
}

/**
* Check if propertyId is set
*
Expand Down
2 changes: 2 additions & 0 deletions Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ dashboard:
property:
title: Select a website to track
content: Select a domain registered in your Google Analytics account.
account:
title: Select an account
reset:
profile: Choose a new Analytics Profile (view)
property: Track a new Analytics Website (property)
19 changes: 19 additions & 0 deletions Resources/views/GoogleAnalytics/accountSelection.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'KunstmaanAdminBundle:Default:layout.html.twig' %}

{% block header %}{% endblock %}

{% block content %}
<form method="post" action="">
<h2>{{ 'dashboard.ga.setup.title' | trans }}</h2>
<h3>{{ 'dashboard.ga.setup.account.title' | trans }}</h3>

<select name="accounts" class="chzn-select">
{% for account in accounts %}
<option value="{{ account.accountId }}">{{ account.accountName }}</option>
{% endfor %}
</select>
<br/>
<button class="btn btn-primary" type="submit">{{ 'form.save' | trans }}</button>
</form>

{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<select name="properties" class="chzn-select">
{% for property in properties %}
<option value="{{ property.propertyId }}::{{ property.accountId }}">{{ property.propertyName }}</option>
<option value="{{ property.propertyId }}">{{ property.propertyName }}</option>
{% endfor %}
</select>
<br/>
Expand Down

0 comments on commit b2d9cc0

Please sign in to comment.