Русский | English
The reports API allows you to get information about site visit statistics and other data.
More details about the methods can be found in documentation .
use Hywax \YaMetrika \Service \ReportService ;
use Hywax \YaMetrika \Transformer \ReportDataTransformer ;
$ reportService = new ReportService ([
'token' => 'YOUR_TOKEN' ,
'counterId' => 'YOUR_COUNTER_ID' ,
'resultTransformer' => new ReportDataTransformer (),
]);
For ease of use with reports, there are 2 ways to pass counterId
.
In constructor of ReportService
class, counterId
parameter.
Using setCounterId
method.
Both methods will pass the counter ID to API requests. Supports both a single ID and an array of counters.
You can use 2 classes to convert data from the API response into a convenient format by passing the resultTransformer
parameter:
ReportDataTransformer::class
- converts data into a convenient format.
ReportRawTransformer::class
- returns data in "raw form".
In addition, you can create your own class that implements the Transformer
interface.
The following data will be obtained: visits, views, unique visitors by day.
public function getVisitors(int $ days = 30 ): array
Name
Type
Description
$days
integer
Number of days. Default 30
public function getVisitorsForPeriod(DateTime $ startDate , DateTime $ endDate ): array
Название
Тип
Описание
$startDate
DateTime
Start date
$endDate
DateTime
End date
public function getMostViewedPages(int $ days = 30 , int $ limit = 10 ): array
Name
Type
Description
$days
integer
Number of days. Default 30
$limit
integer
Record limit. Default 10
public function getMostViewedPagesForPeriod(DateTime $ startDate , DateTime $ endDate , int $ limit = 10 ): array
Name
Type
Description
$startDate
DateTime
Start date
$endDate
DateTime
End date
$limit
integer
Record limit. Default 10
public function getBrowsers(int $ days = 30 , int $ limit = 10 ): array
Name
Type
Description
$days
integer
Number of days. Default 30
$limit
integer
Record limit. Default 10
public function getBrowsersForPeriod(DateTime $ startDate , DateTime $ endDate , int $ limit = 10 ): array
Name
Type
Description
$startDate
DateTime
Start date
$endDate
DateTime
End date
$limit
integer
Record limit. Default 10
Users from search engines
public function getUsersSearchEngine(int $ days = 30 , int $ limit = 10 ): array
Name
Type
Description
$days
integer
Number of days. Default 30
$limit
integer
Record limit. Default 10
public function getUsersSearchEngineForPeriod(DateTime $ startDate , DateTime $ endDate , int $ limit = 10 ): array
Name
Type
Description
$startDate
DateTime
Start date
$endDate
DateTime
End date
$limit
integer
Record limit. Default 10
Users by country and region
public function getGeo($ days = 7 , $ limit = 20 ): array
Name
Type
Description
$days
integer
Number of days. Default 7
$limit
integer
Record limit. Default 20
public function getGeoForPeriod(DateTime $ startDate , DateTime $ endDate , int $ limit = 20 ): array
Name
Type
Description
$startDate
DateTime
Start date
$endDate
DateTime
End date
$limit
integer
Record limit. Default 20
public function getAgeGender($ days = 30 , $ limit = 20 ): array
Name
Type
Description
$days
integer
Number of days. Default 30
$limit
integer
Record limit. Default 20
public function getAgeGenderForPeriod(DateTime $ startDate , DateTime $ endDate , int $ limit = 20 ): array
Name
Type
Description
$startDate
DateTime
Start date
$endDate
DateTime
End date
$limit
integer
Record limit. Default 20
public function getSearchPhrases($ days = 30 , $ limit = 20 ): array
Name
Type
Description
$days
integer
Number of days. Default 30
$limit
integer
Record limit. Default 20
public function getSearchPhrasesForPeriod(DateTime $ startDate , DateTime $ endDate , int $ limit = 20 ): array
Name
Type
Description
$startDate
DateTime
Start date
$endDate
DateTime
End date
$limit
integer
Record limit. Default 20
Templates (preset) automatically define the metrics and groupings that are required for a particular report.
A list of all templates is available in documentation .
public function getPreset(string $ preset , int $ days = 30 , int $ limit = 10 ): array
Name
Type
Description
$preset
string
Present name
$days
integer
Number of days. Default 30
$limit
integer
Record limit. Default 10
public function getPresetForPeriod(string $ preset , DateTime $ startDate , DateTime $ endDate , int $ limit = 10 ): array
Name
Type
Description
$preset
string
Present name
$startDate
DateTime
Start date
$endDate
DateTime
End date
$limit
integer
Record limit. Default 10
If you pass ids
, the counter ID will be replaced for the duration of one request.
public function getCustomQuery(array $ params ): array
Name
Type
Description
$params
array
Request params