Class: GtmOnRails::DataLayer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DataLayer

Returns a new instance of DataLayer.



8
9
10
11
12
13
# File 'lib/gtm_on_rails/models/data_layer.rb', line 8

def initialize(*args)
  options  = args.extract_options!
  @objects = [args].flatten # @objects are instances of GTM::DataLayerObject

  @objects_before_page_view = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/gtm_on_rails/models/data_layer.rb', line 57

def method_missing(method, *args, &block)
  if @objects.respond_to?(method)
    @objects.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#objectsObject

Returns the value of attribute objects.



6
7
8
# File 'lib/gtm_on_rails/models/data_layer.rb', line 6

def objects
  @objects
end

#objects_before_page_viewObject

Returns the value of attribute objects_before_page_view.



6
7
8
# File 'lib/gtm_on_rails/models/data_layer.rb', line 6

def objects_before_page_view
  @objects_before_page_view
end

Instance Method Details



53
54
55
# File 'lib/gtm_on_rails/models/data_layer.rb', line 53

def print_on_html(before_page_view: false)
  (:script, self.to_js(before_page_view: before_page_view))
end

#push(args, **options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gtm_on_rails/models/data_layer.rb', line 15

def push(args, **options)
  args = [args].flatten
  args.each do |object|
    case object
    when Hash
      obj = GtmOnRails::DataLayer::Object.new(object)
    when GtmOnRails::DataLayer::Object
      obj = object
    end

    if options[:before_page_view]
      @objects_before_page_view << obj
    else
      @objects << obj
    end
  end
end

#respond_to?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/gtm_on_rails/models/data_layer.rb', line 65

def respond_to?(method, include_all=false)
  Array.new.respond_to?(method, include_all) || super
end

#to_js(before_page_view: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gtm_on_rails/models/data_layer.rb', line 33

def to_js(before_page_view: false)
  js_codes = []

  js_codes << "var dataLayer = dataLayer || [];"

  objs = before_page_view ? @objects_before_page_view : @objects
  objs.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
      Rails.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