Class: GtmOnRails::DataLayer
- Inherits:
-
Object
- Object
- GtmOnRails::DataLayer
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/gtm_on_rails/models/data_layer.rb
Instance Attribute Summary collapse
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
Instance Method Summary collapse
-
#initialize(*args) ⇒ DataLayer
constructor
A new instance of DataLayer.
- #print_on_js ⇒ Object
- #push(objects) ⇒ Object
Constructor Details
#initialize(*args) ⇒ DataLayer
Returns a new instance of DataLayer.
8 9 10 11 |
# File 'lib/gtm_on_rails/models/data_layer.rb', line 8 def initialize(*args) = args. @objects = args # @objects are instances of GTM::DataLayerObject end |
Instance Attribute Details
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
6 7 8 |
# File 'lib/gtm_on_rails/models/data_layer.rb', line 6 def objects @objects end |
Instance Method Details
#print_on_js ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gtm_on_rails/models/data_layer.rb', line 25 def print_on_js js_codes = [] js_codes << "var dataLayer = dataLayer || [];" @objects.each do |data_layer_object| # dataLayer size limit exception size = data_layer_object.to_json.bytesize if size > GtmOnRails.config.data_layer_limit_byte_size raise ArgumentError.new("DataLayer bytesize is over limit #{GtmOnRails.config.data_layer_limit_byte_size} bytes. Size is #{size} bytes.") end js_codes << data_layer_object.to_js end return content_tag(:script, js_codes.join.html_safe) end |
#push(objects) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/gtm_on_rails/models/data_layer.rb', line 13 def push(objects) objects = [objects].flatten objects.each do |object| case object when Hash @objects << GtmOnRails::DataLayerObject.new(object) when GtmOnRails::DataLayerObject @objects << object end end end |