Skip to content

Commit fcc9360

Browse files
feat: migrate configuration to spatie/laravel-settings
BREAKING CHANGE: Replace config/umami.php with database-backed settings via spatie/laravel-settings. - Remove config/umami.php and all env() reads - Add UmamiSettings class extending Spatie\LaravelSettings\Settings - Add settings migration with default values - Update UmamiServiceProvider to register settings class and migration paths - Update Blade view to resolve settings from container - Add spatie/laravel-settings ^3.3 as dependency - Update README with new installation and usage instructions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3b24cc0 commit fcc9360

7 files changed

Lines changed: 131 additions & 36 deletions

File tree

README.md

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,66 @@
1212

1313
This Laravel package seamlessly integrates Umami analytics into your Blade templates. Easily track website visits and user engagement directly within your Laravel application, providing valuable insights into your website's performance. This package simplifies the integration process, saving you time and effort. With minimal configuration, you can leverage Umami's powerful analytics features to gain a clearer understanding of your audience and website usage.
1414

15+
## Requirements
16+
17+
- PHP 8.2+
18+
- Laravel 11+
19+
- [spatie/laravel-settings](https://github.com/spatie/laravel-settings) configured (the `settings` table must exist)
20+
1521
## Installation
1622

17-
You can install the package via composer:
23+
Install the package via composer:
1824

1925
```bash
2026
composer require jeffersongoncalves/laravel-umami
2127
```
2228

23-
## Usage
29+
Run the migrations to create the Umami settings in your database:
2430

25-
Publish config file.
31+
```bash
32+
php artisan migrate
33+
```
34+
35+
Optionally, you can publish the settings migration to customize defaults:
2636

2737
```bash
28-
php artisan vendor:publish --tag=umami-config
38+
php artisan vendor:publish --tag=umami-settings-migrations
2939
```
3040

31-
Add head template.
41+
## Usage
42+
43+
Add the Umami script to your Blade layout (typically in the `<head>` section):
3244

33-
```php
45+
```blade
3446
@include('umami::script')
3547
```
3648

49+
### Configuring Settings
50+
51+
Settings are stored in the database and can be managed via code:
52+
53+
```php
54+
use JeffersonGoncalves\Umami\Settings\UmamiSettings;
55+
56+
$settings = app(UmamiSettings::class);
57+
$settings->website_id = 'your-website-id';
58+
$settings->host_analytics = 'https://your-umami-instance.com';
59+
$settings->save();
60+
```
61+
62+
### Available Settings
63+
64+
| Property | Type | Default | Description |
65+
|----------|------|---------|-------------|
66+
| `website_id` | `?string` | `null` | Your Umami website ID (required for tracking) |
67+
| `host_analytics` | `string` | `https://cloud.umami.is` | URL of your Umami instance |
68+
| `host_url` | `?string` | `null` | Override data destination URL |
69+
| `auto_track` | `bool` | `true` | Automatically track pageviews and events |
70+
| `domains` | `?string` | `null` | Comma-delimited list of allowed domains |
71+
| `tag` | `?string` | `null` | Tag to group events in the dashboard |
72+
| `exclude_search` | `bool` | `false` | Exclude search parameters from URL |
73+
| `exclude_hash` | `bool` | `false` | Exclude hash value from URL |
74+
3775
## Testing
3876

3977
```bash
@@ -54,7 +92,7 @@ Please review [our security policy](../../security/policy) on how to report secu
5492

5593
## Credits
5694

57-
- [Jèfferson Gonçalves](https://github.com/jeffersongoncalves)
95+
- [Jefferson Goncalves](https://github.com/jeffersongoncalves)
5896
- [All Contributors](../../contributors)
5997

6098
## License

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": "^8.2|^8.3",
2020
"laravel/framework": "^11.0|^12.0",
21-
"spatie/laravel-package-tools": "^1.14.0"
21+
"spatie/laravel-package-tools": "^1.14.0",
22+
"spatie/laravel-settings": "^3.3"
2223
},
2324
"require-dev": {
2425
"larastan/larastan": "^3.0",

config/umami.php

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Spatie\LaravelSettings\Migrations\SettingsMigration;
4+
5+
return new class extends SettingsMigration
6+
{
7+
public function up(): void
8+
{
9+
$this->migrator->add('umami.website_id', null);
10+
$this->migrator->add('umami.host_analytics', 'https://cloud.umami.is');
11+
$this->migrator->add('umami.host_url', null);
12+
$this->migrator->add('umami.auto_track', true);
13+
$this->migrator->add('umami.domains', null);
14+
$this->migrator->add('umami.tag', null);
15+
$this->migrator->add('umami.exclude_search', false);
16+
$this->migrator->add('umami.exclude_hash', false);
17+
}
18+
};

resources/views/script.blade.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
@if(!empty(config('umami.website_id')))
2-
<script async defer data-website-id="{{ config('umami.website_id') }}"
3-
src="{{ config('umami.host_analytics') }}/script.js"
4-
@if(config('umami.host_url')) data-host-url="{{ config('umami.host_url') }}" @endif
5-
@if(config('umami.domains')) data-domains="{{ config('umami.domains') }}" @endif
6-
@if(config('umami.tag')) data-tag="{{ config('umami.tag') }}" @endif
7-
data-auto-track="{{ config('umami.auto_track') ? 'true' : 'false' }}"
8-
data-exclude-search="{{ config('umami.exclude_search') ? 'true' : 'false' }}"
9-
data-exclude-hash="{{ config('umami.exclude_hash') ? 'true' : 'false' }}">
1+
@php($settings = app(\JeffersonGoncalves\Umami\Settings\UmamiSettings::class))
2+
3+
@if(!empty($settings->website_id))
4+
<script async defer data-website-id="{{ $settings->website_id }}"
5+
src="{{ $settings->host_analytics }}/script.js"
6+
@if($settings->host_url) data-host-url="{{ $settings->host_url }}" @endif
7+
@if($settings->domains) data-domains="{{ $settings->domains }}" @endif
8+
@if($settings->tag) data-tag="{{ $settings->tag }}" @endif
9+
data-auto-track="{{ $settings->auto_track ? 'true' : 'false' }}"
10+
data-exclude-search="{{ $settings->exclude_search ? 'true' : 'false' }}"
11+
data-exclude-hash="{{ $settings->exclude_hash ? 'true' : 'false' }}">
1012
</script>
1113
@endif

src/Settings/UmamiSettings.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace JeffersonGoncalves\Umami\Settings;
4+
5+
use Spatie\LaravelSettings\Settings;
6+
7+
class UmamiSettings extends Settings
8+
{
9+
public ?string $website_id;
10+
11+
public string $host_analytics;
12+
13+
public ?string $host_url;
14+
15+
public bool $auto_track;
16+
17+
public ?string $domains;
18+
19+
public ?string $tag;
20+
21+
public bool $exclude_search;
22+
23+
public bool $exclude_hash;
24+
25+
public static function group(): string
26+
{
27+
return 'umami';
28+
}
29+
}

src/UmamiServiceProvider.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace JeffersonGoncalves\Umami;
44

5+
use Illuminate\Support\Facades\Config;
6+
use JeffersonGoncalves\Umami\Settings\UmamiSettings;
57
use Spatie\LaravelPackageTools\Package;
68
use Spatie\LaravelPackageTools\PackageServiceProvider;
79

@@ -10,7 +12,30 @@ class UmamiServiceProvider extends PackageServiceProvider
1012
public function configurePackage(Package $package): void
1113
{
1214
$package->name('laravel-umami')
13-
->hasConfigFile('umami')
1415
->hasViews();
1516
}
17+
18+
public function packageRegistered(): void
19+
{
20+
parent::packageRegistered();
21+
22+
Config::set('settings.settings', array_merge(
23+
Config::get('settings.settings', []),
24+
[UmamiSettings::class]
25+
));
26+
}
27+
28+
public function packageBooted(): void
29+
{
30+
parent::packageBooted();
31+
32+
Config::set('settings.migrations_paths', array_merge(
33+
Config::get('settings.migrations_paths', []),
34+
[__DIR__.'/../database/settings']
35+
));
36+
37+
$this->publishes([
38+
__DIR__.'/../database/settings' => database_path('settings'),
39+
], 'umami-settings-migrations');
40+
}
1641
}

0 commit comments

Comments
 (0)