plausible_proxy_rails 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +101 -0
- data/Rakefile +15 -0
- data/app/helpers/plausible_proxy_rails/helper.rb +18 -0
- data/lib/plausible_proxy_rails/configuration.rb +15 -0
- data/lib/plausible_proxy_rails/engine.rb +15 -0
- data/lib/plausible_proxy_rails/middleware.rb +79 -0
- data/lib/plausible_proxy_rails/version.rb +3 -0
- data/lib/plausible_proxy_rails.rb +18 -0
- metadata +69 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a6fd09306c6f92c3041adb7a9fe89b59f60ffdfa6e6da65643782a54866c8899
|
|
4
|
+
data.tar.gz: c9435ee630438eca0e42fca28d5b4c4edea127fc59e63ec70cda0b4aef0a745d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 845c9759f007c281cf6f034a802cdcc70ded7e2f23ca769101e886dd3647999e7ca90376496e8f78286d755aed56ac0fc1a975017e394b45a5c4fc77f423fe0f
|
|
7
|
+
data.tar.gz: aa7b72174475d03c2b26baff6fee723b660b9786dc84be48aa66a3fd667a85d372b38b898d52d1d9bbc45adb4f62508ba8a75137334b2d56166c6e0d8f05a6e1
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Appercept Limited
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# Plausible Proxy Rails
|
|
2
|
+
|
|
3
|
+
A Rails Engine that proxies [Plausible Analytics](https://plausible.io) requests through your application server. This keeps analytics working for visitors who block third-party scripts, removes the need for `plausible.io` in your Content Security Policy, and improves visitor privacy by preventing direct connections to external servers.
|
|
4
|
+
|
|
5
|
+
## How It Works
|
|
6
|
+
|
|
7
|
+
The gem inserts Rack middleware that intercepts two routes under a configurable proxy path (default: `/ins`):
|
|
8
|
+
|
|
9
|
+
| Request | What happens |
|
|
10
|
+
| -------------------- | ---------------------------------------------------------------------------------------------- |
|
|
11
|
+
| `GET /ins/script.js` | Fetches the Plausible script, caches it for 1 hour, and serves it from your domain |
|
|
12
|
+
| `POST /ins/event` | Forwards the analytics event payload to the Plausible API with the visitor's IP and User-Agent |
|
|
13
|
+
|
|
14
|
+
All other requests pass through to your application untouched.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Add to your Gemfile:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
gem "plausible_proxy_rails"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Then run `bundle install`.
|
|
25
|
+
|
|
26
|
+
## Configuration
|
|
27
|
+
|
|
28
|
+
Create an initializer:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
# config/initializers/plausible.rb
|
|
32
|
+
PlausibleProxyRails.configure do |config|
|
|
33
|
+
config.enabled = Rails.env.production?
|
|
34
|
+
config.domain = "yoursite.com"
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Options
|
|
39
|
+
|
|
40
|
+
| Option | Default | Description |
|
|
41
|
+
| ------------ | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
|
42
|
+
| `enabled` | `false` | Enable the proxy middleware |
|
|
43
|
+
| `domain` | `nil` | Your site's domain as registered in Plausible (required) |
|
|
44
|
+
| `proxy_path` | `"/ins"` | URL path prefix for the proxy endpoints |
|
|
45
|
+
| `script_url` | `"https://plausible.io/js/script.js"` | Plausible script URL — change this to use [script extensions](https://plausible.io/docs/script-extensions) or a self-hosted instance |
|
|
46
|
+
|
|
47
|
+
The middleware is only activated when `enabled` is `true` **and** `domain` is present.
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
Add the script tag to your layout:
|
|
52
|
+
|
|
53
|
+
```erb
|
|
54
|
+
<%# app/views/layouts/application.html.erb %>
|
|
55
|
+
<head>
|
|
56
|
+
<%= plausible_tag %>
|
|
57
|
+
</head>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This renders a `<script>` tag that loads the analytics script from your proxy path and sends events through it. When the proxy is disabled (e.g., in development), `plausible_tag` returns `nil` and nothing is rendered.
|
|
61
|
+
|
|
62
|
+
## Self-Hosted Plausible
|
|
63
|
+
|
|
64
|
+
If you run a [self-hosted Plausible instance](https://plausible.io/docs/self-hosting), point `script_url` at your server. The event API endpoint is derived from the same host automatically:
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
PlausibleProxyRails.configure do |config|
|
|
68
|
+
config.enabled = true
|
|
69
|
+
config.domain = "yoursite.com"
|
|
70
|
+
config.script_url = "https://analytics.yoursite.com/js/script.js"
|
|
71
|
+
end
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Plausible Script Options
|
|
75
|
+
|
|
76
|
+
Plausible provides a site-specific script URL (found in your Plausible account under Site Settings > Site Installation) that looks like `https://plausible.io/js/pa-XXXXXXXXX.js`. This bundles your enabled features automatically. You can also use the generic `https://plausible.io/js/script.js` with [script extensions](https://plausible.io/docs/script-extensions). Either format works with this gem — set your preferred URL as `script_url`:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
# Site-specific script
|
|
80
|
+
config.script_url = "https://plausible.io/js/pa-XXXXXXXXX.js"
|
|
81
|
+
|
|
82
|
+
# Or generic script with extensions
|
|
83
|
+
config.script_url = "https://plausible.io/js/script.file-downloads.outbound-links.js"
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
See the [Plausible docs](https://plausible.io/docs/plausible-script) for more on script options.
|
|
87
|
+
|
|
88
|
+
## Requirements
|
|
89
|
+
|
|
90
|
+
- Ruby >= 3.2
|
|
91
|
+
- Rails >= 7.0
|
|
92
|
+
|
|
93
|
+
## Contributing
|
|
94
|
+
|
|
95
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/appercept/plausible_proxy_rails). See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
|
|
96
|
+
|
|
97
|
+
This project is intended to be a safe, welcoming space for collaboration. Please follow our [Code of Conduct](CODE_OF_CONDUCT.md).
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require "bundler/setup"
|
|
2
|
+
|
|
3
|
+
APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
|
|
4
|
+
load "rails/tasks/engine.rake"
|
|
5
|
+
|
|
6
|
+
load "rails/tasks/statistics.rake"
|
|
7
|
+
|
|
8
|
+
require "bundler/gem_tasks"
|
|
9
|
+
require "rspec/core/rake_task"
|
|
10
|
+
|
|
11
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
12
|
+
|
|
13
|
+
require "standard/rake"
|
|
14
|
+
|
|
15
|
+
task default: %i[spec standard]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module PlausibleProxyRails
|
|
2
|
+
module Helper
|
|
3
|
+
def plausible_tag
|
|
4
|
+
config = PlausibleProxyRails.configuration
|
|
5
|
+
return unless config.enabled?
|
|
6
|
+
|
|
7
|
+
tag.script(
|
|
8
|
+
nil,
|
|
9
|
+
src: "#{config.proxy_path}/script.js",
|
|
10
|
+
defer: true,
|
|
11
|
+
data: {
|
|
12
|
+
domain: config.domain,
|
|
13
|
+
api: "#{config.proxy_path}/event"
|
|
14
|
+
}
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module PlausibleProxyRails
|
|
2
|
+
class Configuration
|
|
3
|
+
attr_accessor :enabled, :domain, :proxy_path, :script_url
|
|
4
|
+
|
|
5
|
+
def initialize
|
|
6
|
+
@enabled = false
|
|
7
|
+
@proxy_path = "/ins"
|
|
8
|
+
@script_url = "https://plausible.io/js/script.js"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def enabled?
|
|
12
|
+
enabled && domain.present?
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module PlausibleProxyRails
|
|
2
|
+
class Engine < ::Rails::Engine
|
|
3
|
+
initializer "plausible_proxy_rails.middleware" do |app|
|
|
4
|
+
if PlausibleProxyRails.configuration.enabled?
|
|
5
|
+
app.middleware.use PlausibleProxyRails::Middleware
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
initializer "plausible_proxy_rails.helpers" do
|
|
10
|
+
ActiveSupport.on_load(:action_view) do
|
|
11
|
+
include PlausibleProxyRails::Helper
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
|
|
5
|
+
module PlausibleProxyRails
|
|
6
|
+
class Middleware
|
|
7
|
+
CACHE_TTL = 3600 # 1 hour in seconds
|
|
8
|
+
|
|
9
|
+
def initialize(app)
|
|
10
|
+
@app = app
|
|
11
|
+
@script_cache = nil
|
|
12
|
+
@cached_at = nil
|
|
13
|
+
@mutex = Mutex.new
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call(env)
|
|
17
|
+
config = PlausibleProxyRails.configuration
|
|
18
|
+
|
|
19
|
+
case env["PATH_INFO"]
|
|
20
|
+
when "#{config.proxy_path}/script.js"
|
|
21
|
+
serve_script(config)
|
|
22
|
+
when "#{config.proxy_path}/event"
|
|
23
|
+
forward_event(env, config)
|
|
24
|
+
else
|
|
25
|
+
@app.call(env)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def serve_script(config)
|
|
32
|
+
body, headers = cached_script(config)
|
|
33
|
+
[200, headers, [body]]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def cached_script(config)
|
|
37
|
+
@mutex.synchronize do
|
|
38
|
+
if @script_cache.nil? || (Time.now - @cached_at) > CACHE_TTL
|
|
39
|
+
fetch_script(config)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
[@script_cache, @cached_headers]
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def fetch_script(config)
|
|
47
|
+
uri = URI(config.script_url)
|
|
48
|
+
response = Net::HTTP.get_response(uri)
|
|
49
|
+
|
|
50
|
+
@script_cache = response.body
|
|
51
|
+
@cached_headers = {
|
|
52
|
+
"content-type" => response["content-type"],
|
|
53
|
+
"cache-control" => "public, max-age=3600"
|
|
54
|
+
}
|
|
55
|
+
@cached_at = Time.now
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def forward_event(env, config)
|
|
59
|
+
request = Rack::Request.new(env)
|
|
60
|
+
body = request.body.read
|
|
61
|
+
|
|
62
|
+
script_uri = URI(config.script_url)
|
|
63
|
+
uri = URI("#{script_uri.scheme}://#{script_uri.host}/api/event")
|
|
64
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
65
|
+
http.use_ssl = true
|
|
66
|
+
|
|
67
|
+
post = Net::HTTP::Post.new(uri.path)
|
|
68
|
+
post["User-Agent"] = env["HTTP_USER_AGENT"]
|
|
69
|
+
post["X-Forwarded-For"] = env["HTTP_X_FORWARDED_FOR"] || env["REMOTE_ADDR"]
|
|
70
|
+
post["Content-Type"] = env["CONTENT_TYPE"]
|
|
71
|
+
post.body = body
|
|
72
|
+
|
|
73
|
+
response = http.request(post)
|
|
74
|
+
response_headers = {"content-type" => response["content-type"]}.compact
|
|
75
|
+
|
|
76
|
+
[response.code.to_i, response_headers, [response.body]]
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require "plausible_proxy_rails/engine"
|
|
2
|
+
require "plausible_proxy_rails/version"
|
|
3
|
+
|
|
4
|
+
module PlausibleProxyRails
|
|
5
|
+
autoload :Configuration, "plausible_proxy_rails/configuration"
|
|
6
|
+
autoload :Helper, "plausible_proxy_rails/helper"
|
|
7
|
+
autoload :Middleware, "plausible_proxy_rails/middleware"
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
def configuration
|
|
11
|
+
@configuration ||= Configuration.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def configure
|
|
15
|
+
yield(configuration)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: plausible_proxy_rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Richard Hatherall
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rails
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 7.0.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 7.0.0
|
|
26
|
+
description: A Rails Engine that proxies Plausible Analytics requests through your
|
|
27
|
+
application server, improving visitor privacy and removing the need for third-party
|
|
28
|
+
CSP exceptions.
|
|
29
|
+
email:
|
|
30
|
+
- richard@appercept.com
|
|
31
|
+
executables: []
|
|
32
|
+
extensions: []
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- LICENSE
|
|
36
|
+
- README.md
|
|
37
|
+
- Rakefile
|
|
38
|
+
- app/helpers/plausible_proxy_rails/helper.rb
|
|
39
|
+
- lib/plausible_proxy_rails.rb
|
|
40
|
+
- lib/plausible_proxy_rails/configuration.rb
|
|
41
|
+
- lib/plausible_proxy_rails/engine.rb
|
|
42
|
+
- lib/plausible_proxy_rails/middleware.rb
|
|
43
|
+
- lib/plausible_proxy_rails/version.rb
|
|
44
|
+
homepage: https://github.com/appercept/plausible_proxy_rails
|
|
45
|
+
licenses:
|
|
46
|
+
- MIT
|
|
47
|
+
metadata:
|
|
48
|
+
homepage_uri: https://github.com/appercept/plausible_proxy_rails
|
|
49
|
+
source_code_uri: https://github.com/appercept/plausible_proxy_rails
|
|
50
|
+
changelog_uri: https://github.com/appercept/plausible_proxy_rails/blob/main/CHANGELOG.md
|
|
51
|
+
rubygems_mfa_required: 'true'
|
|
52
|
+
rdoc_options: []
|
|
53
|
+
require_paths:
|
|
54
|
+
- lib
|
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - ">="
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: 3.2.0
|
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '0'
|
|
65
|
+
requirements: []
|
|
66
|
+
rubygems_version: 3.7.2
|
|
67
|
+
specification_version: 4
|
|
68
|
+
summary: Privacy-preserving Plausible Analytics proxy for Rails
|
|
69
|
+
test_files: []
|