Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Improve `BaseAnalyticsEventHandler` and introduce `Spree::Analytics` (#12420)
Damian Legawiecdfdc557c695b
diff --git a/core/app/models/spree/base_analytics_event_handler.rb b/core/app/models/spree/base_analytics_event_handler.rbindex d1d0b26135..04a90b82e0 100644--- a/core/app/models/spree/base_analytics_event_handler.rb+++ b/core/app/models/spree/base_analytics_event_handler.rb@@ -1,50 +1,23 @@module Spreeclass BaseAnalyticsEventHandler- SUPPORTED_EVENTS = {- product_viewed: 'Product Viewed',- product_list_viewed: 'Product List Viewed',- product_searched: 'Product Searched',- product_added: 'Product Added',- product_removed: 'Product Removed',-- payment_info_entered: 'Payment Info Entered',- coupon_entered: 'Coupon Entered',- coupon_removed: 'Coupon Removed',- coupon_applied: 'Coupon Applied',- coupon_denied: 'Coupon Denied',-- checkout_started: 'Checkout Started',- checkout_email_entered: 'Checkout Email Entered',- checkout_step_viewed: 'Checkout Step Viewed',- checkout_step_completed: 'Checkout Step Completed',-- order_completed: 'Order Completed',- order_cancelled: 'Order Cancelled',- order_refunded: 'Order Refunded',- package_shipped: 'Package Shipped',- order_fulfilled: 'Order Fulfilled',-- gift_card_issued: 'Gift Card Issued'- }.freeze-- # Returns the client method- # @return [Symbol] eg. :ahoy- def self.client_method- raise NotImplementedError, 'Subclasses must implement the client_method method'- end-# Initializes the event handler# @param client [Object] The client object# @param opts [Hash] The options# @option opts [User] :user# @option opts [String] :session_id- def initialize(client, opts = {})- @client = client+ def initialize(opts = {})@user = opts[:user]- @session_id = opts[:session_id]+ @session = opts[:session]+ @request = opts[:request]end- attr_reader :client, :user, :session_id+ attr_reader :user, :session, :request++ # Returns the client+ # @return [Object] The client object+ def client+ raise NotImplementedError, 'Subclasses must implement the client method'+ end# Handles the event# @param event_name [String] eg. 'order_completed'@@ -55,11 +28,11 @@ module Spreeend# rubocop:enable Lint/UnusedMethodArgument- # Returns the label for the event+ # Returns the human name for the event# @param event_name [String] eg. 'order_completed'# @return [String] eg. 'Order Completed'- def event_label(event_name)- SUPPORTED_EVENTS[event_name.to_sym]+ def event_human_name(event_name)+ Analytics.events[event_name.to_sym]endprotected@@ -69,7 +42,7 @@ module Spreedef identity_hash{user_id: user&.id,- session_id: session_id+ session_id: session&.id}endenddiff --git a/core/lib/spree/analytics.rb b/core/lib/spree/analytics.rbnew file mode 100644index 0000000000..a5072187ef--- /dev/null+++ b/core/lib/spree/analytics.rb@@ -0,0 +1,11 @@+module Spree+ module Analytics+ def self.events+ @@supported_events ||= Rails.application.config.spree.analytics_events+ end++ def self.event_handlers+ @@event_handlers ||= Rails.application.config.spree.analytics_event_handlers+ end+ end+enddiff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rbindex 7fbe413692..aa52be3228 100644--- a/core/lib/spree/core.rb+++ b/core/lib/spree/core.rb@@ -177,6 +177,7 @@ require 'spree/money'require 'spree/permitted_attributes'require 'spree/service_module'require 'spree/database_type_utilities'+require 'spree/analytics'require 'spree/core/importer'require 'spree/core/query_filters'diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rbindex 5fd592db6b..72f1b13d14 100644--- a/core/lib/spree/core/engine.rb+++ b/core/lib/spree/core/engine.rb@@ -21,6 +21,7 @@ module Spree:page_sections,:page_blocks,:reports,+ :analytics_events,:analytics_event_handlers)SpreeCalculators = Struct.new(:shipping_methods, :tax_rates, :promotion_actions_create_adjustments, :promotion_actions_create_item_adjustments)PromoEnvironment = Struct.new(:rules, :actions)@@ -232,6 +233,32 @@ module SpreeSpree::Reports::SalesTotal]+ Rails.application.config.spree.analytics_events = {+ product_viewed: 'Product Viewed',+ product_list_viewed: 'Product List Viewed',+ product_searched: 'Product Searched',+ product_added: 'Product Added',+ product_removed: 'Product Removed',++ payment_info_entered: 'Payment Info Entered',+ coupon_entered: 'Coupon Entered',+ coupon_removed: 'Coupon Removed',+ coupon_applied: 'Coupon Applied',+ coupon_denied: 'Coupon Denied',++ checkout_started: 'Checkout Started',+ checkout_email_entered: 'Checkout Email Entered',+ checkout_step_viewed: 'Checkout Step Viewed',+ checkout_step_completed: 'Checkout Step Completed',++ order_completed: 'Order Completed',+ order_cancelled: 'Order Cancelled',+ order_refunded: 'Order Refunded',+ package_shipped: 'Package Shipped',+ order_fulfilled: 'Order Fulfilled',++ gift_card_issued: 'Gift Card Issued'+ }Rails.application.config.spree.analytics_event_handlers = []enddiff --git a/core/spec/models/spree/base_analytics_event_handler_spec.rb b/core/spec/models/spree/base_analytics_event_handler_spec.rbindex 9c6a57a293..b5af7b66eb 100644--- a/core/spec/models/spree/base_analytics_event_handler_spec.rb+++ b/core/spec/models/spree/base_analytics_event_handler_spec.rb@@ -3,20 +3,21 @@ require 'spec_helper'RSpec.describe Spree::BaseAnalyticsEventHandler dolet(:client) { double('client') }let(:user) { create(:user) }- let(:session_id) { 'test-session-123' }- let(:handler) { described_class.new(client, user: user, session_id: session_id) }+ let(:session) { double('session', id: 'test-session-123') }+ let(:request) { double('request', remote_ip: '127.0.0.1') }+ let(:handler) { described_class.new(user: user, session: session, request: request) }- describe '.client_method' do+ describe '#client' doit 'raises NotImplementedError' do- expect { described_class.client_method }.to raise_error(NotImplementedError)+ expect { handler.client }.to raise_error(NotImplementedError)endenddescribe '#initialize' do- it 'sets client, user and session_id' do- expect(handler.client).to eq(client)+ it 'sets user, session and request ' doexpect(handler.user).to eq(user)- expect(handler.session_id).to eq(session_id)+ expect(handler.session).to eq(session)+ expect(handler.request).to eq(request)endend@@ -26,15 +27,15 @@ RSpec.describe Spree::BaseAnalyticsEventHandler doendend- describe '#event_label' do+ describe '#event_human_name' doit 'returns the label for supported events' do- expect(handler.event_label('product_viewed')).to eq('Product Viewed')- expect(handler.event_label('order_completed')).to eq('Order Completed')- expect(handler.event_label('checkout_started')).to eq('Checkout Started')+ expect(handler.event_human_name('product_viewed')).to eq('Product Viewed')+ expect(handler.event_human_name('order_completed')).to eq('Order Completed')+ expect(handler.event_human_name('checkout_started')).to eq('Checkout Started')endit 'returns nil for unsupported events' do- expect(handler.event_label('unsupported_event')).to be_nil+ expect(handler.event_human_name('unsupported_event')).to be_nilendend@@ -43,18 +44,18 @@ RSpec.describe Spree::BaseAnalyticsEventHandler doit 'returns hash with user_id and session_id' doexpect(handler.send(:identity_hash)).to eq({user_id: user.id,- session_id: session_id+ session_id: session.id})endendcontext 'when user is not present' do- let(:handler) { described_class.new(client, session_id: session_id) }+ let(:handler) { described_class.new(session: session) }it 'returns hash with nil user_id and session_id' doexpect(handler.send(:identity_hash)).to eq({user_id: nil,- session_id: session_id+ session_id: session.id})endend