Class: GtmOnRails::DataLayer::Ecommerce::Product
- Defined in:
- lib/gtm_on_rails/models/data_layer/ecommerce/product.rb
Instance Attribute Summary
Attributes inherited from Object
Instance Method Summary collapse
-
#initialize(**args) ⇒ Product
constructor
A new instance of Product.
- #to_js ⇒ Object
Methods inherited from Object
#add, #as_json, #method_missing, #to_json
Constructor Details
#initialize(**args) ⇒ Product
Returns a new instance of Product.
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_js ⇒ Object
39 40 41 |
# File 'lib/gtm_on_rails/models/data_layer/ecommerce/product.rb', line 39 def to_js to_json end |