### Install laravel-amplitude via Composer Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Install the package via Composer. Requires PHP 8.2+, Laravel 12+, and spatie/laravel-settings configured with the settings table. ```bash composer require jeffersongoncalves/laravel-amplitude ``` -------------------------------- ### Publish and run Amplitude settings migration Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Publish and run the Amplitude settings migration to create the necessary database table. ```bash php artisan vendor:publish --tag=amplitude-settings-migrations php artisan migrate ``` -------------------------------- ### Publish spatie/laravel-settings migration Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Publish the spatie/laravel-settings migration to create the settings table if not already done. ```bash php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" ``` -------------------------------- ### Package directory structure Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Shows the package directory structure, including the service provider, settings class, migration, and Blade view. ```text src/ AmplitudeServiceProvider.php # Package service provider Settings/ AmplitudeSettings.php # Spatie Settings class (group: amplitude) database/ settings/ 2026_01_01_000000_create_amplitude_settings.php # Settings migration resources/ views/ script.blade.php # Tracking script Blade view ``` -------------------------------- ### Convert settings to JS config array with toJsConfig() Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Converts Amplitude settings into an array for `amplitude.init()`. It conditionally sets `serverUrl` or `serverZone`, builds `cookieOptions`, and includes `identityStorage`, `autocapture`, `optOut`, `sessionTimeout`, `minIdLength`, `flushQueueSize`, and `flushIntervalMillis`. `debug`/`logLevel` are intentionally omitted here. ```php public function toJsConfig(): array { $config = []; if ($this->server_url) { $config['serverUrl'] = $this->server_url; } else { $config['serverZone'] = $this->server_zone; } $cookieOptions = []; if ($this->cookie_domain) { $cookieOptions['domain'] = $this->cookie_domain; } $cookieOptions['expiration'] = $this->cookie_expiration; $cookieOptions['secure'] = $this->secure_cookie; $config['cookieOptions'] = $cookieOptions; $config['identityStorage'] = $this->identity_storage; $config['autocapture'] = $this->autocapture; $config['optOut'] = $this->opt_out; $config['sessionTimeout'] = $this->session_timeout_minutes * 60000; if ($this->min_id_length !== null) { $config['minIdLength'] = $this->min_id_length; } $config['flushQueueSize'] = $this->flush_queue_size; $config['flushIntervalMillis'] = $this->flush_interval_millis; return $config; } ``` -------------------------------- ### Run tests with composer test Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Run the package test suite. ```bash composer test ``` -------------------------------- ### AmplitudeServiceProvider class Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md The package service provider extends Spatie\LaravelPackageTools\PackageServiceProvider. It registers the package name and views, auto-registers AmplitudeSettings into the settings config, and publishes the settings migration under the 'amplitude-settings-migrations' tag. ```php class AmplitudeServiceProvider extends PackageServiceProvider { public function configure(Package $package): void { $package->name('laravel-amplitude') ->hasViews(); } public function packageRegistered(): void { parent::packageRegistered(); Config::set('settings.settings', array_merge( Config::get('settings.settings', []), [AmplitudeSettings::class] )); } public function packageBooted(): void { parent::packageBooted(); Config::set('settings.migrations_paths', array_merge( [__DIR__.'/../database/settings'], Config::get('settings.migrations_paths', []) )); $this->publishes([ __DIR__.'/../database/settings' => database_path('settings'), ], 'amplitude-settings-migrations'); } } ``` -------------------------------- ### Set Amplitude API key via settings Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Configure the Amplitude API key via the database settings. The api_key property is required for tracking. ```php use JeffersonGoncalves\Amplitude\Settings\AmplitudeSettings; $settings = app(AmplitudeSettings::class); $settings->api_key = 'YOUR_AMPLITUDE_API_KEY'; $settings->save(); ``` -------------------------------- ### Set data residency to EU Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Sets the server_zone to 'EU' and saves the settings. This configures Amplitude to use EU data residency. ```php $settings = app(AmplitudeSettings::class); $settings->server_zone = 'EU'; $settings->save(); ``` -------------------------------- ### Set EU data residency with server_zone Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Set the server_zone to 'EU' for EU data residency. The default is 'US'. ```php // EU Data Residency $settings->server_zone = 'EU'; $settings->save(); ``` -------------------------------- ### Run tests, coverage, analysis, and formatting with Composer Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Use these Composer commands to run the test suite, generate coverage, run static analysis, and format code. The package uses Pest with pestphp/pest-plugin-laravel and orchestra/testbench. ```bash # Run tests composer test # Run tests with coverage composer test-coverage # Run static analysis composer analyse # Run code formatting composer format ``` -------------------------------- ### Set custom_lib_url for proxy loader script Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Load the Amplitude loader script from a self-hosted or proxy URL instead of the default CDN. ```php $settings->custom_lib_url = 'https://proxy.yourdomain.com/amplitude.js'; $settings->save(); ``` -------------------------------- ### Settings migration creating 15 amplitude settings Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Migration that adds the default settings in the 'amplitude' group. Each setting has a default value; modify this migration to change initial configuration. ```php return new class extends SettingsMigration { public function up(): void { $this->migrator->add('amplitude.api_key', null); $this->migrator->add('amplitude.server_zone', 'US'); $this->migrator->add('amplitude.server_url', null); $this->migrator->add('amplitude.custom_lib_url', null); $this->migrator->add('amplitude.autocapture', true); $this->migrator->add('amplitude.identity_storage', 'cookie'); $this->migrator->add('amplitude.cookie_domain', null); $this->migrator->add('amplitude.cookie_expiration', 365); $this->migrator->add('amplitude.secure_cookie', false); $this->migrator->add('amplitude.session_timeout_minutes', 30); $this->migrator->add('amplitude.min_id_length', null); $this->migrator->add('amplitude.opt_out', false); $this->migrator->add('amplitude.debug', false); $this->migrator->add('amplitude.flush_queue_size', 30); $this->migrator->add('amplitude.flush_interval_millis', 1000); } }; ``` -------------------------------- ### Test toJsConfig() behavior with Pest Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Pest tests for the toJsConfig() method. The first verifies that setting server_url results in a config with 'serverUrl' and without 'serverZone'; the second confirms session_timeout_minutes is converted to milliseconds (45 minutes becomes 2700000). ```php it('uses server_url instead of server_zone when set', function () { $settings = app(AmplitudeSettings::class); $settings->server_url = 'https://proxy.example.com'; $config = $settings->toJsConfig(); expect($config)->toHaveKey('serverUrl') ->and($config)->not->toHaveKey('serverZone'); }); it('converts session_timeout_minutes to milliseconds', function () { $settings = app(AmplitudeSettings::class); $settings->session_timeout_minutes = 45; expect($settings->toJsConfig()['sessionTimeout'])->toBe(2700000); }); ``` -------------------------------- ### Test tracking script rendering with Pest Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Pest tests for the Amplitude tracking script view. The first test sets an api_key and asserts the rendered view contains 'amplitude.init' and the key; the second sets api_key to null and asserts the script is not rendered. ```php use JeffersonGoncalves\Amplitude\Settings\AmplitudeSettings; it('renders the tracking script when api_key is set', function () { $settings = app(AmplitudeSettings::class); $settings->api_key = 'test-key-123'; $settings->save(); $view = view('amplitude::script')->render(); expect($view)->toContain('amplitude.init') ->toContain('test-key-123'); }); it('does not render the script when api_key is null', function () { $settings = app(AmplitudeSettings::class); $settings->api_key = null; $settings->save(); $view = view('amplitude::script')->render(); expect($view)->not->toContain('amplitude.init'); }); ``` -------------------------------- ### Read Amplitude settings Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Retrieves the AmplitudeSettings instance and echoes the api_key, debug, and session_timeout_minutes properties. Useful for inspecting current configuration values. ```php use JeffersonGoncalves\Amplitude\Settings\AmplitudeSettings; $settings = app(AmplitudeSettings::class); echo $settings->api_key; // e.g., "abc123def456" echo $settings->debug; // false echo $settings->session_timeout_minutes; // 30 ``` -------------------------------- ### Update Amplitude settings Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Updates the api_key, server_zone, and debug properties on the AmplitudeSettings instance and saves them. Use this to change configuration at runtime. ```php $settings = app(AmplitudeSettings::class); $settings->api_key = 'your-amplitude-api-key'; $settings->server_zone = 'EU'; $settings->debug = true; $settings->save(); ``` -------------------------------- ### Set custom ingestion endpoint and loader URL Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Sets custom server_url and custom_lib_url properties and saves them. Use this to route Amplitude traffic through a proxy or custom endpoint. ```php $settings = app(AmplitudeSettings::class); $settings->server_url = 'https://proxy.example.com'; $settings->custom_lib_url = 'https://proxy.example.com/amplitude.js'; $settings->save(); ``` -------------------------------- ### AmplitudeSettings class with group 'amplitude' Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md The AmplitudeSettings class uses spatie/laravel-settings with group 'amplitude'. It defines all configurable properties with their types and defaults, and includes a toJsConfig method (implementation omitted). ```php use Spatie\LaravelSettings\Settings; class AmplitudeSettings extends Settings { public ?string $api_key; public string $server_zone; public ?string $server_url; public ?string $custom_lib_url; public bool $autocapture; public string $identity_storage; public ?string $cookie_domain; public int $cookie_expiration; public bool $secure_cookie; public int $session_timeout_minutes; public ?int $min_id_length; public bool $opt_out; public bool $debug; public int $flush_queue_size; public int $flush_interval_millis; public static function group(): string { return 'amplitude'; } public function toJsConfig(): array { /* ... */ } } ``` -------------------------------- ### Render Amplitude tracking scripts in script.blade.php Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Blade view that renders the Amplitude tracking scripts only when `api_key` is not empty. Loads the loader script from `custom_lib_url` or the default CDN URL, sets `logLevel` via the real `window.amplitude.Types.LogLevel` enum, and uses `json_encode` with `JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT` plus `Illuminate\Support\Js::from()` to prevent script injection from a malicious `api_key`. ```blade @php use Illuminate\Support\Js; $settings = app(\JeffersonGoncalves\Amplitude\Settings\AmplitudeSettings::class); @endphp @if(!empty($settings->api_key)) @endif ``` -------------------------------- ### Include amplitude tracking script in Blade layout Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Include the tracking script in your layout's head section. The Blade view is provided by the package. ```blade @include('amplitude::script') ``` -------------------------------- ### Set custom server_url for ingestion Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Route ingestion through a custom endpoint by setting server_url. This takes precedence over server_zone. ```php $settings->server_url = 'https://proxy.yourdomain.com'; $settings->save(); ``` -------------------------------- ### Test AmplitudeSettings defaults and group with Pest Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/resources/boost/skills/amplitude-development/SKILL.md Pest tests for AmplitudeSettings defaults and group. The first asserts default values for api_key, server_zone, debug, autocapture, identity_storage, cookie_expiration, and session_timeout_minutes; the second checks the settings group is 'amplitude'. ```php it('has the correct default values', function () { $settings = app(AmplitudeSettings::class); expect($settings->api_key)->toBeNull(); expect($settings->server_zone)->toBe('US'); expect($settings->debug)->toBeFalse(); expect($settings->autocapture)->toBeTrue(); expect($settings->identity_storage)->toBe('cookie'); expect($settings->cookie_expiration)->toBe(365); expect($settings->session_timeout_minutes)->toBe(30); }); it('belongs to the amplitude group', function () { expect(AmplitudeSettings::group())->toBe('amplitude'); }); ``` -------------------------------- ### Include Amplitude script in Blade layout Source: https://github.com/jeffersongoncalves/laravel-amplitude/blob/master/README.md Add the Amplitude script to your Blade layout, typically before the closing tag. ```blade @include('amplitude::script') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.