sakshsky/analytics

A PHP package to track visitor information such as IP, browser, OS, and more, storing data in a JSON file.

dev-main 2025-07-31 03:06 UTC

This package is not auto-updated.

Last update: 2025-08-01 01:27:11 UTC


README

A PHP package to track visitor information such as IP address, browser, OS, user agent, referrer, and more, storing the data in a JSON file. This is useful for basic analytics in web applications.

Requirements

  • PHP >= 7.4

Installation

You can install this package via Composer. If published on Packagist, run:

composer require sakshsky/analytics

For local development or if not published, add a path repository in your project's composer.json:

"repositories": [
    {
        "type": "path",
        "url": "/path/to/analytics"
    }
]

Then require it:

composer require sakshsky/analytics:*@dev

Usage

Basic Example

<?php

require 'vendor/autoload.php';

use Sakshsky\Analytics\Analytics;

// Specify the path to the JSON log file (ensure it's writable)
$tracker = new Analytics('visitor_log.json');
$tracker->track();

echo "Visitor tracked successfully!";

In Laravel

In a controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Sakshsky\Analytics\Analytics;

class ExampleController extends Controller
{
    public function index(Request $request)
    {
        $tracker = new Analytics(storage_path('logs/visitor_log.json'));
        $tracker->track();

        return 'Visitor tracked!';
    }
}
  • Ensure the directory (e.g., storage/logs) has write permissions.

Tracked Data

The package collects and stores the following in JSON format:

  • Request time
  • IP address (handling proxies)
  • User agent
  • Browser (basic detection)
  • OS (basic detection)
  • Referrer
  • Request method
  • Requested URL
  • Query string
  • HTTP protocol
  • Server name
  • Remote port

Notes

  • Concurrency: Uses file locking (flock) to handle multiple writes safely.
  • Privacy: Comply with laws like GDPR when tracking users. IP addresses are personal data.
  • Improvements: For advanced user agent parsing, consider adding dependencies like whichbrowser/parser. For high traffic, use a database instead of a file.
  • Limitations: Browser/OS detection is basic and regex-based; not foolproof.

License

MIT License. See LICENSE for details (add a LICENSE file if needed).

Contributing

Pull requests are welcome on GitHub.

Author: susheel kumar (package@sakshsky.com)