Class: Consently::Configuration
- Inherits:
-
Object
- Object
- Consently::Configuration
- Defined in:
- lib/consently/configuration.rb
Overview
Everything the host application sets in config/initializers/consently.rb.
Defined Under Namespace
Classes: Scope
Constant Summary collapse
- DEFAULT_CATEGORIES =
%i[necessary analytics marketing].freeze
Instance Attribute Summary collapse
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#consent_max_age ⇒ Object
How long a consent stays valid, regardless of the cookie's own lifetime.
-
#consent_required ⇒ Object
Whether this visitor has to be asked at all.
-
#consent_subject ⇒ Object
Who made the decision, for the consent log.
-
#consent_version ⇒ Object
Bump this whenever the policy changes: an older consent stops counting and the banner asks again.
-
#cookie_domain ⇒ Object
Where the visitor's choice is kept.
-
#cookie_max_age ⇒ Object
Where the visitor's choice is kept.
-
#cookie_name ⇒ Object
Where the visitor's choice is kept.
-
#cookie_path ⇒ Object
Where the visitor's choice is kept.
-
#enabled ⇒ Object
true, false, or a callable taking the request - e.g.
-
#google_consent_mode ⇒ Object
Google's consent mode v2.
-
#log_consents ⇒ Object
Store a row per decision, as proof of consent.
-
#policy_url ⇒ Object
Where the cookie policy lives.
-
#reload_after_choice ⇒ Object
Reload the page once a choice is made.
-
#respect_do_not_track ⇒ Object
Honour the browser's Do Not Track header as a rejection.
-
#respect_global_privacy_control ⇒ Object
Global Privacy Control, which - unlike DNT - is a binding opt-out signal in California and Colorado.
-
#scope_resolver ⇒ Object
Which scope a request belongs to, e.g.
-
#stylesheet ⇒ Object
Whether consently_tags links the banner's stylesheet.
Instance Method Summary collapse
- #advanced_google_consent_mode? ⇒ Boolean
-
#category(name) ⇒ Object
A category of your own, shown in the preferences panel alongside the built-in ones.
- #default_scope ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #optional_categories ⇒ Object
-
#scope(name) {|scope| ... } ⇒ Object
Tags for one shop, host or whatever your scope_resolver returns.
-
#tag(key, **options) ⇒ Object
c.tag :google_analytics, id: "G-XXXX".
- #tags_for(scope_name = nil) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/consently/configuration.rb', line 103 def initialize @cookie_name = "consently" @cookie_max_age = 60 * 60 * 24 * 180 # six months, the usual guidance @cookie_path = "/" @cookie_domain = nil @consent_max_age = nil @consent_version = 1 @enabled = true @google_consent_mode = :basic @stylesheet = true @log_consents = false @respect_do_not_track = false @respect_global_privacy_control = true @consent_required = true @reload_after_choice = false @consent_subject = nil @policy_url = nil @scope_resolver = nil @categories = DEFAULT_CATEGORIES.dup @scopes = {} end |
Instance Attribute Details
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
101 102 103 |
# File 'lib/consently/configuration.rb', line 101 def categories @categories end |
#consent_max_age ⇒ Object
How long a consent stays valid, regardless of the cookie's own lifetime. Guidance across the EU converges on asking again about once a year; nil leaves the cookie to expire on its own.
16 17 18 |
# File 'lib/consently/configuration.rb', line 16 def @consent_max_age end |
#consent_required ⇒ Object
Whether this visitor has to be asked at all. false means no banner and everything runs - the answer for traffic outside the EU when your legal advice says so:
c. = ->(request) { EU_COUNTRIES.include?(request.headers["CF-IPCountry"]) }
Careful: this switches tags on without asking, so it is opt-in.
78 79 80 |
# File 'lib/consently/configuration.rb', line 78 def @consent_required end |
#consent_subject ⇒ Object
Who made the decision, for the consent log. A callable taking the request; return anything that identifies the visitor in your own system (a global id, "User#42", an account number). Left nil the log stays anonymous, which is the right default for a public site.
91 92 93 |
# File 'lib/consently/configuration.rb', line 91 def @consent_subject end |
#consent_version ⇒ Object
Bump this whenever the policy changes: an older consent stops counting and the banner asks again.
20 21 22 |
# File 'lib/consently/configuration.rb', line 20 def @consent_version end |
#cookie_domain ⇒ Object
Where the visitor's choice is kept. It is read by JavaScript, so it is a plain cookie rather than a signed one.
Set cookie_domain to ".example.com" when the site spans subdomains - without it a consent given on www does not count on shop.
11 12 13 |
# File 'lib/consently/configuration.rb', line 11 def @cookie_domain end |
#cookie_max_age ⇒ Object
Where the visitor's choice is kept. It is read by JavaScript, so it is a plain cookie rather than a signed one.
Set cookie_domain to ".example.com" when the site spans subdomains - without it a consent given on www does not count on shop.
11 12 13 |
# File 'lib/consently/configuration.rb', line 11 def @cookie_max_age end |
#cookie_name ⇒ Object
Where the visitor's choice is kept. It is read by JavaScript, so it is a plain cookie rather than a signed one.
Set cookie_domain to ".example.com" when the site spans subdomains - without it a consent given on www does not count on shop.
11 12 13 |
# File 'lib/consently/configuration.rb', line 11 def @cookie_name end |
#cookie_path ⇒ Object
Where the visitor's choice is kept. It is read by JavaScript, so it is a plain cookie rather than a signed one.
Set cookie_domain to ".example.com" when the site spans subdomains - without it a consent given on www does not count on shop.
11 12 13 |
# File 'lib/consently/configuration.rb', line 11 def @cookie_path end |
#enabled ⇒ Object
true, false, or a callable taking the request - e.g. c.enabled = ->(request) { Rails.env.production? }
24 25 26 |
# File 'lib/consently/configuration.rb', line 24 def enabled @enabled end |
#google_consent_mode ⇒ Object
Google's consent mode v2. Three settings:
:basic - defaults denied, and Google's own tags stay blocked until
the visitor agrees. Nothing about them reaches Google
before consent. The default, and the strict reading.
:advanced - defaults denied, but Google's tags load right away and
send cookieless pings, which is what lets Google Ads
model the conversions of visitors who said no. More data,
and a request to Google either way - ask your lawyer.
false - no consent mode at all.
true is read as :basic.
38 39 40 |
# File 'lib/consently/configuration.rb', line 38 def @google_consent_mode end |
#log_consents ⇒ Object
Store a row per decision, as proof of consent. Needs the engine mounted
and the migration from rails g consently:consent_log.
55 56 57 |
# File 'lib/consently/configuration.rb', line 55 def @log_consents end |
#policy_url ⇒ Object
Where the cookie policy lives. A string, or a callable taking the view context - handy when the URL is locale dependent.
95 96 97 |
# File 'lib/consently/configuration.rb', line 95 def policy_url @policy_url end |
#reload_after_choice ⇒ Object
Reload the page once a choice is made. Off by default - releasing the blocked tags in place is the whole point, and a reload throws away whatever the visitor was doing. Turn it on when the page itself renders differently depending on consent (an embedded map, a video, a status list) and you would rather let the server decide again.
85 86 87 |
# File 'lib/consently/configuration.rb', line 85 def reload_after_choice @reload_after_choice end |
#respect_do_not_track ⇒ Object
Honour the browser's Do Not Track header as a rejection. Off by default: DNT is advisory and widely ignored, so treating it as a legal signal is your call, not the gem's.
64 65 66 |
# File 'lib/consently/configuration.rb', line 64 def respect_do_not_track @respect_do_not_track end |
#respect_global_privacy_control ⇒ Object
Global Privacy Control, which - unlike DNT - is a binding opt-out signal in California and Colorado. On by default, and a visitor who sends it is never shown the banner: their answer already arrived.
69 70 71 |
# File 'lib/consently/configuration.rb', line 69 def respect_global_privacy_control @respect_global_privacy_control end |
#scope_resolver ⇒ Object
Which scope a request belongs to, e.g. ->(request) { request.host }. Nil means every request sees the default configuration.
99 100 101 |
# File 'lib/consently/configuration.rb', line 99 def scope_resolver @scope_resolver end |
#stylesheet ⇒ Object
Whether consently_tags links the banner's stylesheet. Turn it off if you have taken the views over and styled them yourself.
59 60 61 |
# File 'lib/consently/configuration.rb', line 59 def stylesheet @stylesheet end |
Instance Method Details
#advanced_google_consent_mode? ⇒ Boolean
49 50 51 |
# File 'lib/consently/configuration.rb', line 49 def == :advanced end |
#category(name) ⇒ Object
A category of your own, shown in the preferences panel alongside the built-in ones. Give it a name in your locale file.
127 128 129 130 131 |
# File 'lib/consently/configuration.rb', line 127 def category(name) name = name.to_sym @categories << name unless @categories.include?(name) name end |
#default_scope ⇒ Object
161 162 163 |
# File 'lib/consently/configuration.rb', line 161 def default_scope @default_scope ||= Scope.new end |
#optional_categories ⇒ Object
133 134 135 |
# File 'lib/consently/configuration.rb', line 133 def optional_categories categories - [ Consent::NECESSARY ] end |
#scope(name) {|scope| ... } ⇒ Object
Tags for one shop, host or whatever your scope_resolver returns. Falls back to the tags declared outside any scope, and may override them by declaring the same provider again.
c.scope "shop.example.com" do |s|
s.tag :google_analytics, id: "G-SHOP00001"
end
149 150 151 152 153 |
# File 'lib/consently/configuration.rb', line 149 def scope(name) scope = (@scopes[name.to_s] ||= Scope.new) yield scope if block_given? scope end |
#tag(key, **options) ⇒ Object
c.tag :google_analytics, id: "G-XXXX"
138 139 140 |
# File 'lib/consently/configuration.rb', line 138 def tag(key, **) default_scope.tag(key, **) end |
#tags_for(scope_name = nil) ⇒ Object
155 156 157 158 159 |
# File 'lib/consently/configuration.rb', line 155 def (scope_name = nil) = default_scope..dup .merge!(@scopes[scope_name.to_s].) if scope_name && @scopes.key?(scope_name.to_s) .values end |