Skip to content

Commit b433113

Browse files
committed
feat: Track host with requests
1 parent a3ad66b commit b433113

4 files changed

+37
-1
lines changed

src/LaravelServerAnalytics.php

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public function logRequest(Request $request, Response $response): Analytics
244244
$analytics = Analytics::create([
245245
'user_id' => $userId,
246246
'method' => $this->requestDetails->getMethod(),
247+
'host' => $this->requestDetails->getHost(),
247248
'path' => $this->requestDetails->getPath(),
248249
'status_code' => $this->requestDetails->getStatusCode(),
249250
'user_agent' => $this->requestDetails->getUserAgent(),

src/Models/Analytics.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function getTable()
1414
}
1515

1616
protected $fillable = [
17-
'user_id', 'path', 'method', 'status_code', 'duration_ms', 'user_agent', 'query_params', 'ip_address'
17+
'user_id', 'path', 'method', 'status_code', 'duration_ms', 'user_agent', 'query_params', 'ip_address', 'host'
1818
];
1919

2020
protected $casts = [

src/RequestDetails.php

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public function getMethod(): string
3333
return strtoupper($this->request->getMethod());
3434
}
3535

36+
/**
37+
* Returns the host of the request.
38+
*
39+
* @return string
40+
*/
41+
public function getHost(): string
42+
{
43+
return $this->request->getHost();
44+
}
45+
3646
/**
3747
* Returns the path of the request.
3848
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
// phpcs:ignoreFile
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;
9+
10+
class AddHostToAnalyticsTable extends Migration
11+
{
12+
public function up()
13+
{
14+
Schema::table(ServerAnalytics::getAnalyticsDataTable(), function (Blueprint $table) {
15+
$table->string('host')->nullable()->before('path');
16+
});
17+
}
18+
19+
public function down()
20+
{
21+
Schema::table(ServerAnalytics::getAnalyticsDataTable(), function (Blueprint $table) {
22+
$table->dropColumn('host');
23+
});
24+
}
25+
}

0 commit comments

Comments
 (0)