21 Commits
Author SHA1 Message Date
Mathias 5eff0cafe5 Merge pull request #7 from MattitjaAB/6-laravel-12
Added support for L12
2025-06-16 22:32:57 +02:00
mathiaspalmqvist 0e25ead520 Added support for L12 2025-06-16 22:30:57 +02:00
github-actions[bot] 67507d036a Merge pull request #4 from MattitjaAB/dependabot/github_actions/dependabot/fetch-metadata-2.4.0
Bump dependabot/fetch-metadata from 2.3.0 to 2.4.0
2025-05-12 03:49:36 +00:00
dependabot[bot] 0ce7b51eff Bump dependabot/fetch-metadata from 2.3.0 to 2.4.0
Bumps [dependabot/fetch-metadata](https://github.com/dependabot/fetch-metadata) from 2.3.0 to 2.4.0.
- [Release notes](https://github.com/dependabot/fetch-metadata/releases)
- [Commits](https://github.com/dependabot/fetch-metadata/compare/v2.3.0...v2.4.0)

---
updated-dependencies:
- dependency-name: dependabot/fetch-metadata
  dependency-version: 2.4.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-05-12 03:49:27 +00:00
github-actions[bot] 891c3d8ff8 Merge pull request #3 from MattitjaAB/dependabot/github_actions/aglipanci/laravel-pint-action-2.5
Bump aglipanci/laravel-pint-action from 2.4 to 2.5
2025-02-03 03:46:55 +00:00
dependabot[bot] e2062499ae Bump aglipanci/laravel-pint-action from 2.4 to 2.5
Bumps [aglipanci/laravel-pint-action](https://github.com/aglipanci/laravel-pint-action) from 2.4 to 2.5.
- [Release notes](https://github.com/aglipanci/laravel-pint-action/releases)
- [Commits](https://github.com/aglipanci/laravel-pint-action/compare/2.4...2.5)

---
updated-dependencies:
- dependency-name: aglipanci/laravel-pint-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-02-03 03:46:39 +00:00
mathiaspalmqvist 1f4bbadab7 Update CHANGELOG 2025-01-31 13:19:59 +00:00
Mathias 9c2ebc5db8 Merge pull request #2 from MattitjaAB/Remove-site
Remove site
2025-01-31 14:16:52 +01:00
mathiaspalmqvist 32721b82cc Fix styling 2025-01-31 13:15:16 +00:00
mathiaspalmqvist 6df45258a6 Update README.md 2025-01-31 14:14:55 +01:00
mathiaspalmqvist 6a358d9d65 Removed site from config file 2025-01-31 14:04:07 +01:00
mathiaspalmqvist 6417d3bd6e Update CHANGELOG 2025-01-31 12:15:00 +00:00
mathiaspalmqvist 3b6afc54f9 Merge branch 'main' of github.com:MattitjaAB/laravel-plausible-proxy 2025-01-31 13:10:42 +01:00
mathiaspalmqvist a2c3ad76c0 Added support for 8.3 2025-01-31 13:10:39 +01:00
mathiaspalmqvist 42debe755e Added support for php 8.3 2025-01-31 13:07:08 +01:00
mathiaspalmqvist 9788f6f895 Update CHANGELOG 2025-01-31 11:39:31 +00:00
Mathias 395b15b80b Merge pull request #1 from MattitjaAB/v1.1
Refactor event forwarding and improve test coverage
2025-01-31 12:36:54 +01:00
mathiaspalmqvist 164c3ab5dd Fix styling 2025-01-31 11:36:31 +00:00
mathiaspalmqvist 26454a9501 Refactor event forwarding and improve test coverage
- Updated event forwarding to include request headers
- Refactored validation to improve error handling
- Fixed issue with missing headers in HTTP request
- Improved test cases for event forwarding and caching
- Ensured proper handling of JSON payloads
- Fixed Plausible API URL configuration in tests
2025-01-31 12:36:07 +01:00
mathiaspalmqvist c3033e1093 Ignore Mac DS_Store files 2025-01-31 12:25:47 +01:00
mathiaspalmqvist f622f3152b Update CHANGELOG 2025-01-30 23:15:03 +00:00
10 changed files with 104 additions and 53 deletions

No files matched your search

+1 -1
View File
@@ -14,7 +14,7 @@ jobs:
- name: Dependabot metadata - name: Dependabot metadata
id: metadata id: metadata
uses: dependabot/fetch-metadata@v2.3.0 uses: dependabot/fetch-metadata@v2.4.0
with: with:
github-token: "${{ secrets.GITHUB_TOKEN }}" github-token: "${{ secrets.GITHUB_TOKEN }}"
@@ -20,7 +20,7 @@ jobs:
ref: ${{ github.head_ref }} ref: ${{ github.head_ref }}
- name: Fix PHP code style issues - name: Fix PHP code style issues
uses: aglipanci/laravel-pint-action@2.4 uses: aglipanci/laravel-pint-action@2.5
- name: Commit changes - name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5 uses: stefanzweifel/git-auto-commit-action@v5
+26 -4
View File
@@ -16,14 +16,27 @@ jobs:
strategy: strategy:
fail-fast: true fail-fast: true
matrix: matrix:
os: [ubuntu-latest, windows-latest] os: [ubuntu-latest]
php: [8.4] php: [8.3, 8.4]
laravel: [11.*] laravel: [11.*, 12.*]
stability: [prefer-lowest, prefer-stable] stability: [prefer-lowest, prefer-stable]
include: include:
- laravel: 11.* - laravel: 11.*
php: 8.3
testbench: 9.* testbench: 9.*
carbon: ^2.63 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 }} 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 }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.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 - name: Install dependencies
run: | 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 composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: List Installed Dependencies - name: List Installed Dependencies
+4
View File
@@ -30,3 +30,7 @@ phpstan.neon
testbench.yaml testbench.yaml
/docs /docs
/coverage /coverage
# Ignore Mac DS_Store files
.DS_Store
**/.DS_Store
+28
View File
@@ -1,3 +1,31 @@
# Changelog # Changelog
All notable changes to `laravel-plausible-proxy` will be documented in this file. All notable changes to `laravel-plausible-proxy` will be documented in this file.
## 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
+8 -28
View File
@@ -5,11 +5,11 @@
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/mattitjaab/laravel-plausible-proxy/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/mattitjaab/laravel-plausible-proxy/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/mattitjaab/laravel-plausible-proxy/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/mattitjaab/laravel-plausible-proxy/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/mattitjaab/laravel-plausible-proxy.svg?style=flat-square)](https://packagist.org/packages/mattitjaab/laravel-plausible-proxy) [![Total Downloads](https://img.shields.io/packagist/dt/mattitjaab/laravel-plausible-proxy.svg?style=flat-square)](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 ## Requirements
- Laravel 11 - Laravel 12
- PHP 8.4+ - PHP 8.3, 8.4
## 🚀 Installation ## 🚀 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: 3. Add the following environment variables to your `.env` file:
```env ```env
PLAUSIBLE_DOMAIN=https://plausible.io 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 ## ⚙️ 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 ```php
return [ return [
'domain' => env('PLAUSIBLE_DOMAIN', 'https://plausible.io'), '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**. The route `/js/script.js` is automatically handled and caches the script for **6 hours**.
### **2. Sending Events to Plausible** ### **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. 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.
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"}'
```
## ✅ Features ## ✅ Features
- 🚀 **Fast & Optimized**: Caches the Plausible script to reduce external calls. - 🚀 **Fast & Optimized**: Caches the Plausible script to reduce external calls.
- 🔒 **Privacy-Friendly**: Prevents direct calls to `plausible.io`. - ✅ No issues with ad blockers: Proxies the request via a local API endpoint.
- 📊 **Improved Performance**: Events are processed via Laravel Queues to avoid blocking user interactions. - 🛠 **Fully Configurable**: Easily set your own Plausible instance via `.env`.
- 🛠 **Fully Configurable**: Easily set your own Plausible instance and domain via `.env`.
## 🛠 Testing ## 🛠 Testing
+2 -2
View File
@@ -18,9 +18,9 @@
} }
], ],
"require": { "require": {
"php": "^8.4", "php": "^8.3",
"spatie/laravel-package-tools": "^1.16", "spatie/laravel-package-tools": "^1.16",
"illuminate/contracts": "^10.0||^11.0" "illuminate/contracts": "^10.0||^11.0||^12.0"
}, },
"require-dev": { "require-dev": {
"laravel/pint": "^1.14", "laravel/pint": "^1.14",
-1
View File
@@ -4,5 +4,4 @@
return [ return [
'domain' => env('PLAUSIBLE_DOMAIN', 'https://plausible.io'), 'domain' => env('PLAUSIBLE_DOMAIN', 'https://plausible.io'),
'site' => env('PLAUSIBLE_SITE', 'example.com'),
]; ];
+12 -3
View File
@@ -1,13 +1,18 @@
<?php <?php
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Concurrency;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
Route::post('api/event', function (Request $request) { Route::post('api/event', function (Request $request) {
$validator = Validator::make($request->all(), [ $data = json_decode($request->getContent(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
return response()->json(['error' => 'Invalid JSON format'], 422);
}
$validator = Validator::make($data, [
'n' => 'required|string', 'n' => 'required|string',
'u' => 'required|url', 'u' => 'required|url',
'd' => 'required|string', 'd' => 'required|string',
@@ -20,7 +25,11 @@ Route::post('api/event', function (Request $request) {
return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422); return response()->json(['status' => 'error', 'errors' => $validator->errors()], 422);
} }
Concurrency::defer(fn () => Http::post(config('plausible-proxy.domain').'/api/event', $request->all())); $headers = collect($request->headers->all())
->mapWithKeys(fn ($value, $key) => [$key => is_array($value) ? implode(', ', $value) : $value])
->all();
Http::withHeaders($headers)->post(config('plausible-proxy.domain').'/api/event', $data);
return response()->json(['status' => 'queued']); return response()->json(['status' => 'queued']);
}); });
+22 -13
View File
@@ -15,7 +15,7 @@ it('returns plausible script from cache', function () {
it('fetches plausible script if not cached', function () { it('fetches plausible script if not cached', function () {
Http::fake([ 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'); $response = $this->get('/js/script.js');
@@ -29,7 +29,7 @@ it('fetches plausible script if not cached', function () {
it('handles failed script fetch', function () { it('handles failed script fetch', function () {
Http::fake([ 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'); $response = $this->get('/js/script.js');
@@ -49,26 +49,35 @@ it('rejects invalid event data', function () {
->assertJsonStructure(['status', 'errors']); ->assertJsonStructure(['status', 'errors']);
}); });
it('accepts valid event data and defers request', function () { it('accepts valid event data and forwards headers', function () {
Http::fake(); config()->set('plausible-proxy.domain', 'https://plausible.io');
$concurrencyMock = Mockery::mock('alias:Illuminate\Support\Facades\Concurrency'); Http::fake([
$concurrencyMock->shouldReceive('defer')->once()->andReturnUsing(function ($callback) { 'https://plausible.io/api/event' => function ($request) {
$callback(); return Http::response(['status' => 'queued'], 200);
}); },
]);
$headers = [
'User-Agent' => 'TestAgent',
'Accept' => 'application/json',
];
$response = $this->postJson('/api/event', [ $response = $this->postJson('/api/event', [
'n' => 'pageview', 'n' => 'pageview',
'u' => 'https://example.com/page', 'u' => 'https://example.com/page',
'd' => 'example.com', 'd' => 'example.com',
]); ], $headers);
$response->assertStatus(200) $response->assertStatus(200)
->assertJson(['status' => 'queued']); ->assertJson(['status' => 'queued']);
Http::assertSent(fn ($request) => $request->url() === config('plausible-proxy.domain').'/api/event' && Http::assertSentCount(1);
$request['n'] === 'pageview'
);
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';
});
}); });