Skip to content

Commit d061292

Browse files
committed
Implemented tracking search requests.
1 parent ded0746 commit d061292

File tree

5 files changed

+162
-40
lines changed

5 files changed

+162
-40
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"ext-PDO": "*",
1919
"bluepsyduck/mapper-manager": "^1.0",
2020
"bluepsyduck/laminas-autowire-factory": "^1.0",
21-
"bluepsyduck/ga4-measurement-protocol": "dev-feature/attributes",
21+
"bluepsyduck/ga4-measurement-protocol": "^2.0",
2222
"doctrine/cache": "^1.10",
2323
"factorio-item-browser/api-client": "^4.0",
2424
"factorio-item-browser/api-database": "^3.7",

composer.lock

Lines changed: 36 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Handler/Search/SearchQueryHandler.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
use FactorioItemBrowser\Api\Client\Request\Search\SearchQueryRequest;
88
use FactorioItemBrowser\Api\Client\Response\Search\SearchQueryResponse;
9+
use FactorioItemBrowser\Api\Database\Entity\Combination;
910
use FactorioItemBrowser\Api\Search\SearchManagerInterface;
1011
use FactorioItemBrowser\Api\Server\Response\ClientResponse;
1112
use FactorioItemBrowser\Api\Server\Service\SearchDecoratorService;
13+
use FactorioItemBrowser\Api\Server\Service\TrackingService;
14+
use FactorioItemBrowser\Api\Server\Tracking\Event\SearchEvent;
1215
use Psr\Http\Message\ResponseInterface;
1316
use Psr\Http\Message\ServerRequestInterface;
1417
use Psr\Http\Server\RequestHandlerInterface;
@@ -24,19 +27,26 @@ class SearchQueryHandler implements RequestHandlerInterface
2427
{
2528
private SearchDecoratorService $searchDecoratorService;
2629
private SearchManagerInterface $searchManager;
30+
private TrackingService $trackingService;
2731

2832
public function __construct(
2933
SearchDecoratorService $searchDecoratorService,
30-
SearchManagerInterface $searchManager
34+
SearchManagerInterface $searchManager,
35+
TrackingService $trackingService,
3136
) {
3237
$this->searchDecoratorService = $searchDecoratorService;
3338
$this->searchManager = $searchManager;
39+
$this->trackingService = $trackingService;
3440
}
3541

3642
public function handle(ServerRequestInterface $request): ResponseInterface
3743
{
3844
/** @var SearchQueryRequest $clientRequest */
3945
$clientRequest = $request->getParsedBody();
46+
/** @var Combination $combination */
47+
$combination = $request->getAttribute(Combination::class);
48+
49+
$startTime = microtime(true);
4050

4151
$searchQuery = $this->searchManager->parseQuery(
4252
Uuid::fromString($clientRequest->combinationId),
@@ -56,6 +66,17 @@ public function handle(ServerRequestInterface $request): ResponseInterface
5666
$response = new SearchQueryResponse();
5767
$response->results = $decoratedSearchResults; // @phpstan-ignore-line
5868
$response->totalNumberOfResults = $searchResults->count();
69+
70+
$trackingEvent = new SearchEvent();
71+
$trackingEvent->combinationId = $clientRequest->combinationId;
72+
$trackingEvent->modCount = $combination->getMods()->count();
73+
$trackingEvent->locale = $clientRequest->locale;
74+
$trackingEvent->queryString = $clientRequest->query;
75+
$trackingEvent->resultCount = $response->totalNumberOfResults;
76+
$trackingEvent->runtime = microtime(true) - $startTime;
77+
$trackingEvent->cached = $searchResults->getIsCached();
78+
$this->trackingService->addEvent($trackingEvent);
79+
5980
return new ClientResponse($response);
6081
}
6182
}

src/Tracking/Event/SearchEvent.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace FactorioItemBrowser\Api\Server\Tracking\Event;
6+
7+
use BluePsyduck\Ga4MeasurementProtocol\Attribute\Event;
8+
use BluePsyduck\Ga4MeasurementProtocol\Attribute\Parameter;
9+
use BluePsyduck\Ga4MeasurementProtocol\Request\Event\EventInterface;
10+
11+
/**
12+
* The event representing a search.
13+
*
14+
* @author BluePsyduck <bluepsyduck@gmx.com>
15+
* @license http://opensource.org/licenses/GPL-3.0 GPL v3
16+
*/
17+
#[Event('search')]
18+
class SearchEvent implements EventInterface
19+
{
20+
/**
21+
* The locale used for the search.
22+
* @var string|null
23+
*/
24+
#[Parameter('locale')]
25+
public ?string $locale = null;
26+
27+
/**
28+
* The id of the combination used for the search.
29+
* @var string|null
30+
*/
31+
#[Parameter('combination_id')]
32+
public ?string $combinationId = null;
33+
34+
/**
35+
* The number of mods contained in the combination.
36+
* @var int|null
37+
*/
38+
#[Parameter('mod_count')]
39+
public ?int $modCount = null;
40+
41+
/**
42+
* The query string used for the search.
43+
* @var string|null
44+
*/
45+
#[Parameter('query_string')]
46+
public ?string $queryString = null;
47+
48+
/**
49+
* Whether the search results were already cached.
50+
* @var bool|null
51+
*/
52+
#[Parameter('cached')]
53+
public ?bool $cached = null;
54+
55+
/**
56+
* The number of results returned by the search.
57+
* @var int|null
58+
*/
59+
#[Parameter('result_count')]
60+
public ?int $resultCount = null;
61+
62+
/**
63+
* @var float|null The runtime of the search, in milliseconds.
64+
*/
65+
#[Parameter('runtime')]
66+
public ?float $runtime = null;
67+
}

0 commit comments

Comments
 (0)