1
0
mirror of synced 2024-11-24 22:36:06 +03:00

Добавлен метод загрузки визитов веб-аналитики

This commit is contained in:
Artem Osipov 2023-12-06 12:40:07 +03:00
parent 454a5fac26
commit 2d0ed4c149
6 changed files with 377 additions and 0 deletions

View File

@ -0,0 +1,51 @@
<?php
/**
* @category Page
* @package RetailCrm\Api\Model\Entity\WebAnalytics
*/
namespace RetailCrm\Api\Model\Entity\WebAnalytics;
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
/**
* Class Page
*
* @category Page
* @package RetailCrm\Api\Model\Entity\WebAnalytics
*/
class Page
{
/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("url")
*/
public $url;
/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("title")
*/
public $title;
/**
* @var int
*
* @JMS\Type("integer")
* @JMS\SerializedName("countViews")
*/
public $countViews;
/**
* @var int
*
* @JMS\Type("integer")
* @JMS\SerializedName("timeOnPage")
*/
public $timeOnPage;
}

View File

@ -0,0 +1,107 @@
<?php
/**
* @category Visit
* @package RetailCrm\Api\Model\Entity\WebAnalytics
*/
namespace RetailCrm\Api\Model\Entity\WebAnalytics;
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
/**
* Class Visit
*
* @category Visit
* @package RetailCrm\Api\Model\Entity\WebAnalytics
*/
class Visit
{
/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("createdAt")
*/
public $createdAt;
/**
* @var int
*
* @JMS\Type("integer")
* @JMS\SerializedName("visitLength")
*/
public $visitLength;
/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("exitPage")
*/
public $exitPage;
/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("landingPage")
*/
public $landingPage;
/**
* @var int
*
* @JMS\Type("integer")
* @JMS\SerializedName("pageViews")
*/
public $pageViews;
/**
* @var int
*
* @JMS\Type("integer")
* @JMS\SerializedName("pageDepth")
*/
public $pageDepth;
/**
* @var Customer
*
* @JMS\Type("RetailCrm\Api\Model\Entity\WebAnalytics\Customer")
* @JMS\SerializedName("customer")
*/
public $customer;
/**
* @var Source
*
* @JMS\Type("string")
* @JMS\SerializedName("source")
*/
public $source;
/**
* @var Page[]
*
* @JMS\Type("array<RetailCrm\Api\Model\Entity\WebAnalytics\Page>")
* @JMS\SerializedName("pages")
*/
public $pages;
/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("clientId")
*/
public $clientId;
/**
* @var string
*
* @JMS\Type("string")
* @JMS\SerializedName("site")
*/
public $site;
}

View File

