Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Competitiveness service #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion src/DescomMarketFeedsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DescomMarket\Feeds;

use DescomMarket\Feeds\Console\GetMostCompetitiveProductsCommand;
use DescomMarket\Feeds\Console\IndexUrlCommand;
use DescomMarket\Feeds\Providers\EventServiceProvider;
use Illuminate\Console\Scheduling\Schedule;
Expand Down Expand Up @@ -34,7 +35,7 @@ public function boot()

private function registerScheduler()
{
if (! config('feeds-google.index.enabled')) {
if (!config('feeds-google.index.enabled')) {
return;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Google/GoogleServiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@ private static function service(string $serviceClassName, string|array $scopes)
{
$credentials = config('feeds-google.api.credentials.path');

if (! $credentials) {
if (!$credentials) {
throw new \Exception('No can connect to Google without credentials json file');
}

if (! self::$client) {
if (!self::$client) {
self::$client = new Client();

self::$client->setAuthConfig($credentials);

self::$client->addScope($scopes);

}


Expand Down
48 changes: 48 additions & 0 deletions src/Google/Merchant/Services/Metrics/PerformanceViewService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace DescomMarket\Feeds\Google\Merchant\Services\Metrics;

use DescomMarket\Feeds\Google\GoogleServiceBuilder;
use Google\Service\ShoppingContent\ReportRow;
use Google\Service\ShoppingContent\SearchRequest;

class PerformanceViewService
{
public function get($startDate, $endDate, $pageToken = null, $perPage = 1000)
{
$service = GoogleServiceBuilder::googleMerchant();

$searchRequest = new SearchRequest([
'pageSize' => $perPage,
'pageToken' => $pageToken,
'query' => "
SELECT
segments.date,
segments.week,
segments.program,
segments.offer_id,
segments.title,
segments.brand,
segments.currency_code,
metrics.clicks,
metrics.impressions,
metrics.ctr,
metrics.conversions,
metrics.conversion_value_micros,
metrics.conversion_rate,
metrics.item_fill_rate
FROM MerchantPerformanceView
WHERE segments.date BETWEEN '$startDate' AND '$endDate'"
]);

$search = $service->reports->search(config('feeds-google.merchant.id'), $searchRequest);

$nextPageToken = $search->getNextPageToken();
$results = $search->getResults();

return [
'results' => $results,
'nextPageToken' => $nextPageToken
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace DescomMarket\Feeds\Google\Merchant\Services\Products;

use DescomMarket\Feeds\Google\GoogleServiceBuilder;
use Google\Service\ShoppingContent\ReportRow;
use Google\Service\ShoppingContent\SearchRequest;

class ProductCompetitivenessService
{
public function list($pageToken = null, $perPage = 1000): array
{
$service = GoogleServiceBuilder::googleMerchant();

$searchRequest = new SearchRequest([
'pageSize' => $perPage,
'pageToken' => $pageToken,
'query' => "
SELECT
product_view.id, product_view.title, product_view.brand,
product_view.price_micros,
product_view.currency_code,
price_competitiveness.country_code,
price_competitiveness.benchmark_price_micros,
price_competitiveness.benchmark_price_currency_code
FROM PriceCompetitivenessProductView"
]);

$search = $service->reports->search(config('feeds-google.merchant.id'), $searchRequest);

$nextPageToken = $search->getNextPageToken();
$results = $search->getResults();

$resultsAsObjects = collect($results)->map(function (ReportRow $item) {
return [
'productView' => $item->getProductView(),
'priceCompetitiveness' => $item->getPriceCompetitiveness()
];
})->toArray();

return [
'results' => $resultsAsObjects,
'nextPageToken' => $nextPageToken
];
}

public function get($productId)
{
$service = GoogleServiceBuilder::googleMerchant();

$searchRequest = new SearchRequest([
'pageSize' => 1,
'query' => "
SELECT
product_view.id,
product_view.title,
product_view.brand,
product_view.price_micros,
product_view.currency_code,
price_competitiveness.country_code,
price_competitiveness.benchmark_price_micros,
price_competitiveness.benchmark_price_currency_code
FROM PriceCompetitivenessProductView
WHERE product_view.id LIKE 'online:es:ES:$productId'"
]);

$search = $service->reports->search(config('feeds-google.merchant.id'), $searchRequest);

$results = $search->getResults();

$resultsAsObjects = collect($results)->map(function (ReportRow $item) {
return [
'productView' => $item->getProductView(),
'priceCompetitiveness' => $item->getPriceCompetitiveness()
];
})->first();

return $resultsAsObjects;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function run(array $productData)
{
$merchantId = config('feeds-google.merchant.id');

if (! $merchantId) {
if (!$merchantId) {
return;
}

Expand Down