Skip to content

Commit 02ef8d9

Browse files
committed
feat: Major package improvements and new features
- Fixed critical RequestDataDTO typo bug - Added database indexes for improved query performance - Fixed response time data type consistency - Implemented comprehensive bot/crawler detection - Added IP geolocation service with multiple providers - Implemented caching layer for analytics queries - Added unique visitor tracking with cookies - Created comprehensive REST API endpoints - Added data export functionality (CSV and JSON) - Implemented GDPR compliance features - Added comprehensive test suite - Updated documentation New features include bot detection, geolocation, visitor tracking, API endpoints, data export, and privacy compliance options.
1 parent 4ade4d5 commit 02ef8d9

18 files changed

Lines changed: 1264 additions & 67 deletions

README.md

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,32 @@ return [
4343
'capture' => [
4444
'web' => true,
4545
'api' => true,
46+
'bots' => false, // Set to true to capture bot traffic
4647
],
4748

4849
'queue' => [
49-
'enabled' => env('REQUEST_ANALYTICS_QUEUE_ENABLED', true),
50+
'enabled' => env('REQUEST_ANALYTICS_QUEUE_ENABLED', false),
5051
],
5152

5253
'ignore-paths' => [
53-
54+
// Add paths to ignore, e.g., 'admin', 'api/health'
5455
],
5556

5657
'pruning' => [
5758
'enabled' => env('REQUEST_ANALYTICS_PRUNING_ENABLED', true),
5859
'days' => env('REQUEST_ANALYTICS_PRUNING_DAYS', 90),
5960
],
61+
62+
'geolocation' => [
63+
'enabled' => env('REQUEST_ANALYTICS_GEO_ENABLED', true),
64+
'provider' => env('REQUEST_ANALYTICS_GEO_PROVIDER', 'ipapi'), // ipapi, ipgeolocation, maxmind
65+
'api_key' => env('REQUEST_ANALYTICS_GEO_API_KEY'),
66+
],
67+
68+
'privacy' => [
69+
'anonymize_ip' => env('REQUEST_ANALYTICS_ANONYMIZE_IP', false),
70+
'respect_dnt' => env('REQUEST_ANALYTICS_RESPECT_DNT', true), // Respect Do Not Track header
71+
],
6072
];
6173
```
6274
### Data Purning
@@ -114,17 +126,65 @@ Optionally, you can publish the views using
114126
php artisan vendor:publish --tag="request-analytics-views"
115127
```
116128

129+
## Features
130+
131+
- 📊 **Real-time Analytics Dashboard** - Beautiful, responsive dashboard with charts and metrics
132+
- 🤖 **Bot Detection** - Automatically filters out bot traffic (configurable)
133+
- 🌍 **IP Geolocation** - Track visitor locations using multiple providers
134+
- 🔒 **Privacy Focused** - GDPR compliant with IP anonymization and DNT support
135+
- 🚀 **High Performance** - Built-in caching and optimized database queries
136+
- 📱 **Device Detection** - Track browsers, operating systems, and devices
137+
- 👥 **Visitor Tracking** - Unique visitor identification with cookie-based tracking
138+
- 📤 **Export Data** - Export analytics data to CSV or JSON formats
139+
- 🔌 **REST API** - Full-featured API for programmatic access
140+
- 🧹 **Auto Cleanup** - Automatic data pruning to manage database size
141+
117142
## Usage
118143

