How to enable a message with the tracking visitors using PHP.
Source of API: https://developer.matomo.org/api-reference/reporting-api
An example of message in PHP:
7 tourist visitors are visiting this page!
How to enable a message with the tracking visitors using PHP.
Source of API: https://developer.matomo.org/api-reference/reporting-api
An example of message in PHP:
7 tourist visitors are visiting this page!
“visting” implies currently. That’s going to be harder to track than historical visitors…
If you mean historical visitors… Your API document you linked to says it returns a value:
So you’d read that value out of the API response.
To track visitors using PHP with the Matomo API, you can display the number of visitors like this.
<?php
$site_id = 1; // Your Matomo site ID
$matomo_url = 'https://your-matomo-url/matomo/';
$token = 'your_api_token'; // Your API token
// Fetch live visitor count
$url = $matomo_url . "index.php?module=API&method=Live.getCounters&idSite=$site_id&period=day&date=today&format=JSON&token_auth=$token";
$response = file_get_contents($url);
$data = json_decode($response, true);
echo $data[0]['visitors'] . " tourist visitors are visiting this page!";
?>
Just update the site_id
, matomo_url
, and token
with your actual values, and you’ll display the current visitor count.
Can you show me where in that documentation visitors
should be the name of a field in the API response? Their documentation seems… incorrect, if it is.
You can easily track visitors with cookies, using usernames which could be a timestamp and/or IP. The you can display a message based on count. With ASP, you use response.cookies for write and request.cookies for reading, then counting.
Not sure if that helps but I have been using that method for 25 years. You can get a little more elaborate if you want to involve the use of your database.
Welcome to the land of GDPR requirements…
What count? How does my cookie stored on my computer know about how many times you visited the site? Or in general, how many other people visited the site?
OP hasn’t indicated they have a database.
Pretty easy to do. Every time that cookie visits the site, you add +1 each time. The cookie then stores the count
But the cookie lives on MY computer.
So let’s say we’ve got Alice and Bob.
Alice visits the site. Site sees no cookie, generates a new cookie, gives it a value of 1, and hands it to Alice, whose computer stores the cookie.
Alice visits the site again. Site sees Alice’s cookie come in with the request. Adds one to the value (2), and gives it back to Alice, who stores it.
Now Bob visits the site. He has no cookie. The server…? (Hint: The server has no idea what’s in the value of Alice’s cookie, because she’s not the one doing the request.)
(And when the answer inevitably comes that the server gives Bob a cookie with a value of 1, i repeat: How is this counting the number of other visitors to the site?)
I was giving a very basic answer with cookies. If Alice and Bob use the same computer then you are not going to get an accurate count for each user, but you would get an accurate count for that computer or at least user a specific browser. But if you have users from different computers, you can get an accurate account. Your best accuracy if you have logins of each user, then you can get an accurate account of each user. Even though the original question didn’t mention databases, that would be the most accurate method as you can delete cookies. Nothing full proof here, but cookies is just a way that can work depending the circumstances without a lot of work.
I… dont think you’re getting the point. Or rather, you’ve got it backwards.
They’re not trying to track the number of THIS USER’s visits, they’re trying to track the number of ALL visits. (That’s… why they’re using the API. I think strictly they’re looking at ALL visits or unique vistors within a set timeframe, but the point stands)
Thank you for the messages!
I see there are some scripts on the market:
IMHO it will be simple and effective to use GTM in this case as we know IP should not be transferred from GTM due to GDPR requirements like legal stuff, cookies, etc.
Maybe somebody has GTM code to simply push from containers and events. IP address is not important as other analytics can be tracked but a message is also a positive trigger.