Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa4784c8ec | ||
|
|
cd00572702 | ||
|
|
8c042b27cf | ||
|
|
4aa046d7a7 | ||
|
|
859dcf99be | ||
|
|
2893ed0bc6 | ||
|
|
15e9fcc186 | ||
|
|
42404cc2cc | ||
|
|
b53047a28c | ||
|
|
5eff0cafe5 | ||
|
|
0e25ead520 | ||
|
|
67507d036a | ||
|
|
0ce7b51eff | ||
|
|
891c3d8ff8 | ||
|
|
e2062499ae | ||
|
|
1f4bbadab7 | ||
|
|
9c2ebc5db8 | ||
|
|
32721b82cc | ||
|
|
6df45258a6 | ||
|
|
6a358d9d65 | ||
|
|
6417d3bd6e | ||
|
|
3b6afc54f9 | ||
|
|
a2c3ad76c0 | ||
|
|
42debe755e | ||
|
|
9788f6f895 | ||
|
|
395b15b80b | ||
|
|
164c3ab5dd | ||
|
|
26454a9501 | ||
|
|
c3033e1093 | ||
|
|
f622f3152b |
No files matched your search
@@ -14,7 +14,7 @@ jobs:
|
||||
|
||||
- name: Dependabot metadata
|
||||
id: metadata
|
||||
uses: dependabot/fetch-metadata@v2.3.0
|
||||
uses: dependabot/fetch-metadata@v2.4.0
|
||||
with:
|
||||
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ jobs:
|
||||
ref: ${{ github.head_ref }}
|
||||
|
||||
- name: Fix PHP code style issues
|
||||
uses: aglipanci/laravel-pint-action@2.4
|
||||
uses: aglipanci/laravel-pint-action@2.5
|
||||
|
||||
- name: Commit changes
|
||||
uses: stefanzweifel/git-auto-commit-action@v5
|
||||
|
||||
@@ -16,14 +16,27 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: true
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest]
|
||||
php: [8.4]
|
||||
laravel: [11.*]
|
||||
os: [ubuntu-latest]
|
||||
php: [8.3, 8.4]
|
||||
laravel: [11.*, 12.*]
|
||||
stability: [prefer-lowest, prefer-stable]
|
||||
include:
|
||||
- laravel: 11.*
|
||||
php: 8.3
|
||||
testbench: 9.*
|
||||
carbon: ^2.63
|
||||
- laravel: 11.*
|
||||
php: 8.4
|
||||
testbench: 9.*
|
||||
carbon: ^2.63
|
||||
- laravel: 12.*
|
||||
php: 8.3
|
||||
testbench: 10.*
|
||||
carbon: ^3.8.4
|
||||
- laravel: 12.*
|
||||
php: 8.4
|
||||
testbench: 10.*
|
||||
carbon: ^3.8.4
|
||||
|
||||
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
|
||||
|
||||
@@ -43,9 +56,18 @@ jobs:
|
||||
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
|
||||
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
|
||||
|
||||
- name: Remove Larastan if Laravel 12
|
||||
if: startsWith(matrix.laravel, '12.')
|
||||
run: composer remove larastan/larastan --dev --no-update || true
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.os == 'windows-latest' && '^^^' || '' }}${{ matrix.carbon }}" --no-interaction --no-update
|
||||
composer require \
|
||||
"laravel/framework:${{ matrix.laravel }}" \
|
||||
"orchestra/testbench:${{ matrix.testbench }}" \
|
||||
"nesbot/carbon:${{ matrix.carbon }}" \
|
||||
--no-interaction --no-update
|
||||
|
||||
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
|
||||
|
||||
- name: List Installed Dependencies
|
||||
|
||||
@@ -30,3 +30,7 @@ phpstan.neon
|
||||
testbench.yaml
|
||||
/docs
|
||||
/coverage
|
||||
|
||||
# Ignore Mac DS_Store files
|
||||
.DS_Store
|
||||
**/.DS_Store
|
||||
@@ -1,3 +1,45 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `laravel-plausible-proxy` will be documented in this file.
|
||||
|
||||
## v1.4 - 2025-06-16
|
||||
|
||||
### What's Changed
|
||||
|
||||
* Bump aglipanci/laravel-pint-action from 2.4 to 2.5 by @dependabot in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/3
|
||||
* Bump dependabot/fetch-metadata from 2.3.0 to 2.4.0 by @dependabot in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/4
|
||||
* Added support for L12 by @mathiaspalmqvist in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/7
|
||||
|
||||
### New Contributors
|
||||
|
||||
* @dependabot made their first contribution in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/3
|
||||
|
||||
**Full Changelog**: https://github.com/MattitjaAB/laravel-plausible-proxy/compare/v1.3...v1.4
|
||||
|
||||
## v1.3 - 2025-01-31
|
||||
|
||||
### What's Changed
|
||||
|
||||
* Remove site by @mathiaspalmqvist in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/2
|
||||
|
||||
**Full Changelog**: https://github.com/MattitjaAB/laravel-plausible-proxy/compare/v1.2...v1.3
|
||||
|
||||
## v1.2 - 2025-01-31
|
||||
|
||||
**Full Changelog**: https://github.com/MattitjaAB/laravel-plausible-proxy/compare/v1.1...v1.2
|
||||
|
||||
## v1.1 - 2025-01-31
|
||||
|
||||
### What's Changed
|
||||
|
||||
* Refactor event forwarding and improve test coverage by @mathiaspalmqvist in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/1
|
||||
|
||||
### New Contributors
|
||||
|
||||
* @mathiaspalmqvist made their first contribution in https://github.com/MattitjaAB/laravel-plausible-proxy/pull/1
|
||||
|
||||
**Full Changelog**: https://github.com/MattitjaAB/laravel-plausible-proxy/compare/v1.0...v1.1
|
||||
|
||||
## First release - 2025-01-30
|
||||
|
||||
**Full Changelog**: https://github.com/MattitjaAB/laravel-plausible-proxy/commits/v1.0
|
||||
@@ -5,11 +5,11 @@
|
||||
[](https://github.com/mattitjaab/laravel-plausible-proxy/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
|
||||
[](https://packagist.org/packages/mattitjaab/laravel-plausible-proxy)
|
||||
|
||||
A Laravel package for proxying Plausible Analytics, improving performance and privacy.
|
||||
A Laravel package for proxying Plausible Analytics, improving performance.
|
||||
|
||||
## Requirements
|
||||
- Laravel 11
|
||||
- PHP 8.4+
|
||||
- Laravel 12
|
||||
- PHP 8.3, 8.4
|
||||
|
||||
## 🚀 Installation
|
||||
|
||||
@@ -26,27 +26,15 @@ A Laravel package for proxying Plausible Analytics, improving performance and pr
|
||||
3. Add the following environment variables to your `.env` file:
|
||||
```env
|
||||
PLAUSIBLE_DOMAIN=https://plausible.io
|
||||
PLAUSIBLE_SITE=example.com
|
||||
```
|
||||
|
||||
4. **(Optional)** If you want to use Laravel Queues to send events in the background, ensure your queue driver is set up correctly:
|
||||
```env
|
||||
QUEUE_CONNECTION=database
|
||||
```
|
||||
Then run migrations to create the necessary queue tables:
|
||||
```sh
|
||||
php artisan queue:table
|
||||
php artisan migrate
|
||||
```
|
||||
|
||||
## ⚙️ Configuration
|
||||
|
||||
The `config/plausible-proxy.php` file allows you to customize the package settings:
|
||||
The `config/plausible-proxy.php` file allows you to customize the plausible domain:
|
||||
|
||||
```php
|
||||
return [
|
||||
'domain' => env('PLAUSIBLE_DOMAIN', 'https://plausible.io'),
|
||||
'site' => env('PLAUSIBLE_SITE', 'example.com'),
|
||||
];
|
||||
```
|
||||
|
||||
@@ -63,21 +51,13 @@ After installation, you can include the script like this:
|
||||
The route `/js/script.js` is automatically handled and caches the script for **6 hours**.
|
||||
|
||||
### **2. Sending Events to Plausible**
|
||||
Instead of sending events directly to `plausible.io`, you can now send them to `/api/event`, and they will be queued and processed asynchronously.
|
||||
|
||||
Example request:
|
||||
```sh
|
||||
curl -X POST "https://example.com/api/event" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"n":"pageview","u":"https://example.com/page","d":"example.com"}'
|
||||
```
|
||||
Instead of sending events directly to plausible.io, the script is loaded from /js/script.js and proxies the request to Plausible via /api/event, helping to prevent blocking by ad blockers.
|
||||
|
||||
## ✅ Features
|
||||
|
||||
- 🚀 **Fast & Optimized**: Caches the Plausible script to reduce external calls.
|
||||
- 🔒 **Privacy-Friendly**: Prevents direct calls to `plausible.io`.
|
||||
- 📊 **Improved Performance**: Events are processed via Laravel Queues to avoid blocking user interactions.
|
||||
- 🛠 **Fully Configurable**: Easily set your own Plausible instance and domain via `.env`.
|
||||
- 🚀 **Fast & Optimized**: Caches the Plausible script to reduce external calls.
|
||||
- ✅ No issues with ad blockers: Proxies the request via a local API endpoint.
|
||||
- 🛠 **Fully Configurable**: Easily set your own Plausible instance via `.env`.
|
||||
|
||||
## 🛠 Testing
|
||||
|
||||
|
||||
+2
-2
@@ -18,9 +18,9 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^8.4",
|
||||
"php": "^8.3",
|
||||
"spatie/laravel-package-tools": "^1.16",
|
||||
"illuminate/contracts": "^10.0||^11.0"
|
||||
"illuminate/contracts": "^10.0||^11.0||^12.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"laravel/pint": "^1.14",
|
||||
|
||||
@@ -4,5 +4,4 @@
|
||||
|
||||
return [
|
||||
'domain' => env('PLAUSIBLE_DOMAIN', 'https://plausible.io'),
|
||||
'site' => env('PLAUSIBLE_SITE', 'example.com'),
|
||||
];
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MattitjaAB\LaravelPlausibleProxy\Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/*
|
||||
class ModelFactory extends Factory
|
||||
{
|
||||
protected $model = YourModel::class;
|
||||
|
||||
public function definition()
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
Schema::create('laravel_plausible_proxy_table', function (Blueprint $table) {
|
||||
$table->id();
|
||||
|
||||
// add fields
|
||||
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
};
|
||||
+1
-2
@@ -6,7 +6,6 @@ parameters:
|
||||
paths:
|
||||
- src
|
||||
- config
|
||||
- database
|
||||
tmpDir: build/phpstan
|
||||
checkOctaneCompatibility: true
|
||||
checkModelProperties: true
|
||||
checkModelProperties: true
|
||||
Whitespace-only changes.
+2
-22
@@ -1,26 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Concurrency;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use MattitjaAB\LaravelPlausibleProxy\Http\Controllers\PlausibleEventApiController;
|
||||
|
||||
Route::post('api/event', function (Request $request) {
|
||||
$validator = Validator::make($request->all(), [
|
||||
'n' => 'required|string',
|
||||
'u' => 'required|url',
|
||||
'd' => 'required|string',
|
||||
'r' => 'nullable|url',
|
||||
'm' => 'nullable|string',
|
||||
'p' => 'nullable|array',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
|
||||
}
|
||||
|
||||
Concurrency::defer(fn () => Http::post(config('plausible-proxy.domain').'/api/event', $request->all()));
|
||||
|
||||
return response()->json(['status' => 'queued']);
|
||||
});
|
||||
Route::post('api/event', PlausibleEventApiController::class);
|
||||
+2
-30
@@ -1,34 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use MattitjaAB\LaravelPlausibleProxy\Http\Controllers\PlausibleScriptProxyController;
|
||||
|
||||
Route::get('/js/script.js', function () {
|
||||
$plausibleDomain = config('plausible-proxy.domain');
|
||||
|
||||
if (Cache::has('plausible_script_js')) {
|
||||
return response(Cache::get('plausible_script_js'))
|
||||
->header('Content-Type', 'application/javascript')
|
||||
->header('Cache-Control', 'public, max-age=86400');
|
||||
}
|
||||
|
||||
$response = Http::timeout(5)->get("{$plausibleDomain}/js/script.js");
|
||||
|
||||
if ($response->successful()) {
|
||||
$script = $response->body();
|
||||
Cache::put('plausible_script_js', $script, now()->addHours(6));
|
||||
|
||||
return response($script)
|
||||
->header('Content-Type', 'application/javascript')
|
||||
->header('Cache-Control', 'public, max-age=86400');
|
||||
}
|
||||
|
||||
return response('Error fetching Plausible script', 500);
|
||||
});
|
||||
|
||||
Route::get('/api/routes', function () {
|
||||
return response()->json(
|
||||
collect(Route::getRoutes())->map(fn ($route) => $route->uri())
|
||||
);
|
||||
});
|
||||
Route::get('/js/script.js', PlausibleScriptProxyController::class);
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MattitjaAB\LaravelPlausibleProxy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class LaravelPlausibleProxyCommand extends Command
|
||||
{
|
||||
public $signature = 'laravel-plausible-proxy';
|
||||
|
||||
public $description = 'My command';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$this->comment('All done');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MattitjaAB\LaravelPlausibleProxy\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
|
||||
/**
|
||||
* @see \MattitjaAB\LaravelPlausibleProxy\LaravelPlausibleProxy
|
||||
*/
|
||||
class LaravelPlausibleProxy extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return \MattitjaAB\LaravelPlausibleProxy\LaravelPlausibleProxy::class;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace MattitjaAB\LaravelPlausibleProxy\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use MattitjaAB\LaravelPlausibleProxy\Http\Requests\PlausibleEventRequest;
|
||||
|
||||
class PlausibleEventApiController
|
||||
{
|
||||
public function __invoke(PlausibleEventRequest $request): JsonResponse
|
||||
{
|
||||
$headers = collect($request->headers->all())
|
||||
->mapWithKeys(fn ($value, $key) => [$key => implode(', ', $value)])
|
||||
->all();
|
||||
|
||||
Http::withHeaders($headers)
|
||||
->post(config('plausible-proxy.domain').'/api/event', $request->validated());
|
||||
|
||||
return response()->json(['status' => 'queued']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace MattitjaAB\LaravelPlausibleProxy\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PlausibleScriptProxyController
|
||||
{
|
||||
public function __invoke(): Response
|
||||
{
|
||||
try {
|
||||
$script = Cache::remember(
|
||||
key: 'plausible_script_js',
|
||||
ttl: now()->addHours(6),
|
||||
callback: function () {
|
||||
$plausibleDomain = config('plausible-proxy.domain');
|
||||
|
||||
return Http::timeout(5)
|
||||
->get("{$plausibleDomain}/js/script.js")
|
||||
->throw()
|
||||
->body();
|
||||
}
|
||||
);
|
||||
} catch (RequestException $e) {
|
||||
Log::error('Failed to fetch Plausible script: '.$e->getMessage());
|
||||
|
||||
return response('Error fetching Plausible script', 500);
|
||||
}
|
||||
|
||||
return response($script)
|
||||
->header('Content-Type', 'application/javascript')
|
||||
->header('Cache-Control', 'public, max-age=86400');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace MattitjaAB\LaravelPlausibleProxy\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class PlausibleEventRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'n' => ['required', 'string'],
|
||||
'u' => ['required', 'url'],
|
||||
'd' => ['required', 'string'],
|
||||
'r' => ['nullable', 'url'],
|
||||
'm' => ['nullable', 'string'],
|
||||
'p' => ['nullable', 'array'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace MattitjaAB\LaravelPlausibleProxy;
|
||||
|
||||
class LaravelPlausibleProxy {}
|
||||
@@ -15,7 +15,7 @@ it('returns plausible script from cache', function () {
|
||||
|
||||
it('fetches plausible script if not cached', function () {
|
||||
Http::fake([
|
||||
'https://plausible.io/js/script.js' => Http::response('console.log("Plausible script");', 200),
|
||||
config('plausible-proxy.domain').'/js/script.js' => Http::response('console.log("Plausible script");', 200),
|
||||
]);
|
||||
|
||||
$response = $this->get('/js/script.js');
|
||||
@@ -29,7 +29,7 @@ it('fetches plausible script if not cached', function () {
|
||||
|
||||
it('handles failed script fetch', function () {
|
||||
Http::fake([
|
||||
'https://plausible.io/js/script.js' => Http::response('', 500),
|
||||
config('plausible-proxy.domain').'/js/script.js' => Http::response('', 500),
|
||||
]);
|
||||
|
||||
$response = $this->get('/js/script.js');
|
||||
@@ -46,29 +46,38 @@ it('rejects invalid event data', function () {
|
||||
]);
|
||||
|
||||
$response->assertStatus(422)
|
||||
->assertJsonStructure(['status', 'errors']);
|
||||
->assertJsonValidationErrors(['u' => 'The u field must be a valid URL.']);
|
||||
});
|
||||
|
||||
it('accepts valid event data and defers request', function () {
|
||||
Http::fake();
|
||||
it('accepts valid event data and forwards headers', function () {
|
||||
config()->set('plausible-proxy.domain', 'https://plausible.io');
|
||||
|
||||
$concurrencyMock = Mockery::mock('alias:Illuminate\Support\Facades\Concurrency');
|
||||
$concurrencyMock->shouldReceive('defer')->once()->andReturnUsing(function ($callback) {
|
||||
$callback();
|
||||
});
|
||||
Http::fake([
|
||||
'https://plausible.io/api/event' => function ($request) {
|
||||
return Http::response(['status' => 'queued'], 200);
|
||||
},
|
||||
]);
|
||||
|
||||
$headers = [
|
||||
'User-Agent' => 'TestAgent',
|
||||
'Accept' => 'application/json',
|
||||
];
|
||||
|
||||
$response = $this->postJson('/api/event', [
|
||||
'n' => 'pageview',
|
||||
'u' => 'https://example.com/page',
|
||||
'd' => 'example.com',
|
||||
]);
|
||||
], $headers);
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertJson(['status' => 'queued']);
|
||||
|
||||
Http::assertSent(fn ($request) => $request->url() === config('plausible-proxy.domain').'/api/event' &&
|
||||
$request['n'] === 'pageview'
|
||||
);
|
||||
Http::assertSentCount(1);
|
||||
|
||||
Mockery::close();
|
||||
Http::assertSent(function ($request) {
|
||||
return $request->url() === 'https://plausible.io/api/event'
|
||||
&& isset($request['n']) && $request['n'] === 'pageview'
|
||||
&& isset($request['u']) && $request['u'] === 'https://example.com/page'
|
||||
&& isset($request['d']) && $request['d'] === 'example.com';
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user