Skip to content

Commit

Permalink
Create GoogleAnalytics Extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl DeBisschop committed Nov 25, 2020
1 parent 3623fc3 commit 17356a3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 45 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"PhpEtl\\GoogleAnalytics\\Tests\\": "tests/"
}
},
"scripts": {
Expand Down
61 changes: 33 additions & 28 deletions src/Extractors/GoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace PhpEtl\GoogleAnalytics\Extractors;

use Google\Exception as GoogleException;
use Google_Service_AnalyticsReporting as AnalyticsReporting;
use Wizaplace\Etl\Extractors\Extractor;
use Wizaplace\Etl\Row;

Expand Down Expand Up @@ -65,6 +64,8 @@
*/
class GoogleAnalytics extends Extractor
{
private const REPORT_PAGE_SIZE = 1000;

/**
* The input source.
*
Expand Down Expand Up @@ -152,9 +153,9 @@ class GoogleAnalytics extends Extractor
/** @var string[] */
private array $metricHeaders;

private int $reportPageSize = 1000;
private \Google_Service_AnalyticsReporting $reportingService;

private AnalyticsReporting $reportingService;
public \Google_Service_AnalyticsReporting_ReportRequest $reportRequest;

private int $clientReqCount = 0;

Expand All @@ -181,7 +182,7 @@ public function __construct(?string $config = '')
$client->setAuthConfig($config);

$this->analyticsService = new \Google_Service_Analytics($client);
$this->reportingService = new AnalyticsReporting($client);
$this->reportingService = new \Google_Service_AnalyticsReporting($client);
}
}

Expand All @@ -191,6 +192,7 @@ public function __construct(?string $config = '')
public function extract(): \Generator
{
$this->validate();
$this->reportRequestSetup($this->dimensions, $this->metrics, $this->startDate, $this->endDate);

foreach ($this->getProfiles() as $propertyName => $profileSummary) {
$this->delay();
Expand All @@ -199,7 +201,11 @@ public function extract(): \Generator
continue;
}

$reports = $this->reportingService->reports->batchGet($this->reportRequest($profileSummary->getId()));
$request = $this->reportRequest($profileSummary->getId());
$reports = $this->reportingService->reports->batchGet($request);
if (empty($reports)) {
print_r($request);
}

/** @var \Google_Service_AnalyticsReporting_Report $report */
foreach ($reports as $report) {
Expand Down Expand Up @@ -228,7 +234,7 @@ public function setAnalyticsSvc(\Google_Service_Analytics $analyticsService): se
/**
* Enables dependency injection for the analytics reporting service.
*/
public function setReportingSvc(AnalyticsReporting $reportingService): self
public function setReportingSvc(\Google_Service_AnalyticsReporting $reportingService): self
{
$this->reportingService = $reportingService;

Expand Down Expand Up @@ -308,51 +314,50 @@ private function isWantedView(string $name): bool
return !isset($this->input) || 0 === count($this->views) || in_array($name, $this->views, true);
}

/**
* Run a report for a specific configuration and view/profile.
*/
private function reportRequest(string $viewId): \Google_Service_AnalyticsReporting_GetReportsRequest
public function reportRequest(string $viewId): \Google_Service_AnalyticsReporting_GetReportsRequest
{
$request = new \Google_Service_AnalyticsReporting_ReportRequest();
$this->reportRequest->setViewId($viewId);

$body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$this->reportRequest]);

return $body;
}

