Skip to content

Commit c8333c5

Browse files
authored
Added Laravel proxy guide
1 parent da03ecb commit c8333c5

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

docs/proxy/guides/laravel.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Proxying Plausible through Laravel
2+
3+
:::tip Don't want to manage your own proxy? We can handle it for you
4+
Our managed proxy lets you send analytics through your own domain name as a first-party connection. All you need to do is set up a CNAME record using the instructions we'll send you and update the snippet on your site. We'll take care of everything else. [Contact us for details](https://plausible.io/contact).
5+
:::
6+
7+
## Step 1: Get your snippet
8+
9+
In the "**Site Installation**" area of the "**General**" section in your [site settings](website-settings.md) you can see
10+
the snippet specific for your site. It will look similar to the following:
11+
12+
```html
13+
<script async src="https://plausible.io/js/pa-XXXXX.js"></script>
14+
<script>
15+
window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
16+
plausible.init()
17+
</script>
18+
```
19+
20+
Your snippet will have a different script location than the example above. Look for the `https://plausible.io/js/pa-XXXXX.js` part in your snippet - that's the personalized location for your site's script. Mark it down for subsequent steps.
21+
22+
## Step 2: Create the proxy controller
23+
24+
Create a new controller to handle proxying requests to Plausible:
25+
26+
```bash
27+
php artisan make:controller PlausibleProxyController
28+
```
29+
30+
Add the following code to the controller, replacing `https://plausible.io/js/pa-XXXXX.js` with the script location from Step 1.
31+
32+
```php
33+
<?php
34+
35+
namespace App\Http\Controllers;
36+
37+
use Illuminate\Http\Request;
38+
use Illuminate\Http\Response;
39+
use Illuminate\Support\Facades\Cache;
40+
use Illuminate\Support\Facades\Http;
41+
42+
class PlausibleProxyController extends Controller
43+
{
44+
public function script(): Response
45+
{
46+
$script = Cache::remember('plausible-script', now()->addHours(24), function () {
47+
// Replace with your script URL from Step 1
48+
$response = Http::get('https://plausible.io/js/pa-XXXXX.js');
49+
50+
return $response->successful() ? $response->body() : '';
51+
});
52+
53+
return response($script, 200, [
54+
'Content-Type' => 'application/javascript',
55+
'Cache-Control' => 'public, max-age=86400',
56+
]);
57+
}
58+
59+
public function event(Request $request): Response
60+
{
61+
$response = Http::withHeaders([
62+
'User-Agent' => $request->userAgent(),
63+
'X-Forwarded-For' => $request->ip(),
64+
'Content-Type' => 'text/plain',
65+
])->withBody($request->getContent(), 'text/plain')
66+
->post('https://plausible.io/api/event');
67+
68+
return response($response->body(), $response->status());
69+
}
70+
}
71+
```
72+
73+
## Step 3: Register the routes
74+
75+
Add the following routes to your `routes/web.php` file:
76+
77+
```php
78+
use App\Http\Controllers\PlausibleProxyController;
79+
80+
Route::get('/js/script.js', [PlausibleProxyController::class, 'script'])->name('plausible.script');
81+
Route::post('/api/event', [PlausibleProxyController::class, 'event'])->name('plausible.event');
82+
```
83+
84+
Note that:
85+
86+
- You can use whatever paths you like here. Do choose generic names. If you choose something like `analytics`, `stats` or `plausible`, it may get blocked.
87+
88+
89+
## Step 4: Exempt the event route from CSRF
90+
91+
The Plausible script sends POST requests to the event endpoint, which Laravel's CSRF protection will block by default.
92+
93+
In your `bootstrap/app.php` file, add the event route to the CSRF exceptions:
94+
95+
```php
96+
->withMiddleware(function (Middleware $middleware) {
97+
$middleware->validateCsrfTokens(except: [
98+
'api/event', // Add this line
99+
]);
100+
})
101+
```
102+
103+
## Step 5: Update your snippet
104+
105+
Add the script tags to your application's HTML `<head>`, configuring the `src` and `endpoint` to match the named route from Step 3:
106+
107+
```blade
108+
<script async src="{{ route('plausible.script') }}"></script>
109+
<script>
110+
window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
111+
plausible.init({
112+
endpoint: "/api/event"
113+
})
114+
</script>
115+
```

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ module.exports = {
103103
{ type: 'doc', id: 'proxy/guides/nginx', label: 'Nginx' },
104104
{ type: 'doc', id: 'proxy/guides/caddy', label: 'Caddy' },
105105
{ type: 'doc', id: 'proxy/guides/apache', label: 'Apache' },
106+
{ type: 'doc', id: 'proxy/guides/laravel', label: 'Laravel' },
106107
],
107108
},
108109
],

0 commit comments

Comments
 (0)