-
-
Save anonymous/bbdd47ec13725fa4746da877d9369e96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A simple controller file that checks a given user's account, property and profile hierarchies. | |
*/ | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Auth; | |
use Google_Client; | |
use Google_Service_Analytics; | |
use Google_Service_AnalyticsReporting_DateRange; | |
use Google_Service_AnalyticsReporting_Metric; | |
use Google_Service_AnalyticsReporting_GetReportsRequest; | |
use Google_Service_AnalyticsReporting; | |
use Session; | |
use Google_Service_Analytics_Webproperties; | |
use App\Http\Requests; | |
use App\User; | |
use App\Account; | |
use App\Profile; | |
use App\Property; | |
class InitialDataController extends Controller | |
{ | |
/** | |
* @var $client to be authorized by Google. | |
*/ | |
private $client; | |
/** | |
* @var $analytics Analytics object to be used. | |
*/ | |
private $analytics; | |
private $user; | |
public function __construct() | |
{ | |
$this->client = $this->AuthenticateCurrentClient(); | |
$this->analytics = new Google_Service_Analytics($this->client); | |
} | |
private function AuthenticateCurrentClient(){ | |
$this->user = Auth::user(); | |
$token = Session::get('token'); | |
if(empty($token)){ | |
$token = $this->client->fetchAccessTokenWithRefreshToken($this->user->refreshToken); | |
Session::put('token', $token); | |
} | |
// Authenticate the client. | |
$client = new Google_Client(); | |
$client->setAuthConfig('/home/vagrant/Analytics/client_secret.json'); | |
$client->setAccessToken($token); | |
$client->authenticate($this->user->code); | |
return $client; | |
} | |
public function index(){ | |
$accountsObjects = $this->GetAllAccounts('initial'); | |
$accounts = array(); | |
foreach($accountsObjects as $object){ | |
$accounts[$object['accountID']] = $object['name']; | |
} | |
uasort($accounts, function($a, $b){ | |
return strnatcmp($a,$b); | |
}); | |
return view('initialClientList', compact('accounts')); | |
} | |
public function FetchData(Request $request){ | |
$accounts = $request->newClients; | |
$requestedAccounts = $this->GetAccounts($accounts); | |
$properties = array(); | |
$profiles = array(); | |
foreach($accounts as $key => $account){ | |
$accountID = $key; | |
$accountProperties = $this->GetProperties($accountID); | |
$properties = array_merge($properties, $accountProperties); | |
foreach($accountProperties as $property){ | |
$propertyID = $property['id']; | |
$profiles = array_merge($profiles, (array)$this->GetProfiles($accountID, $propertyID)); | |
} | |
} | |
Account::insert($requestedAccounts); | |
Property::insert($properties); | |
Profile::insert($profiles); | |
return redirect(route('home')); | |
} | |
protected function GetAllAccounts($isInitial = NULL){ | |
try { | |
$accountsObject = $this->analytics->management_accounts->listManagementAccounts(); | |
$accounts = $accountsObject->getItems(); | |
if($isInitial == 'initial'){ | |
$simpleListAccounts = array(); | |
foreach($accounts as $account){ | |
$simpleListAccounts[] = array( | |
'accountID' => $account->id, | |
'name' => $account->name | |
); | |
} | |
return $simpleListAccounts; | |
} | |
else{ | |
return $accounts; | |
} | |
} catch (apiServiceException $e) { | |
print 'There was an Analytics API service error ' | |
. $e->getCode() . ':' . $e->getMessage(); | |
} catch (apiException $e) { | |
print 'There was a general API error ' | |
. $e->getCode() . ':' . $e->getMessage(); | |
} | |
} | |
public function GetAccounts($requestedAccounts){ | |
$accounts = $this->GetAllAccounts(); | |
$matchingAccounts = array(); | |
foreach($accounts as &$account){ | |
if(array_key_exists($account->id, $requestedAccounts)){ | |
$matchingAccounts[] = array( | |
'id' => $account->id, | |
'name' => $account->name, | |
'ownerID' => $this->user->googleID, | |
'updated' => $account->updated, | |
); | |
} | |
} | |
return $matchingAccounts; | |
} | |
protected function GetProperties($accountID){ | |
try { | |
$propertiesObject = $this->analytics->management_webproperties | |
->listManagementWebproperties($accountID); | |
$properties = $propertiesObject->getItems(); | |
$structuredProperties = array(); | |
foreach($properties as &$property){ | |
$structuredProperties[] = array( | |
'accountID' => $property->accountId, | |
'id' => $property->id, | |
'name' => $property->name, | |
'updated' => $property->updated, | |
'websiteURL' => $property->websiteUrl, | |
); | |
} | |
return $structuredProperties; | |
} catch (apiServiceException $e) { | |
print 'There was an Analytics API service error ' | |
. $e->getCode() . ':' . $e->getMessage(); | |
} catch (apiException $e) { | |
print 'There was a general API error ' | |
. $e->getCode() . ':' . $e->getMessage(); | |
} | |
} | |
protected function GetProfiles($accountID, $webPropertyID){ | |
try { | |
$profilesObject = $this->analytics->management_profiles | |
->listManagementProfiles($accountID, $webPropertyID); | |
$profiles = $profilesObject->getItems(); | |
$structuredProfiles = array(); | |
foreach($profiles as &$profile){ | |
$structuredProfiles[] = array( | |
'propertyID' => $profile->webPropertyId, | |
'id' => $profile->id, | |
'name' => $profile->name, | |
'updated' => $profile->updated, | |
'currency' => $profile->currency | |
); | |
} | |
return $structuredProfiles; | |
} catch (apiServiceException $e) { | |
print 'There was an Analytics API service error ' | |
. $e->getCode() . ':' . $e->getMessage(); | |
} catch (apiException $e) { | |
print 'There was a general API error ' | |
. $e->getCode() . ':' . $e->getMessage(); | |
} | |
} | |
public function ListAccounts() | |
{ | |
$accounts = $this->GetAccounts(); | |
return view('home', compact('accounts')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment