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.

Storefront: added analytics tracking of wishlist add/remove events

Damian Legawiec591bd117193e
diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb
index 72f1b13d14..e14d4ccd4b 100644
--- a/core/lib/spree/core/engine.rb
+++ b/core/lib/spree/core/engine.rb
@@ -240,6 +240,9 @@ module Spree
product_added: 'Product Added',
product_removed: 'Product Removed',
+ product_added_to_wishlist: 'Product Added to Wishlist',
+ product_removed_from_wishlist: 'Product Removed from Wishlist',
+
payment_info_entered: 'Payment Info Entered',
coupon_entered: 'Coupon Entered',
coupon_removed: 'Coupon Removed',
diff --git a/storefront/app/controllers/spree/account/wished_items_controller.rb b/storefront/app/controllers/spree/account/wished_items_controller.rb
index 03f24a5693..d40fb5d8c7 100644
--- a/storefront/app/controllers/spree/account/wished_items_controller.rb
+++ b/storefront/app/controllers/spree/account/wished_items_controller.rb
@@ -7,7 +7,9 @@ module Spree
variant = current_store.variants.find(wished_item_params[:variant_id])
@wished_item = current_wishlist.wished_items.build(variant: variant)
- unless @wished_item.save
+ if @wished_item.save
+ track_event('product_added_to_wishlist', variant: variant)
+ else
flash.now[:error] = @wished_item.errors.full_messages.to_sentence.strip
end
@@ -18,16 +20,16 @@ module Spree
end
def destroy
- @wished_item = current_wishlist.wished_items.find_by(variant_id: params[:id])
+ @wished_item = current_wishlist.wished_items.find_by!(variant_id: params[:id])
- if @wished_item.present?
- @wished_item.destroy
+ if @wished_item.destroy
+ track_event('product_removed_from_wishlist', variant: @wished_item.variant)
else
flash.now[:error] = Spree.t('storefront.wished_items.remove_error')
end
respond_to do |format|
- format.html { redirect_to account_wishlist_path }
+ format.html { redirect_to spree.account_wishlist_path }
format.turbo_stream
end
end
diff --git a/storefront/spec/controllers/spree/account/wished_items_controller_spec.rb b/storefront/spec/controllers/spree/account/wished_items_controller_spec.rb
index 7d4e4e7cf2..54c05f9b59 100644
--- a/storefront/spec/controllers/spree/account/wished_items_controller_spec.rb
+++ b/storefront/spec/controllers/spree/account/wished_items_controller_spec.rb
@@ -4,27 +4,30 @@ describe Spree::Account::WishedItemsController, type: :controller do
let(:store) { Spree::Store.default }
let(:variant) { create(:variant, price: 19.99) }
let(:format) { :html }
+ let(:user) { create(:user) }
+ let(:wishlist) { create(:wishlist, user: user, store: store, is_default: true) }
before do
allow(controller).to receive(:current_store).and_return(store)
allow(controller).to receive(:spree_login_path).and_return('/login')
+ allow(controller).to receive_messages try_spree_current_user: user
+ allow(controller).to receive(:current_wishlist).and_return(wishlist)
end
describe '#create' do
subject { post :create, params: { wished_item: { variant_id: variant.id } }, format: format }
- context 'when an item is already added to a wishlist' do
- let(:wishlist) { create(:wishlist, user: user, store: store) }
- let(:user) { create(:user) }
+ it 'adds a wished item to the wishlist' do
+ expect { subject }.to change(Spree::WishedItem, :count).by(1)
+ end
- let!(:wished_item) { create(:wished_item, variant: variant, wishlist: wishlist) }
+ it 'tracks the event' do
+ expect(controller).to receive(:track_event).with('product_added_to_wishlist', variant: variant)
+ subject
+ end
- before do
- allow(controller).to receive(:current_store).and_return(store)
- allow(controller).to receive_messages try_spree_current_user: user
- allow(controller).to receive_messages spree_current_user: user
- allow(controller).to receive(:current_wishlist).and_return(wishlist)
- end
+ context 'when an item is already added to a wishlist' do
+ let!(:wished_item) { create(:wished_item, variant: variant, wishlist: wishlist) }
it 'responds with an error' do
expect { subject }.to_not change(Spree::WishedItem, :count)
@@ -34,26 +37,22 @@ describe Spree::Account::WishedItemsController, type: :controller do
end
describe '#destroy' do
- let(:user) { create(:user) }
- let(:wishlist) { create(:wishlist, user: user, store: store) }
let!(:wished_item) { create(:wished_item, variant: variant, wishlist: wishlist) }
subject { delete :destroy, params: { id: variant.id }, format: format }
context 'when user is logged in' do
- before do
- allow(controller).to receive(:current_store).and_return(store)
- allow(controller).to receive_messages try_spree_current_user: user
- allow(controller).to receive_messages spree_current_user: user
- allow(controller).to receive(:current_wishlist).and_return(wishlist)
- end
-
it 'removes wished item' do
expect { subject }.to change { wishlist.wished_items.count }.by(-1)
expect(response).to have_http_status(302)
end
+ it 'tracks the event' do
+ expect(controller).to receive(:track_event).with('product_removed_from_wishlist', variant: variant)
+ subject
+ end
+
context 'with turbo_stream format' do
render_views
let(:format) { :turbo_stream }
@@ -67,27 +66,22 @@ describe Spree::Account::WishedItemsController, type: :controller do
end
context 'when user is not logged in' do
+ before do
+ allow(controller).to receive(:try_spree_current_user).and_return(nil)
+ end
+
it 'redirects to login page' do
expect(subject).to have_http_status(302)
end
end
context 'when an item is already removed from the wishlist' do
- let(:wishlist) { create(:wishlist, user: user, store: store) }
- let(:user) { create(:user) }
-
before do
wished_item.destroy!
-
- allow(controller).to receive(:current_store).and_return(store)
- allow(controller).to receive_messages try_spree_current_user: user
- allow(controller).to receive_messages spree_current_user: user
- allow(controller).to receive(:current_wishlist).and_return(wishlist)
end
it 'responds with an error' do
- expect { subject }.to_not change(Spree::WishedItem, :count)
- expect(flash[:error]).to eq('You already removed this item from your wishlist')
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end