@ -0,0 +1,41 @@
<?php
/**
* @category VisitsUploadRequest
* @package RetailCrm\Api\Model\Request\WebAnalytics
*/
namespace RetailCrm\Api\Model\Request\WebAnalytics;
use RetailCrm\Api\Component\FormData\Mapping as Form;
use RetailCrm\Api\Interfaces\RequestInterface;
/**
* Class VisitsUploadRequest
*
* @category VisitsUploadRequest
* @package RetailCrm\Api\Model\Request\WebAnalytics
*/
class VisitsUploadRequest implements RequestInterface
{
/**
* @var \RetailCrm\Api\Model\Entity\WebAnalytics\Visit[]
*
* @Form\Type("array<RetailCrm\Api\Model\Entity\WebAnalytics\Visit>")
* @Form\SerializedName("visits")
* @Form\JsonField()
*/
public $visits;
/**
* VisitsUploadRequest constructor.
*
* @param \RetailCrm\Api\Model\Entity\WebAnalytics\Visit[]|null $visits
*/
public function __construct(?array $visits = null)
{
if (null !== $visits) {
$this->visits = $visits;
}
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
* PHP version 7.3
*
* @category SourcesUploadResponse
* @package RetailCrm\Api\Model\Response\WebAnalytics
*/
namespace RetailCrm\Api\Model\Response\WebAnalytics;
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
use RetailCrm\Api\Model\Response\SuccessResponse;
/**
* Class VisitsUploadResponse
*
* @category VisitsUploadResponse
* @package RetailCrm\Api\Model\Response\WebAnalytics
*/
class VisitsUploadResponse extends SuccessResponse
{
/**
* @var \RetailCrm\Api\Model\Entity\WebAnalytics\Visit[]
*
* @JMS\Type("array<RetailCrm\Api\Model\Entity\WebAnalytics\Visit>")
* @JMS\SerializedName("failedVisits")
*/
public $failedVisits;
}

View File

@ -12,8 +12,10 @@ namespace RetailCrm\Api\ResourceGroup;
use RetailCrm\Api\Enum\RequestMethod; use RetailCrm\Api\Enum\RequestMethod;
use RetailCrm\Api\Model\Request\WebAnalytics\ClientIdUploadRequest; use RetailCrm\Api\Model\Request\WebAnalytics\ClientIdUploadRequest;
use RetailCrm\Api\Model\Request\WebAnalytics\SourcesUploadRequest; use RetailCrm\Api\Model\Request\WebAnalytics\SourcesUploadRequest;
use RetailCrm\Api\Model\Request\WebAnalytics\VisitsUploadRequest;
use RetailCrm\Api\Model\Response\WebAnalytics\ClientIdUploadResponse; use RetailCrm\Api\Model\Response\WebAnalytics\ClientIdUploadResponse;
use RetailCrm\Api\Model\Response\WebAnalytics\SourcesUploadResponse; use RetailCrm\Api\Model\Response\WebAnalytics\SourcesUploadResponse;
use RetailCrm\Api\Model\Response\WebAnalytics\VisitsUploadResponse;
/** /**
* Class WebAnalytics * Class WebAnalytics
@ -173,4 +175,97 @@ class WebAnalytics extends AbstractApiResourceGroup
return $response; return $response;
} }
/**
*
* Makes POST "/api/v5/web-analytics/client-ids/upload" request.
*
* Example:
* ```php
* use RetailCrm\Api\Factory\SimpleClientFactory;
* use RetailCrm\Api\Interfaces\ApiExceptionInterface;
* use RetailCrm\Api\Model\Entity\WebAnalytics\Visit;
* use RetailCrm\Api\Model\Entity\WebAnalytics\Customer;
* use RetailCrm\Api\Model\Request\WebAnalytics\VisitsUploadRequest;
*
* $client = SimpleClientFactory::createClient('https://test.retailcrm.pro', 'apiKey');
*
* $entity = new Visit();
* $dateTime = new \DateTime('2023-12-06T12:00:00');
* $entity->createdAt = $dateTime->format('Y-m-d H:i:s');
* $entity->visitLength = 10;
* $entity->exitPage = '/exit-page';
* $entity->landingPage = '/landing-page';
* $entity->pageViews = 5;
* $entity->pageDepth = 3;
* $entity->customer = new Customer();
* $entity->customer->id = 10;
* $entity->customer->externalId = 'externalId';
* $entity->source = new Source();
* $entity->source = "sourse";
* $entity->medium = "medium";
* $entity->campaign = "campaign";
* $entity->keyword = "keyword";
* $entity->content = "content";
* $page1 = new Page();
* $page1->url = ('/page1');
* $page1->title = ('Page 1');
* $page1->countViews = 2;
* $page1->timeOnPage = 60;
* $page2 = new Page();
* $page2->url = ('/page2');
* $page2->title = ('Page 2');
* $page2->countViews = 3;
* $page2->timeOnPage = 45;
* $pages = [$page1, $page2];
* $entity->pages = $pages;
* $entity->clientId = '123456';
* $entity->site = 'example.com';
*
* $request = new VisitsUploadRequest([$entity]);
*
* try {
* $response = $client->webAnalytics->visitsUpload($request);
* } catch (ApiExceptionInterface $exception) {
* echo sprintf(
* 'Error from RetailCRM API (status code: %d): %s',
* $exception->getStatusCode(),
* $exception->getMessage()
* );
*
* if (count($exception->getErrorResponse()->errors) > 0) {
* echo PHP_EOL . 'Errors: ' . implode(', ', $exception->getErrorResponse()->errors);
* }
*
* return;
* }
*
* echo 'Upload is successful';
* ```
*
* @param \RetailCrm\Api\Model\Request\WebAnalytics\VisitsUploadRequest $request
*
* @return \RetailCrm\Api\Model\Response\WebAnalytics\VisitsUploadResponse
* @throws \RetailCrm\Api\Interfaces\ApiExceptionInterface
* @throws \RetailCrm\Api\Interfaces\ClientExceptionInterface
* @throws \RetailCrm\Api\Exception\Api\AccountDoesNotExistException
* @throws \RetailCrm\Api\Exception\Api\ApiErrorException
* @throws \RetailCrm\Api\Exception\Api\MissingCredentialsException
* @throws \RetailCrm\Api\Exception\Api\MissingParameterException
* @throws \RetailCrm\Api\Exception\Api\ValidationException
* @throws \RetailCrm\Api\Exception\Client\HandlerException
* @throws \RetailCrm\Api\Exception\Client\HttpClientException
*/
public function visitsUpload(VisitsUploadRequest $request): VisitsUploadResponse
{
/** @var VisitsUploadResponse $response */
$response = $this->sendRequest(
RequestMethod::POST,
'web-analytics/visits/upload',
$request,
VisitsUploadResponse::class
);
return $response;
}
} }

