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
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.rbindex 72f1b13d14..e14d4ccd4b 100644--- a/core/lib/spree/core/engine.rb+++ b/core/lib/spree/core/engine.rb@@ -240,6 +240,9 @@ module Spreeproduct_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.rbindex 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 Spreevariant = 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)+ elseflash.now[:error] = @wished_item.errors.full_messages.to_sentence.stripend@@ -18,16 +20,16 @@ module Spreeenddef 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)elseflash.now[:error] = Spree.t('storefront.wished_items.remove_error')endrespond_to do |format|- format.html { redirect_to account_wishlist_path }+ format.html { redirect_to spree.account_wishlist_path }format.turbo_streamendenddiff --git a/storefront/spec/controllers/spree/account/wished_items_controller_spec.rb b/storefront/spec/controllers/spree/account/wished_items_controller_spec.rbindex 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 dolet(: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 doallow(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)enddescribe '#create' dosubject { 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' doexpect { subject }.to_not change(Spree::WishedItem, :count)@@ -34,26 +37,22 @@ describe Spree::Account::WishedItemsController, type: :controller doenddescribe '#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' doexpect { 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' dorender_viewslet(:format) { :turbo_stream }@@ -67,27 +66,22 @@ describe Spree::Account::WishedItemsController, type: :controller doendcontext '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' doexpect(subject).to have_http_status(302)endendcontext 'when an item is already removed from the wishlist' do- let(:wishlist) { create(:wishlist, user: user, store: store) }- let(:user) { create(:user) }-before dowished_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)endit '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)endendend