Class: GtmOnRails::DataLayer::Ecommerce::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/gtm_on_rails/models/data_layer/ecommerce/product.rb

Instance Attribute Summary

Attributes inherited from Object

#data

Instance Method Summary collapse

Methods inherited from Object

#add, #as_json, #method_missing, #to_json

Constructor Details

#initialize(**args) ⇒ Product

Returns a new instance of Product.

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gtm_on_rails/models/data_layer/ecommerce/product.rb', line 4

def initialize(**args)
  raise ArgumentError.new("required either 'id' or 'name', or both.") if args[:id].blank? && args[:name].blank?

  @data = {}

  @data[:id]       = args[:id]       if args[:id].present?
  @data[:name]     = args[:name]     if args[:name].present?
  @data[:brand]    = args[:brand]    if args[:brand].present?
  @data[:variant]  = args[:variant]  if args[:variant].present?
  @data[:price]    = args[:price]    if args[:price].present?
  @data[:quantity] = args[:quantity] if args[:quantity].present?
  @data[:coupon]   = args[:coupon]   if args[:coupon].present?
  @data[:position] = args[:position] if args[:position].present?
  if args[:category].present?
    if args[:category].is_a?(Array)
      raise ArgumentError.new("'category' can be up to 5.") if args[:category].count > 5
      @data[:category] = args[:category].join('/')
    else
      @data[:category] = args[:category]
    end
  end

  # Custom Dimensions
  dimension_keys = args.keys.map(&:to_s).select{|key| key.match(/^dimension[0-9]+/)}.map(&:to_sym)
  dimension_keys.each do |dimension_key|
    @data[dimension_key] = args[dimension_key]
  end

  # Custom Metrics
  metric_keys = args.keys.map(&:to_s).select{|key| key.match(/^metric[0-9]+/)}.map(&:to_sym)
  metric_keys.each do |metric_key|
    @data[metric_key] = args[metric_key]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GtmOnRails::DataLayer::Object

Instance Method Details

#to_jsObject



39
40
41
# File 'lib/gtm_on_rails/models/data_layer/ecommerce/product.rb', line 39

def to_js
  to_json
end