Class: Spree::ProductViewsToCartAdditionsReport

Inherits:
Report
  • Object
show all
Defined in:
app/reports/spree/product_views_to_cart_additions_report.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

DEFAULT_SORTABLE_ATTRIBUTE =
:product_name
HEADERS =
{ product_name: :string, views: :integer, cart_additions: :integer, cart_to_view_ratio: :string }
SEARCH_ATTRIBUTES =
{ start_date: :product_view_from, end_date: :product_view_till }
SORTABLE_ATTRIBUTES =
[:product_name, :views, :cart_additions]

Constants inherited from Report

Report::TIME_SCALES

Instance Attribute Summary

Attributes inherited from Report

#current_page, #paginate, #records_per_page, #reporting_period, #search, #sortable_attribute, #sortable_type, #total_records

Instance Method Summary collapse

Methods inherited from Report

#active_record_sort, deeplink, #deeplink_properties, #generate, #get_results, #header_sorted?, #initialize, #name, #paginated?, #pagination_required?, #set_sortable_attributes, #time_scale_columns, #time_scale_columns_to_s, #time_scale_selects, #total_pages

Constructor Details

This class inherits a constructor from Spree::Report

Instance Method Details

#report_queryObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/reports/spree/product_views_to_cart_additions_report.rb', line 20

def report_query
  cart_additions =
    Spree::CartEvent
      .added
      .joins(:variant)
      .joins(:product)
      .where(created_at: reporting_period)
      .group('spree_products.name', 'spree_products.slug')
      .select(
        'spree_products.name              as product_name',
        'spree_products.slug              as product_slug',
        'SUM(spree_cart_events.quantity)  as cart_additions'
      )
  total_views =
    Spree::Product
      .joins(:page_view_events)
      .group(:name)
      .select(
        'spree_products.name  as product_name',
        'COUNT(*)             as views'
      )

  Spree::Report::QueryFragments
    .from_join(cart_additions, total_views, "q1.product_name = q2.product_name")
    .project(
      'q1.product_name',
      'q1.product_slug',
      'q2.views',
      'q1.cart_additions'
    )
end