Class: GtmOnRails::DataLayer
- Inherits:
-
Object
- Object
- GtmOnRails::DataLayer
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/gtm_on_rails/models/data_layer.rb
Defined Under Namespace
Classes: Ecommerce, Event, Object
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_html ⇒ Object
- #push(objects) ⇒ Object
- #to_js ⇒ 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_html ⇒ Object
43 44 45 |
# File 'lib/gtm_on_rails/models/data_layer.rb', line 43 def print_on_html content_tag(:script, self.to_js) 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::DataLayer::Object.new(object) when GtmOnRails::DataLayer::Object @objects << object end end end |
#to_js ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gtm_on_rails/models/data_layer.rb', line 25 def to_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 logger.warn("DataLayer bytesize is over limit #{GtmOnRails.config.data_layer_limit_byte_size} bytes. Size is #{size} bytes.") next end js_codes << data_layer_object.to_js end return js_codes.join.html_safe end |