PHP API to get visitor counts

Hi all, I am writing a PHP script that I want to be able to get Visitor count information from a self hosted version of Matomo, is such a thing possible?

For this all I want to know is quantity, not sure I’m going to care about many details at this point.

I’ve checked out the API and can’t find a getVisitors call, is there such a thing?

Thanks!

Would this API answers your needs:
https://demo.matomo.cloud/?module=API&method=VisitsSummary.get&idSite=1&period=day&date=yesterday&format=xml&token_auth=anonymous

<result>
  <nb_uniq_visitors>1601</nb_uniq_visitors>
  <nb_users>2</nb_users>
  <nb_visits>1660</nb_visits>
  <nb_actions>2536</nb_actions>
  <nb_visits_converted>92</nb_visits_converted>
  <bounce_count>1208</bounce_count>
  <sum_visit_length>148690</sum_visit_length>
  <max_actions>15</max_actions>
  <bounce_rate>73%</bounce_rate>
  <nb_actions_per_visit>1.5</nb_actions_per_visit>
  <avg_time_on_site>90</avg_time_on_site>
</result>

API description:
https://developer.matomo.org/api-reference/reporting-api#VisitsSummary

Thanks Phillippe,

I believe it would. A couple of follow up questions:

  • Would it be better to use a key instead of being anonymous?
  • If I wanted to get the results for a week, would I need to submit the request for 7 times? or is there a way to ask it for a week’s worth of information in one request broken up by date?

Thanks!

Being anonymous means your Matomo is accessible anonymously, I suggest you don’t do that for security purposes (and the use of a key is not a problem, as the API would be used through your PHP, then the key would not be publicly visible).

The API provide several option:
https://developer.matomo.org/api-reference/reporting-api#standard-api-parameters

  • period — the period you request the statistics for. Can be any of: day , week , month , year or range . All reports are returned for the dates based on the website’s time zone.
    • day returns data for a given day.
    • week returns data for the week that contains the specified ‘date’
    • month returns data for the month that contains the specified ‘date’
    • year returns data for the year that contains the specified ‘date’
    • range returns data for the specified ‘date’ range.For example to request a report for the range Jan 1st to Feb 15th you would write &period=range&date=2011-01-01,2011-02-15

In this case you’ll get the cumulated nb_uniq_visitors / nb_users / … for the selected period.
If you need the detail for each day:

I tried to copy your example and I came up with the following url:

<matomo url>/?module=API&method=VisitsSummary.get&idSite=4&period=day&date=today,yesterdayformat=json&token_auth=<token>

this is producing the following error:

"Date format must be: YYYY-MM-DD, or 'today' or 'yesterday' or any keyword supported by the strtotime function (see http://php.net/strtotime for more information): today, yesterday"

Not sure what the issue is, or how to fix it. I’ve also tried adding a space between today and yesterday no change.

And amperstand (&) is missing between yesterday and format

Sorry I meant I was using the following:

?module=API&method=VisitsSummary.get&idSite=4&period=day&date=today,yesterday&format=json&token_auth=

Still getting the same error.

It seems you cannot append 2 date keywords…
Then you have to “calculate” the dates of yesterday and today into YYYY-MM-DD :wink:

OK, I wonder if there is also a limit to how many days you can do at one time…
/?module=API&method=VisitsSummary.get&idSite=4&period=day&date=2022-08-25,2022-08-24,2022-08-23,2022-08-22,2022-08-21,2022-08-20,2022-08-19,2022-08-18 also has the same error.

Yeah, it seems you can put only 2 dates…
Or you can also use API:

https://demo.matomo.cloud/index.php?module=API&format=XML&idSite=1&period=day&date=2022-07-26,2022-08-24&method=API.get&filter_limit=100&format_metrics=1&expanded=1&token_auth=anonymous&force_api_session=1

https://demo.matomo.cloud/index.php?module=API&format=XML&idSite=1&period=day&date=2022-07-26,2022-08-24&method=API.get&filter_limit=100&format_metrics=1&expanded=1&token_auth=anonymous&force_api_session=1

Thanks mate this API really helpful for my website as well