Skip to content

Commit 053b148

Browse files
authored
Merge pull request #29 from binafy/feature-ajax-request
[1.x] Add `Ajax` mode condition for visit monitoring
2 parents a7f4466 + 8dba319 commit 053b148

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- [Delete Visit Monitoring Records By Specific Days](#delete-visit-monitoring-records-by-specific-days)
2020
- [Turn ON-OFF](#turn-on-off)
2121
- [Views](#visit-monitoring-views)
22+
- [Ajax Requests](#ajax-requests)
2223
- [Action Monitoring](#action-monitoring)
2324
- [Views](#action-monitoring-views)
2425
- [Authentication Monitoring](#authentication-monitoring)
@@ -289,6 +290,26 @@ Laravel-User-Monitoring also has an amazing views that you can use it very easy,
289290

290291
![Visit Monitoring Preview](/art/visits-monitoring/preview.png "Visit Monitoring")
291292

293+
<a name="ajax-requests"></a>
294+
### Ajax Requests
295+
296+
Maybe you may disable record visits for `Ajax` requests, you can use config to disable it:
297+
298+
```php
299+
'visit_monitoring' => [
300+
...
301+
302+
/*
303+
* If you want to disable visit monitoring in Ajax mode, set it to false.
304+
*/
305+
'ajax_requests' => true,
306+
307+
...
308+
],
309+
```
310+
311+
When set to false, Ajax requests will not be recorded.
312+
292313
<a name="action-monitoring"></a>
293314
## Action Monitoring
294315

config/user-monitoring.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@
5151
'table' => 'visits_monitoring',
5252

5353
/*
54-
* If you want to disable visit monitoring, you can change it to false.
54+
* If you want to disable visit monitoring, set it to false.
5555
*/
5656
'turn_on' => true,
5757

58+
/*
59+
* If you want to disable visit monitoring in Ajax mode, set it to false.
60+
*/
61+
'ajax_requests' => true,
62+
5863
/*
5964
* You can specify pages not to be monitored.
6065
*/

src/Middlewares/VisitMonitoringMiddleware.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7-
use Illuminate\Http\Response;
87
use Illuminate\Support\Facades\DB;
98
use Jenssegers\Agent\Agent;
109

1110
class VisitMonitoringMiddleware
1211
{
1312
/**
14-
* Handle.
15-
*
16-
* @param Request $request
17-
* @param Closure $next
18-
* @return Response
13+
* Handle monitor visiting.
1914
*/
20-
public function handle(Request $request, Closure $next)
15+
public function handle(Request $request, Closure $next): mixed
2116
{
2217
if (config('user-monitoring.visit_monitoring.turn_on', false) === false) {
2318
return $next($request);
2419
}
20+
if (config('user-monitoring.visit_monitoring.ajax_requests', false) === false && $request->ajax()) {
21+
return $next($request);
22+
}
2523

2624
$agent = new Agent();
2725
$guard = config('user-monitoring.user.guard', 'web');
@@ -46,12 +44,8 @@ public function handle(Request $request, Closure $next)
4644

4745
/**
4846
* Check request page are exists in expect pages.
49-
*
50-
* @param string $page
51-
* @param array $exceptPages
52-
* @return bool
5347
*/
54-
private function checkIsExceptPages(string $page, array $exceptPages)
48+
private function checkIsExceptPages(string $page, array $exceptPages): bool
5549
{
5650
return collect($exceptPages)->contains($page);
5751
}

tests/Feature/VisitMonitoringTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@
5959
assertDatabaseMissing(config('user-monitoring.visit_monitoring.table'), ['page' => url('/')]);
6060
});
6161

62+
// Ajax
63+
64+
test('visit monitoring store ajax requests', function () {
65+
get('/', ['X-Requested-With' => 'XMLHttpRequest']);
66+
67+
// DB Assertions
68+
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 1);
69+
assertDatabaseHas(config('user-monitoring.visit_monitoring.table'), ['created_at' => now()]);
70+
});
71+
72+
test('visit monitoring skip store when ajax mode is off for ajax requests', function () {
73+
config()->set('user-monitoring.visit_monitoring.ajax_requests', false);
74+
75+
get('/', ['X-Requested-With' => 'XMLHttpRequest']);
76+
77+
// DB Assertions
78+
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 0);
79+
assertDatabaseMissing(config('user-monitoring.visit_monitoring.table'), ['page' => 'http:\/\/localhost']);
80+
});
81+
6282
/**
6383
* Create user.
6484
*

0 commit comments

Comments
 (0)