PHPackages                             me-shaon/laravel-request-analytics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. me-shaon/laravel-request-analytics

ActiveLibrary

me-shaon/laravel-request-analytics
==================================

Simple request data analytics package for Laravel projects

v1.8.0(7mo ago)2076.8k↑172.9%25[1 issues](https://github.com/me-shaon/laravel-request-analytics/issues)[4 PRs](https://github.com/me-shaon/laravel-request-analytics/pulls)MITPHPPHP ^8.1 | ^8.2 | ^8.3CI passing

Since Mar 4Pushed 1mo ago5 watchersCompare

[ Source](https://github.com/me-shaon/laravel-request-analytics)[ Packagist](https://packagist.org/packages/me-shaon/laravel-request-analytics)[ Docs](https://github.com/me-shaon/laravel-request-analytics)[ GitHub Sponsors](https://github.com/me-shaon)[ RSS](/packages/me-shaon-laravel-request-analytics/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (18)Used By (0)

Laravel Request Analytics
=========================

[](#laravel-request-analytics)

[![Latest Version on Packagist](https://camo.githubusercontent.com/65a015c6ad647c4bea779a3a303d269a53adb3da1d6bab38ca953a5db1737424/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d652d7368616f6e2f6c61726176656c2d726571756573742d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/me-shaon/laravel-request-analytics)[![GitHub Tests Action Status](https://camo.githubusercontent.com/5bda42fcda3af05cc28a21ccb2dd7261fca3c3669af95a776bea762602be789a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d652d7368616f6e2f6c61726176656c2d726571756573742d616e616c79746963732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/me-shaon/laravel-request-analytics/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/a47d42898c85f3ed09386d83a3d7b917b407d90b9e66ca045ef3a8f2611f49fc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d652d7368616f6e2f6c61726176656c2d726571756573742d616e616c79746963732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/me-shaon/laravel-request-analytics/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/8ddb0318a3cf6c37230d41d0974dbc5455445ff8ed44fc4014330421e103486c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d652d7368616f6e2f6c61726176656c2d726571756573742d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/me-shaon/laravel-request-analytics)

[![Laravel request analytics](https://github.com/me-shaon/laravel-request-analytics/raw/main/banner_preview.png?raw=true)](https://github.com/me-shaon/laravel-request-analytics/blob/main/banner_preview.png?raw=true)

Overview
--------

[](#overview)

Laravel Request Analytics is a comprehensive web analytics solution designed specifically for Laravel applications. This package provides detailed insights into your application's traffic patterns, user behavior, and performance metrics through an intuitive dashboard and powerful API endpoints.

Built with performance and privacy in mind, the package offers intelligent bot detection, IP geolocation services, and GDPR-compliant data handling. Whether you're running a small blog or a large-scale application, Laravel Request Analytics provides the tools you need to understand your audience and optimize user experience.

Installation
------------

[](#installation)

### Requirements

[](#requirements)

- PHP 8.1 or higher
- Laravel 10.0 or higher
- MySQL 5.7+ / PostgreSQL 10+ / SQLite 3.8+

### Quick Installation (Recommended) ✨

[](#quick-installation-recommended-)

Get up and running in just two commands:

```
# Install the package
composer require me-shaon/laravel-request-analytics

# Run the interactive installer
php artisan request-analytics:install
```

This interactive installer will:

- Welcome you with helpful information about the package
- Automatically publish configuration files, assets, and views
- Publish and optionally run database migrations
- Optionally prompt you to star our GitHub repository
- Provide next steps and dashboard access information

After running the installer, your analytics dashboard will be ready at `/analytics`!

[![Package Installation Guideline Video Banner](https://github.com/me-shaon/laravel-request-analytics/raw/main/package-installation-guideline-video-banner.webp?raw=true)](https://youtu.be/m3m6t2v0YR8)

### Manual Setup (Advanced)

[](#manual-setup-advanced)

For users who prefer manual control over the installation process or need custom configurations:

#### Database Setup

[](#database-setup)

Publish and run the migrations:

```
php artisan vendor:publish --tag="request-analytics-migrations"
php artisan migrate
```

#### Configuration

[](#configuration)

Publish the configuration file:

```
php artisan vendor:publish --tag="request-analytics-config"
```

The configuration file will be published to `config/request-analytics.php` with the following options:

```
return [
    'database' => [
        'connection' => env('REQUEST_ANALYTICS_DB_CONNECTION', null), // Use default connection if null
        'table' => env('REQUEST_ANALYTICS_TABLE_NAME', 'request_analytics'),
    ],

    'route' => [
        'name' => 'request.analytics',
        'pathname' => env('REQUEST_ANALYTICS_PATHNAME', 'analytics'),
    ],

    'capture' => [
        'web' => true,
        'api' => true,
        'bots' => false, // Set to true to capture bot traffic
    ],

    'middleware' => [
        'web' => [
            'web',
            // 'auth', // Uncomment if using web authentication
            'request-analytics.access',
        ],
        'api' => [
            'api',
            // 'auth:sanctum', // Uncomment if using Sanctum authentication
            'request-analytics.access',
        ],
    ],

    'queue' => [
        'enabled' => env('REQUEST_ANALYTICS_QUEUE_ENABLED', false),
        'on_queue' => env('REQUEST_ANALYTICS_ON_QUEUE', 'default'),
    ],

    'ignore-paths' => [
        env('REQUEST_ANALYTICS_PATHNAME', 'analytics'),
        'broadcasting/auth',
        'livewire/*',
    ],

    'skip_ips' => [
        // '127.0.0.1',
        // '192.168.1.0/24',
        // Add IP addresses or CIDR blocks to skip tracking
    ],

    'skip_referrers' => [
        // 'spam-site.com',
        // 'unwanted-referrer.com',
        // Add referrer domains to skip tracking
    ],

    'pruning' => [
        'enabled' => env('REQUEST_ANALYTICS_PRUNING_ENABLED', true),
        'days' => env('REQUEST_ANALYTICS_PRUNING_DAYS', 90),
    ],

    'geolocation' => [
        'enabled' => env('REQUEST_ANALYTICS_GEO_ENABLED', true),
        'provider' => env('REQUEST_ANALYTICS_GEO_PROVIDER', 'ipapi'), // ipapi, ipgeolocation, maxmind
        'api_key' => env('REQUEST_ANALYTICS_GEO_API_KEY'),

        // MaxMind specific configuration
        'maxmind' => [
            'type' => env('REQUEST_ANALYTICS_MAXMIND_TYPE', 'webservice'), // webservice or database
            'user_id' => env('REQUEST_ANALYTICS_MAXMIND_USER_ID'),
            'license_key' => env('REQUEST_ANALYTICS_MAXMIND_LICENSE_KEY'),
            'database_path' => env('REQUEST_ANALYTICS_MAXMIND_DB_PATH', storage_path('app/GeoLite2-City.mmdb')),
        ],
    ],

    'privacy' => [
        'anonymize_ip' => env('REQUEST_ANALYTICS_ANONYMIZE_IP', false),
    ],

    'cache' => [
        'ttl' => env('REQUEST_ANALYTICS_CACHE_TTL', 5), // Cache TTL in minutes
    ],
];
```

#### Publish Assets &amp; Views

[](#publish-assets--views)

Publish dashboard assets:

```
php artisan vendor:publish --tag="request-analytics-assets"
```

Optionally, publish the views for customization:

```
php artisan vendor:publish --tag="request-analytics-views"
```

#### Publishing Options

[](#publishing-options)

The package supports granular publishing of individual components:

```
# Publish specific components
php artisan vendor:publish --tag="request-analytics-config"
php artisan vendor:publish --tag="request-analytics-migrations"
php artisan vendor:publish --tag="request-analytics-assets"
php artisan vendor:publish --tag="request-analytics-views"

# Or publish everything at once
php artisan vendor:publish --provider="MeShaon\RequestAnalytics\RequestAnalyticsServiceProvider"
```

### Automated Data Pruning

[](#automated-data-pruning)

The package includes automatic data cleanup to manage database size. Configure pruning in your scheduler:

**Laravel 11+**

Add to `routes/console.php`:

```
use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune', [
    '--model' => 'MeShaon\RequestAnalytics\Models\RequestAnalytics',
])->monthly();
```

Or in `bootstrap/app.php`:

```
use Illuminate\Console\Scheduling\Schedule;

->withSchedule(function (Schedule $schedule) {
    $schedule->command('model:prune', [
        '--model' => 'MeShaon\RequestAnalytics\Models\RequestAnalytics',
    ])->monthly();
})
```

**Laravel 10 and below**

Add to `app/Console/Kernel.php`:

```
protected function schedule(Schedule $schedule): void
{
    $schedule->command('model:prune', [
        '--model' => 'MeShaon\RequestAnalytics\Models\RequestAnalytics',
    ])->monthly();
}
```

Key Features
------------

[](#key-features)

### Analytics &amp; Reporting

[](#analytics--reporting)

- **Real-time Dashboard**: Interactive charts and metrics with responsive design
- **Comprehensive Metrics**: Page views, unique visitors, bounce rates, and session duration
- **Traffic Analysis**: Detailed breakdown of traffic sources and user pathways
- **Performance Insights**: Load times and user interaction patterns

### Privacy &amp; Compliance

[](#privacy--compliance)

- **GDPR Compliance**: Built-in privacy controls and data anonymization
- **IP Anonymization**: Configurable IP address masking for user privacy
- **Data Retention**: Configurable automatic data pruning and cleanup

### Intelligence &amp; Detection

[](#intelligence--detection)

- **Advanced Bot Detection**: Filters search engines, social bots, and crawlers
- **IP Address Filtering**: Skip tracking for specific IP addresses or CIDR blocks (e.g., internal IPs, admin IPs)
- **Referrer Filtering**: Exclude tracking for requests from specific referrer domains (e.g., spam sites, unwanted sources)
- **Device Recognition**: Browser, OS, and device type identification
- **Geolocation Services**: Multiple provider support (IP-API, IPGeolocation, MaxMind)
- **Visitor Tracking**: Cookie-based unique visitor identification

### Performance &amp; Integration

[](#performance--integration)

- **High Performance**: Optimized database queries with intelligent caching
- **Queue Support**: Background processing for high-traffic applications
- **REST API**: Complete programmatic access to analytics data
- **Laravel Integration**: Seamless integration with Laravel's authentication and middleware systems

Usage
-----

[](#usage)

### Getting Started

[](#getting-started)

After installation, the package automatically starts tracking requests based on your configuration. Here's what happens next:

1. **Dashboard Access**: Visit `/analytics` (or your configured pathname) to view the analytics dashboard
2. **Automatic Tracking**: Request data is captured automatically for web and API routes

### Quick Commands

[](#quick-commands)

```
# Run the interactive installer
php artisan request-analytics:install

# View available analytics commands
php artisan list | grep analytics

# Clear old analytics data (if pruning is enabled)
php artisan model:prune --model="MeShaon\RequestAnalytics\Models\RequestAnalytics"
```

Configuration Options
---------------------

[](#configuration-options)

### Route Configuration

[](#route-configuration)

- `route.name`: Named route identifier (default: `request.analytics`)
- `route.pathname`: URL path for dashboard access (default: `analytics`)

### Data Capture Settings

[](#data-capture-settings)

- `capture.web`: Track web requests (default: `true`)
- `capture.api`: Track API requests (default: `true`)
- `capture.bots`: Include bot traffic in analytics (default: `false`)

### Queue Processing

[](#queue-processing)

- `queue.enabled`: Process analytics data in background jobs for better performance
- `queue.on_queue`: Specifies the queue name for processing analytics jobs

### Path Filtering

[](#path-filtering)

- `ignore-paths`: Array of paths to exclude from tracking (e.g., admin routes, health checks)

### IP and Referrer Filtering

[](#ip-and-referrer-filtering)

Filter out unwanted traffic to improve data quality and protect privacy by excluding specific IP addresses and referrer domains from analytics tracking.

- `skip_ips`: Array of IP addresses or CIDR blocks to skip tracking. Supports exact IP matches (e.g., `'127.0.0.1'`) and CIDR notation for IP ranges (e.g., `'192.168.1.0/24'`). Useful for excluding internal networks, admin IPs, or development environments.
- `skip_referrers`: Array of referrer domains to exclude from tracking. Filters out spam referrers, bot traffic, or internal tools (e.g., `'spam-site.com'`, `'internal-tool.com'`) to maintain clean analytics data.

### Data Retention

[](#data-retention)

- `pruning.enabled`: Automatic data cleanup (default: `true`)
- `pruning.days`: Days to retain data (default: 90)

### Geolocation Services

[](#geolocation-services)

The package supports multiple geolocation providers:

#### IP-API (Default - Free)

[](#ip-api-default---free)

```
'geolocation' => [
    'enabled' => true,
    'provider' => 'ipapi',
    'api_key' => null, // Not required
]
```

- No API key required
- 45 requests per minute limit
- Includes country, region, city, timezone

#### IPGeolocation

[](#ipgeolocation)

```
'geolocation' => [
    'enabled' => true,
    'provider' => 'ipgeolocation',
    'api_key' => env('REQUEST_ANALYTICS_GEO_API_KEY'),
]
```

- Requires API key from [ipgeolocation.io](https://ipgeolocation.io)
- Higher rate limits and accuracy
- Additional ISP and threat intelligence data

#### MaxMind

[](#maxmind)

```
'geolocation' => [
    'enabled' => true,
    'provider' => 'maxmind',
    'api_key' => env('REQUEST_ANALYTICS_GEO_API_KEY'),
]
```

- Requires GeoIP2 database or web service account
- Highest accuracy and performance
- Enterprise-grade IP intelligence

### Privacy &amp; Compliance

[](#privacy--compliance-1)

```
'privacy' => [
    'anonymize_ip' => env('REQUEST_ANALYTICS_ANONYMIZE_IP', false),
]
```

- **IP Anonymization**: Masks the last octet of IPv4 addresses (192.168.1.xxx)

### Bot Detection

[](#bot-detection)

Advanced bot detection includes:

- **Search Engines**: Google, Bing, Yahoo, DuckDuckGo, Baidu
- **Social Media**: Facebook, Twitter, LinkedIn, Pinterest crawlers
- **SEO Tools**: Ahrefs, SEMrush, Moz, Screaming Frog
- **Monitoring**: Pingdom, UptimeRobot, StatusCake
- **Development**: curl, wget, Postman, Insomnia

Dashboard Access &amp; Usage
----------------------------

[](#dashboard-access--usage)

### Dashboard Access

[](#dashboard-access)

The analytics dashboard is available at `/analytics` by default. Access the dashboard through your configured route after authentication.

### Access Control

[](#access-control)

Implement the `CanAccessAnalyticsDashboard` interface in your User model to control dashboard access:

```
