Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Revert "Clean up analytics configuration API (#13314)"

Damian Legawiec21c08eff100a
diff --git a/core/app/models/spree/base_analytics_event_handler.rb b/core/app/models/spree/base_analytics_event_handler.rb
index e1680d5802..5bb6b7de80 100644
--- a/core/app/models/spree/base_analytics_event_handler.rb
+++ b/core/app/models/spree/base_analytics_event_handler.rb
@@ -35,7 +35,7 @@ module Spree
# @param event_name [String] eg. 'order_completed'
# @return [String] eg. 'Order Completed'
def event_human_name(event_name)
- Spree.analytics.events[event_name.to_sym]
+ Analytics.events[event_name.to_sym]
end
protected
diff --git a/core/lib/spree/analytics.rb b/core/lib/spree/analytics.rb
new file mode 100644
index 0000000000..6a1d3d554d
--- /dev/null
+++ b/core/lib/spree/analytics.rb
@@ -0,0 +1,11 @@
+module Spree
+ module Analytics
+ def self.events
+ @@supported_events ||= Spree.analytics_events
+ end
+
+ def self.event_handlers
+ @@event_handlers ||= Spree.analytics_event_handlers
+ end
+ end
+end
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index beb0c8770b..059f813067 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -280,8 +280,20 @@ module Spree
Rails.application.config.spree.metafields
end
- def self.analytics
- Rails.application.config.spree.analytics
+ def self.analytics_events
+ Rails.application.config.spree.analytics_events
+ end
+
+ def self.analytics_events=(value)
+ Rails.application.config.spree.analytics_events = value
+ end
+
+ def self.analytics_event_handlers
+ Rails.application.config.spree.analytics_event_handlers
+ end
+
+ def self.analytics_event_handlers=(value)
+ Rails.application.config.spree.analytics_event_handlers = value
end
def self.integrations
@@ -361,6 +373,7 @@ require 'spree/money'
require 'spree/permitted_attributes'
require 'spree/service_module'
require 'spree/database_type_utilities'
+require 'spree/analytics'
require 'spree/core/partials'
require 'spree/core/importer'
diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb
index 7e6969cf49..6f734b6979 100644
--- a/core/lib/spree/core/engine.rb
+++ b/core/lib/spree/core/engine.rb
@@ -25,13 +25,13 @@ module Spree
:reports,
:translatable_resources,
:metafields,
- :analytics,
+ :analytics_events,
+ :analytics_event_handlers,
:integrations)
SpreeCalculators = Struct.new(:shipping_methods, :tax_rates, :promotion_actions_create_adjustments, :promotion_actions_create_item_adjustments)
PromoEnvironment = Struct.new(:rules, :actions)
SpreeValidators = Struct.new(:addresses)
MetafieldsEnvironment = Struct.new(:types, :enabled_resources)
- AnalyticsEnvironment = Struct.new(:events, :handlers)
isolate_namespace Spree
engine_name 'spree'
@@ -72,12 +72,6 @@ module Spree
app.config.spree.metafields.enabled_resources = []
end
- initializer 'spree.register.analytics' do |app|
- app.config.spree.analytics = AnalyticsEnvironment.new
- app.config.spree.analytics.events = {}
- app.config.spree.analytics.handlers = []
- end
-
# We need to define promotions rules here so extensions and existing apps
# can add their custom classes on their initializer files
initializer 'spree.promo.environment' do |app|
@@ -315,7 +309,7 @@ module Spree
Spree.user_class
]
- Rails.application.config.spree.analytics.events = {
+ Rails.application.config.spree.analytics_events = {
product_viewed: 'Product Viewed',
product_list_viewed: 'Product List Viewed',
product_searched: 'Product Searched',
@@ -340,7 +334,7 @@ module Spree
checkout_step_completed: 'Checkout Step Completed',
order_completed: 'Order Completed',
}
- Rails.application.config.spree.analytics.handlers = []
+ Rails.application.config.spree.analytics_event_handlers = []
Rails.application.config.spree.integrations = []
diff --git a/core/spec/lib/spree/core_environment_spec.rb b/core/spec/lib/spree/core_environment_spec.rb
index b22d167939..183eaf0c63 100644
--- a/core/spec/lib/spree/core_environment_spec.rb
+++ b/core/spec/lib/spree/core_environment_spec.rb
@@ -78,14 +78,14 @@ RSpec.describe 'Spree environment accessors' do
expect(Spree.metafields.enabled_resources).to be_an(Array)
end
- it 'provides access to analytics.events' do
- expect(Spree.analytics.events).to eq(Rails.application.config.spree.analytics.events)
- expect(Spree.analytics.events).to be_a(Hash)
+ it 'provides access to analytics_events' do
+ expect(Spree.analytics_events).to eq(Rails.application.config.spree.analytics_events)
+ expect(Spree.analytics_events).to be_a(Hash)
end
- it 'provides access to analytics.handlers' do
- expect(Spree.analytics.handlers).to eq(Rails.application.config.spree.analytics.handlers)
- expect(Spree.analytics.handlers).to be_an(Array)
+ it 'provides access to analytics_event_handlers' do
+ expect(Spree.analytics_event_handlers).to eq(Rails.application.config.spree.analytics_event_handlers)
+ expect(Spree.analytics_event_handlers).to be_an(Array)
end
it 'provides access to integrations' do
diff --git a/storefront/app/helpers/spree/analytics_helper.rb b/storefront/app/helpers/spree/analytics_helper.rb
index 82ebc42175..30d4f06568 100644
--- a/storefront/app/helpers/spree/analytics_helper.rb
+++ b/storefront/app/helpers/spree/analytics_helper.rb
@@ -1,7 +1,7 @@
module Spree
module AnalyticsHelper
def analytics_event_handlers
- @analytics_event_handlers ||= Spree.analytics.handlers.map do |handler|
+ @analytics_event_handlers ||= Spree::Analytics.event_handlers.map do |handler|
handler.new(user: try_spree_current_user, session: session, request: request, store: Spree::Store.current, visitor_id: visitor_id)
end
end
@@ -26,7 +26,7 @@ module Spree
end
def unsupported_event?(event_name)
- !Spree.analytics.events.key?(event_name.to_sym)
+ !Spree::Analytics.events.key?(event_name.to_sym)
end
end
end