$request->setViewId($viewId);
public function reportRequestSetup(array $dimensions, array $metrics, string $start, string $end): void
{
$this->reportRequest = new \Google_Service_AnalyticsReporting_ReportRequest();

$dateRange = new \Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate($this->startDate);
$dateRange->setEndDate($this->endDate);
$request->setDateRanges($dateRange);
$dateRange->setStartDate($start);
$dateRange->setEndDate($end);
$this->reportRequest->setDateRanges($dateRange);

// Max 7 dimensions.
$array = [];
foreach ($this->dimensions as $dimension) {
foreach ($dimensions as $dimension) {
$reportDimension = new \Google_Service_AnalyticsReporting_Dimension();
$reportDimension->setName($dimension);
$array[] = $reportDimension;
}
$request->setDimensions($array);
$this->reportRequest->setDimensions($array);

$request->setDimensionFilterClauses([]);
$this->reportRequest->setDimensionFilterClauses([]);

// At least one metric required, max 10.
$array = [];
foreach ($this->metrics as $metric) {
foreach ($metrics as $metric) {
$reportingMetric = new \Google_Service_AnalyticsReporting_Metric();
$reportingMetric->setExpression($metric['name']);
$reportingMetric->setAlias(str_replace('ga:', '', $metric['name']));
$reportingMetric->setFormattingType($metric['type']);
$array[] = $reportingMetric;
}
$request->setMetrics($array);

// Default 1000, max 100000.
$request->setPageSize($this->reportPageSize);
$this->reportRequest->setMetrics($array);

$request->setIncludeEmptyRows(true);
$this->reportRequest->setPageSize(self::REPORT_PAGE_SIZE);

$body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests([$request]);

return $body;
$this->reportRequest->setIncludeEmptyRows(true);
}

/**
Expand Down
41 changes: 25 additions & 16 deletions tests/Extractors/GoogleAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

declare(strict_types=1);

namespace Tests\Extractors;
namespace PhpEtl\GoogleAnalytics\Tests\Extractors;

use Tests;
use Wizaplace\Etl\Extractors\GoogleAnalytics;
use PhpEtl\GoogleAnalytics\Tests\TestCase;
use PhpEtl\GoogleAnalytics\Extractors\GoogleAnalytics;
use Wizaplace\Etl\Row;

/**
Expand All @@ -22,9 +22,10 @@
*/
class GoogleAnalyticsTest extends TestCase
{
protected $input = [];

protected $options = [
protected array $input = [];

protected array $options = [
'startDate' => '2010-11-11',
'dimensions' => ['ga:date'],
'metrics' => [['name' => 'ga:pageviews', 'type' => 'INTEGER']]
Expand Down Expand Up @@ -58,17 +59,31 @@ public function defaultOptions()
$analyticsService = $this->prophesize(\Google_Service_Analytics::class);
$analyticsService->management_accountSummaries = $mgmtAcctSummary->reveal();


$extractor = new GoogleAnalytics();
$extractor->setAnalyticsSvc($analyticsService->reveal())
->setReportingSvc($this->getReportingService()->reveal());
$extractor->input($this->input);
$extractor->options($this->options);
$extractor->reportRequestSetup(
$this->options['dimensions'],
$this->options['metrics'],
$this->options['startDate'],
date('Y-m-d', strtotime('-1 day'))
);

$resourceReports = $this->prophesize(\Google_Service_AnalyticsReporting_Resource_Reports::class);
$reportRequest = $extractor->reportRequest('default');
print_r($reportRequest);
$resourceReports->batchGet($reportRequest)->shouldBeCalled()->willReturn($this->getReportsResponse());

$reportingService = $this->prophesize(\Google_Service_AnalyticsReporting::class);
$reportingService->reports = $resourceReports->reveal();

$extractor->setAnalyticsSvc($analyticsService->reveal())
->setReportingSvc($reportingService->reveal());

static::assertEquals($expected, iterator_to_array($extractor->extract()));
}

private function getReportingService()
private function getReportsResponse(): \Google_Service_AnalyticsReporting_GetReportsResponse
{
$columnHeader = new \Google_Service_AnalyticsReporting_ColumnHeader();
$columnHeader->setDimensions(['ga:date']);
Expand All @@ -95,12 +110,6 @@ private function getReportingService()
$getRptsResponse = $this->prophesize(\Google_Service_AnalyticsReporting_GetReportsResponse::class);
$getRptsResponse->getReports()->willReturn([$report->reveal()]);

$resourceReports = $this->prophesize(\Google_Service_AnalyticsReporting_Resource_Reports::class);
$resourceReports->batchGet(static::any())->shouldBeCalled()->willReturn($getRptsResponse->reveal());

$reportingService = $this->prophesize(\Google_Service_AnalyticsReporting::class);
$reportingService->reports = $resourceReports->reveal();

return $reportingService;
return $getRptsResponse->reveal();
}
}
48 changes: 48 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* @author Wizacha DevTeam <dev@wizacha.com>
* @copyright Copyright (c) Wizacha
* @copyright Copyright (c) Leonardo Marquine
* @license MIT
*/

declare(strict_types=1);

namespace PhpEtl\GoogleAnalytics\Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
use Wizaplace\Etl\Loaders\Loader;
use Wizaplace\Etl\Transformers\Transformer;

/**
* Class TestCase
*
* @package Tests
*/
abstract class TestCase extends BaseTestCase
{

/**
* @param $step
* @param $data
*/
protected function execute($step, $data)
{
if ($step instanceof Transformer) {
$method = 'transform';
}

if ($step instanceof Loader) {
$method = 'load';
}

$step->initialize();

foreach ($data as $row) {
$step->$method($row);
}

$step->finalize();
}
}

0 comments on commit 17356a3

Please sign in to comment.