Sha256: e9c181bf0cc9863ed3895b39ac17f5365b9cba2b70420750d928be6cac6f9fd8

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

module Spree
  class TrendingSearchReport < Spree::Report
    DEFAULT_SORTABLE_ATTRIBUTE = :occurrences
    HEADERS                    = { searched_term: :string, occurrences: :integer }
    SEARCH_ATTRIBUTES          = { start_date: :start_date, end_date: :end_date, keywords_cont: :keyword }
    SORTABLE_ATTRIBUTES        = [:occurrences]

    def paginated?
      true
    end

    class Result < Spree::Report::Result
      charts FrequencyDistributionPieChart

      class Observation < Spree::Report::Observation
        observation_fields [:searched_term, :occurrences]
      end
    end

    deeplink searched_term: { template: %Q{<a href='/products?utf8=%E2%9C%93&keywords={%# o['searched_term'] %}' target="_blank">{%# o['searched_term'] %}</a>} }

    def paginated_report_query
      report_query
        .take(records_per_page)
        .skip(current_page)
    end

    def record_count_query
      Spree::Report::QueryFragments.from_subquery(report_query).project(Arel.star.count)
    end

    def report_query
      Spree::Report::QueryFragments.from_subquery(searches)
        .project("count(searched_term) as occurrences", "searched_term")
        .group("searched_term")
    end

    private def searches
      Spree::PageEvent
        .where(activity: 'search')
        .where(created_at: reporting_period)
        .where(Spree::PageEvent.arel_table[:search_keywords].matches(keyword_search))
        .select("search_keywords as searched_term")
    end

    private def keyword_search
      search[:keywords_cont].present? ? "%#{ search[:keywords_cont] }%" : '%'
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spree_admin_insights-3.2.0 app/reports/spree/trending_search_report.rb
solidus_admin_insights-2.1.1 app/reports/spree/trending_search_report.rb
solidus_admin_insights-2.1.0 app/reports/spree/trending_search_report.rb