Published on August 10, 2022

Monitor when a user exceeds the usage limit for your PHP service

Monitor when a user exceeds the usage limit for your PHP service

These days, a lot of services are moving to a pay-as-you-go model. This model is either based on a monthly usage limit or metered usage. This is especially common for cloud, software, and other online services. So, chances are that if you're building a service with PHP, you'll have to roll your usage model and limits.

No matter your implementation, you will be required to set up an internal system to track usage and notify yourself and your team when a user has reached their limit. This is a common enough problem as it helps you understand how your users use your service, and you can improve your product based on that.

LogSnag is a service that helps you monitor your important events in real-time. It's an excellent tool for this problem and works seamlessly with PHP. In addition, LogSnag makes it trivial to send events to your dashboard and receive push notifications when something important happens.

For example, let's say you're building a service with PHP that allows users to upload files. However, you want to limit the number of files a user can upload to 10. You can use LogSnag to send an event to your dashboard when a user uploads a file. You can then set up a rule to notify you when a user has uploaded ten files. This way, you will know when a user has reached their limit, allowing you to take further action if needed.


Setting up LogSnag

  1. Sign up for a free LogSnag account.
  2. Create your first project from the dashboard.
  3. Head to settings and copy your API token.

PHP code snippets

Copy the following code snippet to your PHP project. Please note that you will need to replace the API token with your own.

Using PHP with cURL
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.logsnag.com/v1/log',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{"project":"my-saas","channel":"limits","event":"Usage Limit Exceeded","description":"The user has exceeded the usage limit for the service.","icon":"🚨","notify":true}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer YOUR_API_TOKEN'
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
Using PHP with Guzzle
<?php
$client = new Client();
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer YOUR_API_TOKEN'
];
$body = '{
"project": "my-saas",
"channel": "limits",
"event": "Usage Limit Exceeded",
"description": "The user has exceeded the usage limit for the service.",
"icon": "🚨",
"notify": true
}';
$request = new Request('POST', 'https://api.logsnag.com/v1/log', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();
Using PHP with HTTP_Request2
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('https://api.logsnag.com/v1/log');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
'follow_redirects' => TRUE
));
$request->setHeader(array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer YOUR_API_TOKEN'
));
$request->setBody('{"project":"my-saas","channel":"limits","event":"Usage Limit Exceeded","description":"The user has exceeded the usage limit for the service.","icon":"🚨","notify":true}');
try {
$response = $request->send();
if ($response->getStatus() == 200) {
echo $response->getBody();
}
else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
}
catch(HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
Using PHP with pecl_http
<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl('https://api.logsnag.com/v1/log');
$request->setRequestMethod('POST');
$body = new http\Message\Body;
$body->append('{"project":"my-saas","channel":"limits","event":"Usage Limit Exceeded","description":"The user has exceeded the usage limit for the service.","icon":"🚨","notify":true}');
$request->setBody($body);
$request->setOptions(array());
$request->setHeaders(array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer YOUR_API_TOKEN'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();

PHP integration details

LogSnag is a flexible and easy-to-use event tracking service that works excellently with PHP. In addition to real-time event tracking and cross-platform push notifications, LogSnag provides powerful user journey tracking, simple event filtering, search, and analytic tools such as charts.

In addition to tracking usage events, you can also use LogSnag to track other important events such as errors, user sign-ups, user logins, payments, or anything else you can think of.

Setting up LogSnag with your PHP application takes a few minutes, and you can start tracking events in no time.

Other use-cases for LogSnag

  1. Monitor your CI/CD build status for your PHP application
  2. Monitor your CPU usage in your PHP application
  3. Monitor when database goes down in your PHP application
  4. Monitor high disk usage in your PHP application
  5. Monitor when a user changes their email address in your PHP application
  6. Monitor failed logins in your PHP application
  7. Monitor failed payments for your PHP application
  8. Monitor memory usage in your PHP application
  9. Monitor MySQL downtime in your PHP application
  10. Monitor when a new feature is used in your PHP application
  11. Monitor your Postgres downtime in your PHP application
  12. Monitor Redis downtime in your PHP application
  13. Monitor suspicious activity in your PHP application
  14. Monitor when a user is being rate limited in your PHP application
  15. Get a notification when your PHP code is done executing
  16. Send push notifications to your phone or desktop using PHP
  17. Track canceled subscriptions in your PHP application
  18. Track your PHP cron jobs
  19. Track when a file is uploaded to your PHP application
  20. Track when a form is submitted to your PHP application
  21. Track payment events via PHP
  22. Track user sign in events in PHP
  23. Track user signup events via PHP
  24. Track waitlist signup events via PHP
View all common use-cases with PHP