119-
```php
120-
$requestAnalytics = new MeShaon\RequestAnalytics();
121-
echo $requestAnalytics->echoPhrase('Hello, MeShaon!');
122-
```
144+
### Dashboard Access
145+
146+
After installation, the analytics dashboard is available at `/analytics` (configurable). Users must be authenticated and implement the `CanAccessAnalyticsDashboard` interface.
147+
## API Endpoints
148+
149+
The package provides a comprehensive REST API for accessing analytics data:
150+
151+
- `GET /api/v1/analytics/overview` - Get analytics overview with summary and charts
152+
- `GET /api/v1/analytics/visitors` - Get paginated visitor data
153+
- `GET /api/v1/analytics/page-views` - Get paginated page view data
154+
- `POST /api/v1/analytics/export` - Export analytics data to CSV or JSON
155+
156+
### API Authentication
157+
158+
API endpoints use Laravel Sanctum for authentication. Ensure your API consumers have valid tokens.
159+
160+
## Configuration Options
161+
162+
### Geolocation Providers
163+
164+
The package supports multiple geolocation providers:
165+
166+
1. **IP-API** (default, free): No API key required, limited to 45 requests per minute
167+
2. **IPGeolocation**: Requires API key from [ipgeolocation.io](https://ipgeolocation.io)
168+
3. **MaxMind**: Requires GeoIP2 database or web service account
169+
170+
### Privacy Settings
171+
172+
- **IP Anonymization**: Enable to anonymize the last octet of IPv4 addresses
173+
- **Do Not Track**: Respect the DNT browser header (enabled by default)
174+
175+
### Bot Detection
176+
177+
The package automatically detects and filters common bots and crawlers including:
178+
- Search engine bots (Google, Bing, Yahoo, etc.)
179+
- Social media bots (Facebook, Twitter, LinkedIn, etc.)
180+
- SEO tools (Ahrefs, SEMrush, etc.)
181+
- Monitoring services (Pingdom, UptimeRobot, etc.)
182+
- Development tools (curl, wget, Postman, etc.)
183+
123184
## Access Control
124185

125186
### Web Access
126187
To control access to the dashboard, implement the `CanAccessAnalyticsDashboard` interface in your User model:
127-
Then you can use the `canAccessAnalyticsDashboard` method in your your `User` model:
128188
```php
129189
<?php
130190

config/request-analytics.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'capture' => [
1010
'web' => true,
1111
'api' => true,
12+
'bots' => false, // Set to true to capture bot traffic
1213
],
1314

1415
'queue' => [
@@ -23,4 +24,15 @@
2324
'enabled' => env('REQUEST_ANALYTICS_PRUNING_ENABLED', true),
2425
'days' => env('REQUEST_ANALYTICS_PRUNING_DAYS', 90),
2526
],
27+
28+
'geolocation' => [
29+
'enabled' => env('REQUEST_ANALYTICS_GEO_ENABLED', true),
30+
'provider' => env('REQUEST_ANALYTICS_GEO_PROVIDER', 'ipapi'), // ipapi, ipgeolocation, maxmind
31+
'api_key' => env('REQUEST_ANALYTICS_GEO_API_KEY'),
32+
],
33+
34+
'privacy' => [
35+
'anonymize_ip' => env('REQUEST_ANALYTICS_ANONYMIZE_IP', false),
36+
'respect_dnt' => env('REQUEST_ANALYTICS_RESPECT_DNT', true), // Respect Do Not Track header
37+
],
2638
];
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table('request_analytics', function (Blueprint $table) {
12+
$table->index('visited_at');
13+
$table->index('session_id');
14+
$table->index('path');
15+
$table->index('user_id');
16+
$table->index('request_category');
17+
$table->index(['visited_at', 'session_id']);
18+
$table->index(['path', 'visited_at']);
19+
$table->index(['browser', 'visited_at']);
20+
$table->index(['operating_system', 'visited_at']);
21+
$table->index(['country', 'visited_at']);
22+
});
23+
}
24+
25+
public function down()
26+
{
27+
Schema::table('request_analytics', function (Blueprint $table) {
28+
$table->dropIndex(['visited_at']);
29+
$table->dropIndex(['session_id']);
30+
$table->dropIndex(['path']);
31+
$table->dropIndex(['user_id']);
32+
$table->dropIndex(['request_category']);
33+
$table->dropIndex(['visited_at', 'session_id']);
34+
$table->dropIndex(['path', 'visited_at']);
35+
$table->dropIndex(['browser', 'visited_at']);
36+
$table->dropIndex(['operating_system', 'visited_at']);
37+
$table->dropIndex(['country', 'visited_at']);
38+
});
39+
}
40+
};

database/migrations/create_request_analytics_table.php.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ return new class extends Migration
2323
$table->string('language')->nullable();
2424
$table->tinyText('query_params')->nullable();
2525
$table->string('session_id');
26+
$table->string('visitor_id')->nullable()->index();
2627
$table->foreignId('user_id')->nullable()->constrained();
2728
$table->string('http_method');
2829
$table->string('request_category');

routes/web.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
use Illuminate\Support\Facades\Route;
44
use MeShaon\RequestAnalytics\Controllers\RequestAnalyticsController;
5+
use MeShaon\RequestAnalytics\Controllers\Api\AnalyticsApiController;
56

67
Route::middleware(['web', 'auth', 'request-analytics.access'])
78
->get(config('request-analytics.route.pathname'), [RequestAnalyticsController::class, 'show'])
89
->name(config('request-analytics.route.name'));
910

1011
Route::middleware(['api', 'auth:sanctum', 'request-analytics.access'])
11-
->get('api/v1'.config('request-analytics.route.pathname'), function () {
12-
return response()->json([
13-
'data' => 'Coming soon...',
14-
]);
15-
})->name(config('request-analytics.route.name').'.api');
12+
->prefix('api/v1/analytics')
13+
->name('request-analytics.api.')
14+
->group(function () {
15+
Route::get('/overview', [AnalyticsApiController::class, 'overview'])->name('overview');
16+
Route::get('/visitors', [AnalyticsApiController::class, 'visitors'])->name('visitors');
17+
Route::get('/page-views', [AnalyticsApiController::class, 'pageViews'])->name('page-views');
18+
Route::post('/export', [AnalyticsApiController::class, 'export'])->name('export');
19+
});

0 commit comments

Comments
 (0)