yenhunter/laravel-website-analytics 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yenhunter/laravel-website-analytics

Composer 安装命令:

composer require yenhunter/laravel-website-analytics

包简介

A lightweight visitor tracking package for Laravel.

README 文档

README

A lightweight, efficient visitor tracking package for Laravel applications. It tracks unique visitors, page views, and user locations (GeoIP) without bloating your database. It includes a dedicated database for Country Coordinates, allowing for instant generation of Vector Maps without hardcoding logic in JavaScript.

🚀 Features

  • Efficient Tracking: Groups visits by IP and Date (prevents spamming DB on every page reload).
  • GeoIP Integration: Automatically detects Country based on IP (via stevebauman/location).
  • Bot Protection: Ignores common bots and crawlers (configurable).
  • Database-Backed Mapping: Includes a pre-filled database of Country Coordinates (Lat/Long) for mapping.
  • Dashboard Ready: Helper methods to get formatted data for ApexCharts and Vector Maps instantly.

Installation

You can install the package via composer:

composer require yenhunter/laravel-website-analytics

1. Run Migrations

Publish and run the migrations to create the website_analytics and analytics_countries tables:

php artisan migrate

2. Configure GeoIP

This package relies on stevebauman/location for country detection. Publish the config:

php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"

3. Seed Country Coordinates (Important)

To populate the map with Latitude/Longitude data for countries, run the included seeder command:

php artisan analytics:seed-countries

This populates the analytics_countries table so your vector map works immediately.

Usage

1. Enable Tracking

Register the Middleware in your bootstrap/app.php file (Laravel 11) or Http/Kernel.php (Laravel 10).

bootstrap/app.php:

use Yenhunter\LaravelWebsiteAnalytics\Http\Middleware\TrackVisitor;

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [
        TrackVisitor::class,
    ]);
})

By default, the middleware tracks routes named visitor.*.

2. Retrieving Data for Dashboard

Inject the Analytics class into any Controller.

namespace App\Http\Controllers;

use Yenhunter\LaravelWebsiteAnalytics\Analytics;

class DashboardController extends Controller
{
    public function index(Analytics $analytics)
    {
        // Get line chart data (Last 7 days)
        $chartData = $analytics->getChartData(7); 

        // Get map data (Returns Country Name, Coords, and Visit Count)
        // Format: [{ name: 'USA', coords: [37.09, -95.71], visits: 500 }, ...]
        $mapData = $analytics->getMapData();

        return view('admin.dashboard', compact('chartData', 'mapData'));
    }
}

Frontend Examples

1. Line Chart (ApexCharts)

The getChartData() method returns arrays perfectly formatted for ApexCharts.

<div id="traffic-chart"></div>

<script>
    var options = {
        chart: { type: 'line', height: 350 },
        series: [{
            name: 'Total Views',
            data: @json($chartData['total'])
        }, {
            name: 'Unique Visitors',
            data: @json($chartData['unique'])
        }],
        xaxis: {
            categories: @json($chartData['dates'])
        }
    };
    var chart = new ApexCharts(document.querySelector("#traffic-chart"), options);
    chart.render();
</script>

2. World Map (jsVectorMap)

The getMapData() method returns an array of objects with coordinates, so you do not need to hardcode Lat/Long in JavaScript.

<div id="world-map-markers" style="height: 350px;"></div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        // 1. Get Data directly from Laravel
        var dbData = @json($mapData);

        new jsVectorMap({
            selector: '#world-map-markers',
            map: 'world',
            markersSelectable: true,
            
            // 2. Pass data directly to markers
            markers: dbData, // [{name: 'BD', coords: [23.68, 90.35], visits: 100}]

            labels: {
                markers: {
                    render: marker => `${marker.name}: ${marker.visits}`
                }
            },
            onMarkerTooltipShow(event, tooltip, index) {
                tooltip.text(`${dbData[index].name}<br>Visits: ${dbData[index].visits}`);
            }
        });
    });
</script>

Database Structure

Table: website_analytics

Tracks individual daily visits per IP.

Column Type Description
id BigInt Primary Key
ip_address String Visitor IP
visit_date Date The date of the visit
visits Integer Count of hits per day
country_code String ISO Code (e.g., US, BD)
user_agent String Browser/Device Info

Table: analytics_countries

Stores static coordinate data for mapping. Populated via analytics:seed-countries.

Column Type Description
id BigInt Primary Key
country_code String Unique ISO Code (e.g., US)
name String Country Name (e.g., United States)
lat Decimal Latitude
long Decimal Longitude

License

The MIT License (MIT). Please see License File for more information.

yenhunter/laravel-website-analytics 适用场景与选型建议

yenhunter/laravel-website-analytics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 yenhunter/laravel-website-analytics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yenhunter/laravel-website-analytics 我们能提供哪些服务?
定制开发 / 二次开发

基于 yenhunter/laravel-website-analytics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04