Support custom Plausible script path
dependabot-auto-merge / dependabot (pull_request_target) Skipped
Fix PHP code style issues / php-code-styling (push) Successful in 6s
PHPStan / phpstan (push) Successful in 2m32s
run-tests / P8.3 - L11.* - prefer-lowest - ubuntu-latest (push) Failing after 34s
run-tests / P8.4 - L11.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L11.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L12.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L12.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L12.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L12.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L11.* - prefer-stable - ubuntu-latest (push) Canceled after 2s
dependabot-auto-merge / dependabot (pull_request_target) Skipped
Fix PHP code style issues / php-code-styling (push) Successful in 6s
PHPStan / phpstan (push) Successful in 2m32s
run-tests / P8.3 - L11.* - prefer-lowest - ubuntu-latest (push) Failing after 34s
run-tests / P8.4 - L11.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L11.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L12.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L12.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L12.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L12.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L11.* - prefer-stable - ubuntu-latest (push) Canceled after 2s
Add a PLAUSIBLE_SCRIPT config option so site-specific scripts (js/pa-XXXXX.js) and legacy script extensions can be proxied. The script is still served locally from /js/script.js and the cache key is derived from the script URL so changing the script takes effect immediately. Closes #14
This commit is contained in:
4 files changed
+94
-27
No files matched your search
@@ -36,9 +36,12 @@ The `config/plausible-proxy.php` file allows you to customize the Plausible doma
|
||||
```php
|
||||
return [
|
||||
'domain' => env('PLAUSIBLE_DOMAIN', 'https://plausible.io'),
|
||||
'script' => env('PLAUSIBLE_SCRIPT', 'js/script.js'),
|
||||
];
|
||||
```
|
||||
|
||||
`PLAUSIBLE_SCRIPT` is the path of the script on your Plausible instance. Whatever script you choose, it is always served locally from `/js/script.js`.
|
||||
|
||||
## Usage
|
||||
|
||||
### 1. Proxying the JavaScript file
|
||||
@@ -53,6 +56,32 @@ After installation, you can include the script like this:
|
||||
|
||||
The route `/js/script.js` is handled automatically and caches the script for 6 hours.
|
||||
|
||||
#### Site-specific script (pa-XXXXX.js)
|
||||
|
||||
Plausible's current installation instructions use a site-specific script such as `https://plausible.io/js/pa-12345ABCDE.js`, where optional measurements (outbound links, file downloads, etc.) are configured in the Plausible dashboard. To use it, set the script path in your `.env`:
|
||||
|
||||
```env
|
||||
PLAUSIBLE_SCRIPT=js/pa-12345ABCDE.js
|
||||
```
|
||||
|
||||
Then load it locally and tell it to send events through the proxy with the `endpoint` option:
|
||||
|
||||
```html
|
||||
<script async src="/js/script.js"></script>
|
||||
<script>
|
||||
window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
|
||||
plausible.init({ endpoint: '/api/event' })
|
||||
</script>
|
||||
```
|
||||
|
||||
#### Legacy script extensions
|
||||
|
||||
Legacy script variants work the same way, for example:
|
||||
|
||||
```env
|
||||
PLAUSIBLE_SCRIPT=js/script.outbound-links.js
|
||||
```
|
||||
|
||||
### 2. Sending events to Plausible
|
||||
|
||||
The script loaded from `/js/script.js` sends events to `/api/event` on your own domain, which forwards them to Plausible. This helps prevent blocking by ad blockers.
|
||||
|
||||
Reference in new issue
Block a user