Module: Consently::TagsHelper
- Defined in:
- app/helpers/consently/tags_helper.rb
Overview
The three helpers a host application calls: the tags in
, the noscript fallbacks right after , and the banner anywhere on the page.Instance Method Summary collapse
-
#consently_banner(policy_url: nil) ⇒ Object
The banner, the preferences panel, and the JavaScript that releases the blocked tags.
- #consently_consent ⇒ Object
-
#consently_data_layer_push(event, category: :analytics, **payload) ⇒ Object
Push an event onto the dataLayer from a view, respecting consent: with no analytics consent the event is simply not emitted.
-
#consently_log_url ⇒ Object
Where the banner POSTs the decision, when consent logging is on and the engine is mounted.
-
#consently_noscript_tags ⇒ Object
Goes directly after
- Google Tag Manager and Meta both still ship a noscript fallback. -
#consently_policy ⇒ Object
A complete cookie policy for the tags this request would load: every category, every vendor, every cookie it sets and for how long, plus whether the visitor has agreed to it right now.
-
#consently_preferences_link(name = nil, **options, &block) ⇒ Object
A "Cookie settings" link for the footer.
-
#consently_stylesheet_tag ⇒ Object
The banner brings its own plain CSS - no framework, no build step.
-
#consently_tags ⇒ Object
Every tag configured for this request.
Instance Method Details
#consently_banner(policy_url: nil) ⇒ Object
The banner, the preferences panel, and the JavaScript that releases the blocked tags. Render it once per page, ideally at the end of the body.
The container stays in the DOM after a choice is made so that
consently_preferences_link has something to reopen.
44 45 46 47 48 49 50 51 |
# File 'app/helpers/consently/tags_helper.rb', line 44 def (policy_url: nil) return "".html_safe unless && Consently.(request) render "consently/banner", consent: , policy_url: policy_url || , categories: Consently.config.optional_categories end |
#consently_consent ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'app/helpers/consently/tags_helper.rb', line 66 def @consently_consent ||= if Consently.(request) Consent.([Consently.config.], version: Consently.config.) else # Nobody to ask, so nothing is held back. Consent.new(categories: Consently.config.categories, version: Consently.config.) end end |
#consently_data_layer_push(event, category: :analytics, **payload) ⇒ Object
Push an event onto the dataLayer from a view, respecting consent: with no analytics consent the event is simply not emitted.
<%= consently_data_layer_push("purchase", value: 120, currency: "EUR") %>
79 80 81 82 83 84 |
# File 'app/helpers/consently/tags_helper.rb', line 79 def (event, category: :analytics, **payload) return "".html_safe unless && .granted?(category) payload = payload.merge(event: event) "window.dataLayer = window.dataLayer || []; window.dataLayer.push(#{payload.to_json});" end |
#consently_log_url ⇒ Object
Where the banner POSTs the decision, when consent logging is on and the engine is mounted. Nil otherwise, and the banner skips the request.
104 105 106 107 108 109 110 |
# File 'app/helpers/consently/tags_helper.rb', line 104 def return nil unless Consently.config. . rescue NoMethodError, NameError nil end |
#consently_noscript_tags ⇒ Object
Goes directly after
- Google Tag Manager and Meta both still ship a noscript fallback. Only rendered for categories already granted: there is no way to hold an iframe back and release it later.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/helpers/consently/tags_helper.rb', line 26 def return "".html_safe unless fallbacks = Consently.(request).filter_map do |provider| next unless .granted?(provider.category) provider.noscript&.html_safe end return "".html_safe if fallbacks.empty? content_tag(:noscript, safe_join(fallbacks, "\n")) end |
#consently_policy ⇒ Object
A complete cookie policy for the tags this request would load: every category, every vendor, every cookie it sets and for how long, plus whether the visitor has agreed to it right now.
Drop it into your own policy page under your own heading and legal text.
91 92 93 |
# File 'app/helpers/consently/tags_helper.rb', line 91 def render "consently/policy", tags: Consently.(request), consent: end |
#consently_preferences_link(name = nil, **options, &block) ⇒ Object
A "Cookie settings" link for the footer. Reopens the panel.
Marked rather than wired: the link lives outside the banner element, so a data-action on it would never bind. The controller watches the whole document for a click on anything carrying this attribute, which also means your own markup can reopen the panel just by wearing it.
59 60 61 62 63 64 |
# File 'app/helpers/consently/tags_helper.rb', line 59 def (name = nil, **, &block) name ||= t("consently.preferences_link") [:data] = { consently_open: true }.merge([:data] || {}) link_to(name, "#consently", , &block) end |
#consently_stylesheet_tag ⇒ Object
The banner brings its own plain CSS - no framework, no build step. The
look is driven by custom properties, so overriding a few variables is
usually enough; rails g consently:views is there for the rest.
98 99 100 |
# File 'app/helpers/consently/tags_helper.rb', line 98 def stylesheet_link_tag "consently", media: "all" end |
#consently_tags ⇒ Object
Every tag configured for this request. Tags whose category the visitor has not agreed to are rendered inert (type="text/plain") and the banner turns them into real scripts the moment consent is given - so a visitor who accepts does not have to reload to be counted.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/helpers/consently/tags_helper.rb', line 9 def return "".html_safe unless parts = [] parts << if Consently.config.stylesheet parts << if Consently.config. Consently.(request).each do |provider| granted = .granted?(provider.category) provider.scripts.each { |script| parts << (script, provider, granted) } end safe_join(parts, "\n") end |