Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 91 additions & 72 deletions src/Thujohn/Analytics/Analytics.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
<?php namespace Thujohn\Analytics;
<?php

class Analytics {
protected $client;
protected $service;
private $site_ids = array();
namespace Thujohn\Analytics;

public function __construct(\Google_Client $client) {
$this->setClient($client);
$this->setService($client);
}
class Analytics
{
protected $client;
protected $service;
private $site_ids = [];

public function getClient() {
return $this->client;
}
public function __construct(\Google_Client $client)
{
$this->setClient($client);
$this->setService($client);
}

public function setClient(\Google_Client $client) {
$this->client = $client;
public function getClient()
{
return $this->client;
}

return $this;
}
public function setClient(\Google_Client $client)
{
$this->client = $client;

public function getService() {
return $this->service;
}
return $this;
}

public function setService(\Google_Client $client) {
$this->service = new \Google_Service_Analytics($client);
public function getService()
{
return $this->service;
}

return $this;
}
public function setService(\Google_Client $client)
{
$this->service = new \Google_Service_Analytics($client);

public function query($id, $start_date, $end_date, $metrics, $others = array()) {
return $this->service->data_ga->get($id, $start_date, $end_date, $metrics, $others);
}
return $this;
}

public function query($id, $start_date, $end_date, $metrics, $others = [])
{
return $this->service->data_ga->get($id, $start_date, $end_date, $metrics, $others);
}

/**
* Runs analytics query calls in batch mode
* It accepts an array of queries as specified by the parameters of the Analytics::query function
* With an additional optional parameter named key, which is used to identify the results for a specific object
* With an additional optional parameter named key, which is used to identify the results for a specific object.
*
* Returns an array with object keys as response-KEY where KEY is the key you specified or a random key returned
* from analytics.
*
* @param array $queries
*
* @return array|null
*/
public function batchQueries(array $queries) {
public function batchQueries(array $queries)
{

/*
* Set the client to use batch mode
Expand All @@ -60,7 +72,7 @@ public function batchQueries(array $queries) {
$key = array_pull($query, 'key');

// call the original query method to get the request object
$req = call_user_func_array(__NAMESPACE__ .'\Analytics::query' ,$query);
$req = call_user_func_array(__NAMESPACE__.'\Analytics::query', $query);

$batch->add($req, $key);
}
Expand All @@ -73,46 +85,53 @@ public function batchQueries(array $queries) {
return $results;
}

public function segments() {
return $this->service->management_segments;
}

public function accounts() {
return $this->service->management_accounts;
}

public function goals() {
return $this->service->management_goals;
}

public function profiles() {
return $this->service->management_profiles;
}

public function webproperties() {
return $this->service->management_webproperties;
}

public function getAllSitesIds() {
if (empty($this->site_ids)) {
$sites = $this->service->management_profiles->listManagementProfiles("~all", "~all");
foreach($sites['items'] as $site) {
$this->site_ids[$site['websiteUrl']] = 'ga:' . $site['id'];
}
}

return $this->site_ids;
}

public function getSiteIdByUrl($url) {
if (!isset($this->site_ids[$url])) {
$this->getAllSitesIds();
}

if (isset($this->site_ids[$url])) {
return $this->site_ids[$url];
}

throw new \Exception("Site $url is not present in your Analytics account.");
}
}
public function segments()
{
return $this->service->management_segments;
}

public function accounts()
{
return $this->service->management_accounts;
}

public function goals()
{
return $this->service->management_goals;
}

public function profiles()
{
return $this->service->management_profiles;
}

public function webproperties()
{
return $this->service->management_webproperties;
}

public function getAllSitesIds()
{
if (empty($this->site_ids)) {
$sites = $this->service->management_profiles->listManagementProfiles('~all', '~all');
foreach ($sites['items'] as $site) {
$this->site_ids[$site['websiteUrl']] = 'ga:'.$site['id'];
}
}

return $this->site_ids;
}

public function getSiteIdByUrl($url)
{
if (!isset($this->site_ids[$url])) {
$this->getAllSitesIds();
}

if (isset($this->site_ids[$url])) {
return $this->site_ids[$url];
}

throw new \Exception("Site $url is not present in your Analytics account.");
}
}
26 changes: 15 additions & 11 deletions src/Thujohn/Analytics/AnalyticsFacade.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php namespace Thujohn\Analytics;
<?php

use Illuminate\Support\Facades\Facade;

class AnalyticsFacade extends Facade {
namespace Thujohn\Analytics;

/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'analytics'; }
use Illuminate\Support\Facades\Facade;

}
class AnalyticsFacade extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'analytics';
}
}
116 changes: 58 additions & 58 deletions src/Thujohn/Analytics/AnalyticsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
<?php namespace Thujohn\Analytics;
<?php

use Illuminate\Support\ServiceProvider;

class AnalyticsServiceProvider extends ServiceProvider {
namespace Thujohn\Analytics;

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
use Illuminate\Support\ServiceProvider;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('thujohn/analytics');
}
class AnalyticsServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('Thujohn\Analytics\Analytics', function ($app) {
if(!\File::exists($app['config']->get('analytics::certificate_path')))
{
throw new \Exception("Can't find the .p12 certificate in: " . $app['config']->get('analytics::certificate_path'));
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('thujohn/analytics');
}

$config = array(
'oauth2_client_id' => $app['config']->get('analytics::client_id'),
'use_objects' => $app['config']->get('analytics::use_objects'),
);
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('Thujohn\Analytics\Analytics', function ($app) {
if (!\File::exists($app['config']->get('analytics::certificate_path'))) {
throw new \Exception("Can't find the .p12 certificate in: ".$app['config']->get('analytics::certificate_path'));
}

$client = new \Google_Client($config);
$config = [
'oauth2_client_id' => $app['config']->get('analytics::client_id'),
'use_objects' => $app['config']->get('analytics::use_objects'),
];

$client->setAccessType('offline');
$client = new \Google_Client($config);

$client->setAssertionCredentials(
new \Google_Auth_AssertionCredentials(
$app['config']->get('analytics::service_email'),
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents($app['config']->get('analytics::certificate_path'))
)
);
$client->setAccessType('offline');

return new Analytics($client);
});
$client->setAssertionCredentials(
new \Google_Auth_AssertionCredentials(
$app['config']->get('analytics::service_email'),
['https://www.googleapis.com/auth/analytics.readonly'],
file_get_contents($app['config']->get('analytics::certificate_path'))
)
);

$this->app->singleton('analytics', 'Thujohn\Analytics\Analytics');
}
return new Analytics($client);
});

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('analytics');
}
$this->app->singleton('analytics', 'Thujohn\Analytics\Analytics');
}

}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['analytics'];
}
}
Loading