diff --git a/composer.json b/composer.json index e6e329e..af45f3e 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ }, "autoload-dev": { "psr-4": { - "Tests\\": "tests/" + "PhpEtl\\GoogleAnalytics\\Tests\\": "tests/" } }, "scripts": { diff --git a/src/Extractors/GoogleAnalytics.php b/src/Extractors/GoogleAnalytics.php index 3072bc6..6a72573 100644 --- a/src/Extractors/GoogleAnalytics.php +++ b/src/Extractors/GoogleAnalytics.php @@ -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; @@ -65,6 +64,8 @@ */ class GoogleAnalytics extends Extractor { + private const REPORT_PAGE_SIZE = 1000; + /** * The input source. * @@ -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; @@ -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); } } @@ -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(); @@ -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) { @@ -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; @@ -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); } /** diff --git a/tests/Extractors/GoogleAnalyticsTest.php b/tests/Extractors/GoogleAnalyticsTest.php index 7b3955f..553cb41 100644 --- a/tests/Extractors/GoogleAnalyticsTest.php +++ b/tests/Extractors/GoogleAnalyticsTest.php @@ -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; /** @@ -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']] @@ -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']); @@ -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(); } } diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..adb974e --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,48 @@ + + * @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(); + } +}