View File

@ -15,9 +15,12 @@ use RetailCrm\Api\Interfaces\ApiExceptionInterface;
use RetailCrm\Api\Model\Entity\WebAnalytics\ClientId; use RetailCrm\Api\Model\Entity\WebAnalytics\ClientId;
use RetailCrm\Api\Model\Entity\WebAnalytics\Customer; use RetailCrm\Api\Model\Entity\WebAnalytics\Customer;
use RetailCrm\Api\Model\Entity\WebAnalytics\Order; use RetailCrm\Api\Model\Entity\WebAnalytics\Order;
use RetailCrm\Api\Model\Entity\WebAnalytics\Page;
use RetailCrm\Api\Model\Entity\WebAnalytics\Source; use RetailCrm\Api\Model\Entity\WebAnalytics\Source;
use RetailCrm\Api\Model\Entity\WebAnalytics\Visit;
use RetailCrm\Api\Model\Request\WebAnalytics\ClientIdUploadRequest; use RetailCrm\Api\Model\Request\WebAnalytics\ClientIdUploadRequest;
use RetailCrm\Api\Model\Request\WebAnalytics\SourcesUploadRequest; use RetailCrm\Api\Model\Request\WebAnalytics\SourcesUploadRequest;
use RetailCrm\Api\Model\Request\WebAnalytics\VisitsUploadRequest;
use RetailCrm\TestUtils\Factory\TestClientFactory; use RetailCrm\TestUtils\Factory\TestClientFactory;
use RetailCrm\TestUtils\TestCase\AbstractApiResourceGroupTestCase; use RetailCrm\TestUtils\TestCase\AbstractApiResourceGroupTestCase;
@ -97,4 +100,54 @@ EOF;
self::assertModelEqualsToResponse($json, $response); self::assertModelEqualsToResponse($json, $response);
} }
public function testVisitsUpload(): void
{
$json = '{"success": true}';
$entity = new Visit();
$dateTime = new \DateTime('2023-12-06T12:00:00');
$entity->createdAt = $dateTime->format('Y-m-d H:i:s');
$entity->visitLength = 10;
$entity->exitPage = '/exit-page';
$entity->landingPage = '/landing-page';
$entity->pageViews = 5;
$entity->pageDepth = 3;
$entity->customer = new Customer();
$entity->customer->id = 10;
$entity->customer->externalId = 'externalId';
$entity->source = new Source();
$entity->source->source = "source";
$entity->source->medium = "medium";
$entity->source->campaign = "campaign";
$entity->source->keyword = "keyword";
$entity->source->content = "content";
$page1 = new Page();
$page1->url = ('/page1');
$page1->title = ('Page 1');
$page1->countViews = 2;
$page1->timeOnPage = 60;
$page2 = new Page();
$page2->url = ('/page2');
$page2->title = ('Page 2');
$page2->countViews = 3;
$page2->timeOnPage = 45;
$pages = [$page1, $page2];
$entity->pages = $pages;
$entity->clientId = '123456';
$entity->site = 'example.com';
$request = new VisitsUploadRequest([$entity]);
$mock = static::createApiMockBuilder('web-analytics/visits/upload');
$mock->matchMethod(RequestMethod::POST)
->matchBody(self::encodeForm($request))
->reply(201)
->withBody($json);
$client = TestClientFactory::createClient($mock->getClient());
$response = $client->webAnalytics->visitsUpload($request);
self::assertModelEqualsToResponse($json, $response);
}
} }