PHP SDK

PHP server-side SDK with Laravel integration

PHP SDK

The entrolytics/php-client package provides server-side analytics tracking for PHP applications with first-class Laravel support.

Installation

composer require entrolytics/php-client

Quick Start

Install via Composer

composer require entrolytics/php

Create a Client

<?php

use EntrolyticsNG\Client;

$client = new Client('ent_xxx');

Track Events

// Track custom event
$client->track([
    'website_id' => 'your-website-id',
    'event' => 'purchase',
    'data' => [
        'revenue' => 99.99,
        'currency' => 'USD',
        'product' => 'pro-plan'
    ]
]);

// Track page view
$client->pageView([
    'website_id' => 'your-website-id',
    'url' => '/pricing',
    'referrer' => $_SERVER['HTTP_REFERER'] ?? null
]);

// Identify user
$client->identify([
    'website_id' => 'your-website-id',
    'user_id' => 'user_456',
    'traits' => [
        'email' => 'user@example.com',
        'plan' => 'pro'
    ]
]);

Laravel Integration

Publish Configuration

php artisan vendor:publish --tag=entrolytics-config

Configure Environment

Add to .env:

.env
ENTROLYTICS_WEBSITE_ID=your-website-id
ENTROLYTICS_API_KEY=ent_xxx
ENTROLYTICS_HOST=https://entrolytics.dev

Use Facade

use EntrolyticsNG\Laravel\EntrolyticsFacade as Entrolytics;

// Track event
Entrolytics::track([
    'website_id' => config('entrolytics.website_id'),
    'event' => 'signup',
    'data' => ['plan' => 'pro']
]);

// Identify user
Entrolytics::identify([
    'website_id' => config('entrolytics.website_id'),
    'user_id' => (string) auth()->id(),
    'traits' => [
        'email' => auth()->user()->email,
        'plan' => auth()->user()->subscription->plan
    ]
]);

Add Blade Directive

resources/views/layouts/app.blade.php
<!DOCTYPE html>
<html>
<head>
    @entrolytics
</head>
<body>
    {{ $slot }}
</body>
</html>

API Reference

Client Methods

track(array $params): bool

ParameterTypeRequiredDescription
website_idstringYesWebsite ID
eventstringYesEvent name
dataarrayNoEvent data
urlstringNoPage URL
user_idstringNoUser ID
ip_addressstringNoClient IP

pageView(array $params): bool

ParameterTypeRequiredDescription
website_idstringYesWebsite ID
urlstringYesPage URL
referrerstringNoReferrer URL
titlestringNoPage title

identify(array $params): bool

ParameterTypeRequiredDescription
website_idstringYesWebsite ID
user_idstringYesUser ID
traitsarrayNoUser traits

Phase 2: Web Vitals

trackVital(array $params): bool

Track Core Web Vitals metrics.

ParameterTypeRequiredDescription
website_idstringYesWebsite ID
metricstringYesVital type (LCP, INP, CLS, TTFB, FCP)
valuefloatYesMetric value
ratingstringYesRating (good, needs-improvement, poor)
deltafloatNoDifference from previous
idstringNoUnique identifier
navigation_typestringNoNavigation type
attributionarrayNoDebug attribution
urlstringNoPage URL
pathstringNoURL path
session_idstringNoSession ID
$client->trackVital([
    'website_id' => 'abc123',
    'metric' => 'LCP',
    'value' => 2500.0,
    'rating' => 'good',
    'url' => 'https://example.com/page',
    'path' => '/page'
]);

Phase 2: Form Analytics

trackFormEvent(array $params): bool

Track form interaction events.

ParameterTypeRequiredDescription
website_idstringYesWebsite ID
event_typestringYesEvent type (start, field_focus, field_blur, submit, abandon, validation_error)
form_idstringYesForm identifier
url_pathstringYesPage path
form_namestringNoHuman-readable form name
field_namestringNoField name
field_typestringNoInput type
field_indexintNoField position
time_on_fieldintNoms spent on field
time_since_startintNoms since form start
error_messagestringNoValidation error
successboolNoSubmission success
session_idstringNoSession ID
$client->trackFormEvent([
    'website_id' => 'abc123',
    'event_type' => 'submit',
    'form_id' => 'contact-form',
    'url_path' => '/contact',
    'form_name' => 'Contact Form',
    'success' => true,
    'time_since_start' => 45000
]);

Phase 2: Deployment Tracking

setDeployment(array $params): bool

Register deployment context.

ParameterTypeRequiredDescription
website_idstringYesWebsite ID
deploy_idstringYesDeployment ID
git_shastringNoGit commit SHA
git_branchstringNoGit branch
deploy_urlstringNoDeployment URL
sourcestringNoPlatform (vercel, netlify, etc.)
$client->setDeployment([
    'website_id' => 'abc123',
    'deploy_id' => 'deploy_456',
    'git_sha' => 'abc1234567890',
    'git_branch' => 'main',
    'source' => 'vercel'
]);

Advanced Usage

Requirements

  • PHP >= 8.1
  • guzzlehttp/guzzle >= 7.0

Optional

  • Laravel >= 10.0 | ^11.0 (for Laravel integration)

Features

  • ✅ Simple, fluent API
  • ✅ Laravel service provider and facade
  • ✅ Blade directive for script injection
  • ✅ Middleware for automatic page tracking
  • ✅ Dependency injection support
  • ✅ Comprehensive error handling
  • ✅ Phase 2: Web Vitals tracking
  • ✅ Phase 2: Form Analytics
  • ✅ Phase 2: Deployment tracking
  • ✅ Self-hosted instance support

Support