privatus-analytics-rails 1.0.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/CHANGELOG.md +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +181 -0
- data/lib/privatus-analytics-rails.rb +3 -0
- data/lib/privatus_analytics/configuration.rb +75 -0
- data/lib/privatus_analytics/helper.rb +19 -0
- data/lib/privatus_analytics/middleware.rb +62 -0
- data/lib/privatus_analytics/railtie.rb +18 -0
- data/lib/privatus_analytics/tag.rb +59 -0
- data/lib/privatus_analytics/version.rb +5 -0
- data/lib/privatus_analytics.rb +28 -0
- metadata +99 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fce613e4182efd36c3b231cdae877639724e6ae9b786f09cd11adaddf05182d5
|
|
4
|
+
data.tar.gz: c0a616f3c617a40484b81ebb855dab2e6ce9d042b5d5864fde8095900e469031
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: efe3c7d6d58028ecbb100cd663b34dfae9daca77fe045c9c19b7346223fa7d18d351f1edc91068ccd490f1aef3b19953fef2b762298fd9bf91745dc6bfa13b31
|
|
7
|
+
data.tar.gz: 3d03ce8f72e0d0a755f9118f5fd3dc68caffab80c00bc5deebc60bec453f4b751cb0b6df3726f8ffbb660e17c19061c072cd4aeb7c7cc5256cf098ccf5f9a891
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0 - 2026-10-20
|
|
4
|
+
|
|
5
|
+
- Initial release.
|
|
6
|
+
- `privatus_analytics_tag` view helper.
|
|
7
|
+
- Optional Rack middleware that adds the tag to the head of HTML responses (off by default).
|
|
8
|
+
- Settings for site id, script host, modules, excluded paths, single-page app mode and more.
|
|
9
|
+
- Skips every environment except production by default.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Two Phase LLC
|
|
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,181 @@
|
|
|
1
|
+
# Privatus Analytics for Rails
|
|
2
|
+
|
|
3
|
+
Adds the cookieless [Privatus Analytics](https://privatusanalytics.com)
|
|
4
|
+
tracking script to your Rails app with a view helper, or with an optional Rack
|
|
5
|
+
middleware that puts it in the `<head>` for you.
|
|
6
|
+
|
|
7
|
+
Privatus Analytics counts visits, sources, pages and conversions without
|
|
8
|
+
cookies, local storage or fingerprinting, so analytics does not need a consent
|
|
9
|
+
banner by itself. Check with your own counsel for your country.
|
|
10
|
+
|
|
11
|
+
The gem only prints the browser script. It never sends events from your server
|
|
12
|
+
and sets no cookies. You need a Privatus Analytics account to see reports, and
|
|
13
|
+
there is a free plan. Sign up at https://privatusanalytics.com.
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
* Ruby 3.2 or later.
|
|
18
|
+
* Rails 6.1 to 8.x.
|
|
19
|
+
* A Privatus Analytics site id (it starts with `pa_`). You find it under
|
|
20
|
+
Site settings, Tracking.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
Add the gem to your `Gemfile`:
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
gem "privatus-analytics-rails"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then run `bundle install`. For the full setup guide and every tracker option,
|
|
31
|
+
see the [Privatus Analytics docs](https://privatusanalytics.com/docs).
|
|
32
|
+
|
|
33
|
+
Set your site id with an environment variable:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
PRIVATUS_SITE_ID=pa_YOURSITEID
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
or in `config/environments/production.rb` (or `config/application.rb`):
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
config.privatus_analytics.site_id = "pa_YOURSITEID"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Add the tag
|
|
46
|
+
|
|
47
|
+
Put the helper in the `<head>` of your layout, for example
|
|
48
|
+
`app/views/layouts/application.html.erb`:
|
|
49
|
+
|
|
50
|
+
```erb
|
|
51
|
+
<head>
|
|
52
|
+
<title>My app</title>
|
|
53
|
+
<%= privatus_analytics_tag %>
|
|
54
|
+
</head>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
In production it prints:
|
|
58
|
+
|
|
59
|
+
```html
|
|
60
|
+
<script defer src="https://privatusstats.com/js/pa.js" data-site="pa_YOURSITEID"></script>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The tag prints nothing when there is no site id, and nothing outside the
|
|
64
|
+
production environment (see below). If your app uses a Content Security Policy
|
|
65
|
+
nonce, the helper adds it for you.
|
|
66
|
+
|
|
67
|
+
### Or let the gem add it
|
|
68
|
+
|
|
69
|
+
If you would rather not touch your layouts, switch on the middleware:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
config.privatus_analytics.auto_inject = true
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
It adds the tag just before `</head>` in uncompressed `text/html` responses with
|
|
76
|
+
status 200. It skips Ajax requests, pages without a `<head>`, and pages that
|
|
77
|
+
already have the tag. Responses that carry a `Content-Encoding` header are left
|
|
78
|
+
alone, so if you compress inside the Rack stack, add the middleware above
|
|
79
|
+
`Rack::Deflater` or use the helper instead.
|
|
80
|
+
|
|
81
|
+
## Configuration
|
|
82
|
+
|
|
83
|
+
Every setting is optional except the site id.
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
# config/environments/production.rb
|
|
87
|
+
Rails.application.configure do
|
|
88
|
+
config.privatus_analytics.site_id = "pa_YOURSITEID" # or ENV["PRIVATUS_SITE_ID"]
|
|
89
|
+
config.privatus_analytics.script_host = "https://privatusstats.com" # or ENV["PRIVATUS_SCRIPT_HOST"]
|
|
90
|
+
config.privatus_analytics.modules = %w[engage auto vitals clicks ask]
|
|
91
|
+
config.privatus_analytics.excluded_paths = %w[/admin/* /preview/*]
|
|
92
|
+
config.privatus_analytics.domains = %w[example.com www.example.com]
|
|
93
|
+
config.privatus_analytics.spa = "auto" # auto, history, hash or off
|
|
94
|
+
config.privatus_analytics.manual = false # true turns off the automatic pageview
|
|
95
|
+
config.privatus_analytics.auto_inject = false
|
|
96
|
+
config.privatus_analytics.enabled_environments = %w[production]
|
|
97
|
+
config.privatus_analytics.extra_attributes = { "data-mask" => "/orders/*->/orders/:id" }
|
|
98
|
+
end
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
| Setting | Default | What it does |
|
|
102
|
+
|---|---|---|
|
|
103
|
+
| `site_id` | `ENV["PRIVATUS_SITE_ID"]` | The `data-site` attribute. Nothing prints without it |
|
|
104
|
+
| `script_host` | `https://privatusstats.com` | Where `/js/pa.js` is served. Change it only if you host your own copy |
|
|
105
|
+
| `modules` | tracker default (`engage`) | The `data-modules` attribute. Setting it replaces the default, so keep `engage` for time on page and scroll depth. `[]` loads none |
|
|
106
|
+
| `excluded_paths` | none | The `data-exclude` attribute. Path globs the tracker never counts |
|
|
107
|
+
| `domains` | all hosts | The `data-domains` attribute. Count only these hostnames |
|
|
108
|
+
| `spa` | `auto` | The `data-spa` attribute |
|
|
109
|
+
| `manual` | `false` | The `data-manual` attribute |
|
|
110
|
+
| `enabled_environments` | `["production"]` | Rails environments that get the tag |
|
|
111
|
+
| `enabled` | `nil` | `true` or `false` forces the tag on or off in every environment |
|
|
112
|
+
| `auto_inject` | `false` | Adds the tag with a Rack middleware |
|
|
113
|
+
| `extra_attributes` | `{}` | Any other `data-*` attribute |
|
|
114
|
+
|
|
115
|
+
Each tracker attribute is explained in the [script attributes
|
|
116
|
+
reference](https://privatusanalytics.com/docs/tracker/attributes), and the
|
|
117
|
+
modules in [Modules](https://privatusanalytics.com/docs/tracker/modules).
|
|
118
|
+
|
|
119
|
+
You can also override a setting for one page:
|
|
120
|
+
|
|
121
|
+
```erb
|
|
122
|
+
<%= privatus_analytics_tag(modules: %w[engage auto], excluded_paths: ["/preview/*"]) %>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Development and test
|
|
126
|
+
|
|
127
|
+
Development, test and staging requests are not counted by default, so your own
|
|
128
|
+
clicking around never reaches your reports. To see the tag while you build, set
|
|
129
|
+
`config.privatus_analytics.enabled = true`. The tracker also ignores `localhost`
|
|
130
|
+
unless you pass `data-allow-local` or `data-debug` (see [Debug
|
|
131
|
+
mode](https://privatusanalytics.com/docs/tracker/debug)):
|
|
132
|
+
|
|
133
|
+
```ruby
|
|
134
|
+
config.privatus_analytics.extra_attributes = { "data-debug" => "true" }
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Leaving people out
|
|
138
|
+
|
|
139
|
+
Use `data-exclude` through `excluded_paths` for admin areas. To leave out
|
|
140
|
+
signed-in staff, render the helper conditionally:
|
|
141
|
+
|
|
142
|
+
```erb
|
|
143
|
+
<%= privatus_analytics_tag unless current_user&.admin? %>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Track your own events
|
|
147
|
+
|
|
148
|
+
The gem only loads the tracker. Track custom events from the browser, as
|
|
149
|
+
described in the [JavaScript API
|
|
150
|
+
docs](https://privatusanalytics.com/docs/tracker/javascript-api):
|
|
151
|
+
|
|
152
|
+
```erb
|
|
153
|
+
<button data-privatus-event="Newsletter signup" data-privatus-prop-list="weekly">Subscribe</button>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Privacy
|
|
157
|
+
|
|
158
|
+
The script sets no cookies and stores nothing in the browser. See the
|
|
159
|
+
[privacy documentation](https://privatusanalytics.com/docs/privacy) for what is
|
|
160
|
+
collected.
|
|
161
|
+
|
|
162
|
+
## Development
|
|
163
|
+
|
|
164
|
+
The tests run in Docker so your local Ruby does not matter:
|
|
165
|
+
|
|
166
|
+
```sh
|
|
167
|
+
./run-docker.sh
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
It runs the Minitest suite against a small dummy Rails app, RuboCop, and
|
|
171
|
+
`gem build`.
|
|
172
|
+
|
|
173
|
+
## Contributing
|
|
174
|
+
|
|
175
|
+
Bug reports and pull requests are welcome at
|
|
176
|
+
https://github.com/Privatus-Analytics/rails.
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
MIT. See [LICENSE.txt](LICENSE.txt). Privatus Analytics is a product of Two
|
|
181
|
+
Phase LLC, Wyoming, USA.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PrivatusAnalytics
|
|
4
|
+
# Settings for the tracking script. Set them with
|
|
5
|
+
# `config.privatus_analytics.<name> = value` in a Rails environment file,
|
|
6
|
+
# or with `PrivatusAnalytics.configure`.
|
|
7
|
+
class Configuration
|
|
8
|
+
DEFAULT_HOST = "https://privatusstats.com"
|
|
9
|
+
MODULES = %w[engage auto vitals clicks ask].freeze
|
|
10
|
+
SPA_MODES = %w[auto history hash off].freeze
|
|
11
|
+
|
|
12
|
+
# The site id (starts with pa_). Falls back to ENV["PRIVATUS_SITE_ID"].
|
|
13
|
+
attr_writer :site_id
|
|
14
|
+
# Origin that serves /js/pa.js. Falls back to ENV["PRIVATUS_SCRIPT_HOST"].
|
|
15
|
+
attr_writer :script_host
|
|
16
|
+
# Tracker modules to load, for example %w[engage auto]. nil keeps the tracker default.
|
|
17
|
+
attr_accessor :modules
|
|
18
|
+
# Path globs the tracker never counts, for example %w[/admin/* /preview/*].
|
|
19
|
+
attr_accessor :excluded_paths
|
|
20
|
+
# Hostnames to count. nil counts every host.
|
|
21
|
+
attr_accessor :domains
|
|
22
|
+
# One of auto, history, hash, off. nil keeps the tracker default.
|
|
23
|
+
attr_accessor :spa
|
|
24
|
+
# true turns off the automatic pageview. See the manual mode docs.
|
|
25
|
+
attr_accessor :manual
|
|
26
|
+
# Rails environments that get the tag. Used when `enabled` is nil.
|
|
27
|
+
attr_accessor :enabled_environments
|
|
28
|
+
# true or false forces the tag on or off whatever the environment. nil uses enabled_environments.
|
|
29
|
+
attr_accessor :enabled
|
|
30
|
+
# true adds the tag to the head of HTML responses with a Rack middleware.
|
|
31
|
+
attr_accessor :auto_inject
|
|
32
|
+
# Extra data- attributes as a hash, for example { "data-mask" => "/orders/*->/orders/:id" }.
|
|
33
|
+
attr_accessor :extra_attributes
|
|
34
|
+
|
|
35
|
+
def initialize
|
|
36
|
+
@site_id = nil
|
|
37
|
+
@script_host = nil
|
|
38
|
+
@modules = nil
|
|
39
|
+
@excluded_paths = []
|
|
40
|
+
@domains = nil
|
|
41
|
+
@spa = nil
|
|
42
|
+
@manual = false
|
|
43
|
+
@enabled_environments = %w[production]
|
|
44
|
+
@enabled = nil
|
|
45
|
+
@auto_inject = false
|
|
46
|
+
@extra_attributes = {}
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def site_id
|
|
50
|
+
value = @site_id.to_s.strip
|
|
51
|
+
value = ENV.fetch("PRIVATUS_SITE_ID", "").strip if value.empty?
|
|
52
|
+
value.empty? ? nil : value
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def script_host
|
|
56
|
+
value = @script_host.to_s.strip
|
|
57
|
+
value = ENV.fetch("PRIVATUS_SCRIPT_HOST", "").strip if value.empty?
|
|
58
|
+
value = DEFAULT_HOST if value.empty?
|
|
59
|
+
value.chomp("/")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Whether the current Rails environment should get the tag.
|
|
63
|
+
def enabled?(environment = current_environment)
|
|
64
|
+
return @enabled ? true : false unless @enabled.nil?
|
|
65
|
+
|
|
66
|
+
Array(enabled_environments).map(&:to_s).include?(environment.to_s)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def current_environment
|
|
72
|
+
defined?(Rails) && Rails.respond_to?(:env) ? Rails.env.to_s : ENV.fetch("RAILS_ENV", "development")
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PrivatusAnalytics
|
|
4
|
+
# View helper. Included into ActionView by the Railtie.
|
|
5
|
+
module Helper
|
|
6
|
+
# Prints the Privatus Analytics script tag. Put it in the head of your layout.
|
|
7
|
+
#
|
|
8
|
+
# <%= privatus_analytics_tag %>
|
|
9
|
+
# <%= privatus_analytics_tag(modules: %w[engage auto]) %>
|
|
10
|
+
#
|
|
11
|
+
# Prints nothing without a site id, or outside the enabled environments.
|
|
12
|
+
def privatus_analytics_tag(options = {})
|
|
13
|
+
options = options.dup
|
|
14
|
+
options[:nonce] ||= content_security_policy_nonce if respond_to?(:content_security_policy_nonce)
|
|
15
|
+
html = Tag.new(PrivatusAnalytics.configuration, options).to_s
|
|
16
|
+
html.respond_to?(:html_safe) ? html.html_safe : html
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PrivatusAnalytics
|
|
4
|
+
# Adds the script tag just before </head> in HTML responses.
|
|
5
|
+
# Off unless `config.privatus_analytics.auto_inject = true`.
|
|
6
|
+
class Middleware
|
|
7
|
+
HEAD_CLOSE = %r{</head>}i
|
|
8
|
+
MARKER = "/js/pa.js"
|
|
9
|
+
|
|
10
|
+
def initialize(app, configuration = nil)
|
|
11
|
+
@app = app
|
|
12
|
+
@configuration = configuration
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call(env)
|
|
16
|
+
status, headers, body = @app.call(env)
|
|
17
|
+
return [status, headers, body] unless inject?(status, headers, env)
|
|
18
|
+
|
|
19
|
+
inject(status, headers, body)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def configuration
|
|
25
|
+
@configuration || PrivatusAnalytics.configuration
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def inject?(status, headers, env)
|
|
29
|
+
return false unless configuration.auto_inject
|
|
30
|
+
return false unless status == 200 && html?(headers) && !encoded?(headers)
|
|
31
|
+
return false if env["HTTP_X_REQUESTED_WITH"].to_s.casecmp?("XMLHttpRequest")
|
|
32
|
+
|
|
33
|
+
Tag.new(configuration).render?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def encoded?(headers)
|
|
37
|
+
!(headers["content-encoding"] || headers["Content-Encoding"]).nil?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def html?(headers)
|
|
41
|
+
type = headers["content-type"] || headers["Content-Type"]
|
|
42
|
+
type.to_s.downcase.start_with?("text/html")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def inject(status, headers, body)
|
|
46
|
+
html = +""
|
|
47
|
+
body.each { |part| html << part.to_s }
|
|
48
|
+
body.close if body.respond_to?(:close)
|
|
49
|
+
|
|
50
|
+
unless html.include?(MARKER) || !html.match?(HEAD_CLOSE)
|
|
51
|
+
tag = Tag.new(configuration).to_s
|
|
52
|
+
html = html.sub(HEAD_CLOSE) { "#{tag}\n</head>" }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
headers = headers.dup
|
|
56
|
+
headers.delete("content-length")
|
|
57
|
+
headers.delete("Content-Length")
|
|
58
|
+
headers["content-length"] = html.bytesize.to_s
|
|
59
|
+
[status, headers, [html]]
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/railtie"
|
|
4
|
+
|
|
5
|
+
module PrivatusAnalytics
|
|
6
|
+
# Wires the settings, the view helper and the optional middleware into Rails.
|
|
7
|
+
class Railtie < Rails::Railtie
|
|
8
|
+
config.privatus_analytics = PrivatusAnalytics.configuration
|
|
9
|
+
|
|
10
|
+
initializer "privatus_analytics.helper" do
|
|
11
|
+
ActiveSupport.on_load(:action_view) { include PrivatusAnalytics::Helper }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
initializer "privatus_analytics.middleware" do |app|
|
|
15
|
+
app.middleware.use PrivatusAnalytics::Middleware
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "cgi"
|
|
4
|
+
|
|
5
|
+
module PrivatusAnalytics
|
|
6
|
+
# Builds the script tag. Returns an empty string when no tag should print.
|
|
7
|
+
class Tag
|
|
8
|
+
LIST_OPTIONS = { modules: "data-modules", excluded_paths: "data-exclude", domains: "data-domains" }.freeze
|
|
9
|
+
|
|
10
|
+
def initialize(configuration = PrivatusAnalytics.configuration, overrides = {})
|
|
11
|
+
@configuration = configuration
|
|
12
|
+
@overrides = overrides.transform_keys(&:to_sym)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def render?
|
|
16
|
+
!@configuration.site_id.nil? && @configuration.enabled?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def to_s
|
|
20
|
+
return "" unless render?
|
|
21
|
+
|
|
22
|
+
parts = attributes.map { |name, value| value == true ? name : %(#{name}="#{CGI.escapeHTML(value.to_s)}") }
|
|
23
|
+
"<script #{parts.join(' ')}></script>"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def attributes
|
|
27
|
+
attrs = { "defer" => true, "src" => "#{@configuration.script_host}/js/pa.js",
|
|
28
|
+
"data-site" => @configuration.site_id }
|
|
29
|
+
add_options(attrs)
|
|
30
|
+
@configuration.extra_attributes.each { |name, value| attrs[name.to_s] = value unless value.nil? }
|
|
31
|
+
attrs["nonce"] = @overrides[:nonce] if @overrides[:nonce]
|
|
32
|
+
attrs
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def add_options(attrs)
|
|
38
|
+
LIST_OPTIONS.each do |name, attribute|
|
|
39
|
+
values = list(option(name))
|
|
40
|
+
# nil keeps the tracker default. An empty modules list loads none.
|
|
41
|
+
attrs[attribute] = values.join(",") if values && (name == :modules || !values.empty?)
|
|
42
|
+
end
|
|
43
|
+
spa = option(:spa).to_s
|
|
44
|
+
attrs["data-spa"] = spa if Configuration::SPA_MODES.include?(spa) && spa != "auto"
|
|
45
|
+
attrs["data-manual"] = "true" if option(:manual) == true
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def option(name)
|
|
49
|
+
@overrides.key?(name) ? @overrides[name] : @configuration.public_send(name)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# nil stays nil. Anything else becomes a clean array of strings.
|
|
53
|
+
def list(value)
|
|
54
|
+
return nil if value.nil?
|
|
55
|
+
|
|
56
|
+
Array(value).flat_map { |item| item.to_s.split(",") }.map(&:strip).reject(&:empty?)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "privatus_analytics/version"
|
|
4
|
+
require "privatus_analytics/configuration"
|
|
5
|
+
require "privatus_analytics/tag"
|
|
6
|
+
require "privatus_analytics/middleware"
|
|
7
|
+
require "privatus_analytics/helper"
|
|
8
|
+
# Adds the cookieless Privatus Analytics tracking script to Rails apps.
|
|
9
|
+
#
|
|
10
|
+
# The gem only prints the browser script. It never sends events from the server.
|
|
11
|
+
module PrivatusAnalytics
|
|
12
|
+
class << self
|
|
13
|
+
def configuration
|
|
14
|
+
@configuration ||= Configuration.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def configure
|
|
18
|
+
yield configuration
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Puts every setting back to its default. Handy in tests.
|
|
22
|
+
def reset_configuration!
|
|
23
|
+
configuration.send(:initialize)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
require "privatus_analytics/railtie" if defined?(Rails::Railtie)
|
metadata
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: privatus-analytics-rails
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Two Phase LLC
|
|
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: actionview
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '6.1'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '9'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '6.1'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '9'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: railties
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '6.1'
|
|
39
|
+
- - "<"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '9'
|
|
42
|
+
type: :runtime
|
|
43
|
+
prerelease: false
|
|
44
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '6.1'
|
|
49
|
+
- - "<"
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '9'
|
|
52
|
+
description: Adds the Privatus Analytics tracking script to your Rails views with
|
|
53
|
+
a helper or an optional Rack middleware. The script sets no cookies, so analytics
|
|
54
|
+
does not need a consent banner by itself.
|
|
55
|
+
email:
|
|
56
|
+
- james@privatusanalytics.com
|
|
57
|
+
executables: []
|
|
58
|
+
extensions: []
|
|
59
|
+
extra_rdoc_files: []
|
|
60
|
+
files:
|
|
61
|
+
- CHANGELOG.md
|
|
62
|
+
- LICENSE.txt
|
|
63
|
+
- README.md
|
|
64
|
+
- lib/privatus-analytics-rails.rb
|
|
65
|
+
- lib/privatus_analytics.rb
|
|
66
|
+
- lib/privatus_analytics/configuration.rb
|
|
67
|
+
- lib/privatus_analytics/helper.rb
|
|
68
|
+
- lib/privatus_analytics/middleware.rb
|
|
69
|
+
- lib/privatus_analytics/railtie.rb
|
|
70
|
+
- lib/privatus_analytics/tag.rb
|
|
71
|
+
- lib/privatus_analytics/version.rb
|
|
72
|
+
homepage: https://privatusanalytics.com
|
|
73
|
+
licenses:
|
|
74
|
+
- MIT
|
|
75
|
+
metadata:
|
|
76
|
+
homepage_uri: https://privatusanalytics.com
|
|
77
|
+
source_code_uri: https://github.com/Privatus-Analytics/rails
|
|
78
|
+
changelog_uri: https://github.com/Privatus-Analytics/rails/blob/main/CHANGELOG.md
|
|
79
|
+
bug_tracker_uri: https://github.com/Privatus-Analytics/rails/issues
|
|
80
|
+
documentation_uri: https://privatusanalytics.com/docs
|
|
81
|
+
rubygems_mfa_required: 'true'
|
|
82
|
+
rdoc_options: []
|
|
83
|
+
require_paths:
|
|
84
|
+
- lib
|
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '3.2'
|
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
requirements: []
|
|
96
|
+
rubygems_version: 3.6.9
|
|
97
|
+
specification_version: 4
|
|
98
|
+
summary: Cookieless Privatus Analytics tracking script for Rails.
|
|
99
|
+
test_files: []
|