diff --git a/app/assets/javascripts/spree/frontend/remove_from_cart_analytics.js b/app/assets/javascripts/spree/frontend/remove_from_cart_analytics.js new file mode 100644 index 0000000..c67ecfb --- /dev/null +++ b/app/assets/javascripts/spree/frontend/remove_from_cart_analytics.js @@ -0,0 +1,34 @@ +//= require spree/frontend + +function gaRemoveFromCart(variant) { + clearGAplugins(); + gtag( + 'event', + 'remove_from_cart', + { + items: [{ + id: variant.sku, + name: variant.name, + price: variant.price, + variant: variant.options, + quantity: variant.quantity + }] + } + ); +} + +Spree.ready(function(){ + $('body').on('product_remove_from_cart', function(event) { + var variant = { + sku: event.variant_sku, + name: event.variant_name, + price: event.variant_price, + options: event.variant_options, + quantity: event.variant_quantity + } + + if (typeof gtag !== 'undefined') { + gaRemoveFromCart(variant) + } + }) +}) diff --git a/lib/generators/spree_analytics_trackers/install/install_generator.rb b/lib/generators/spree_analytics_trackers/install/install_generator.rb index 5f1b7e0..5925445 100644 --- a/lib/generators/spree_analytics_trackers/install/install_generator.rb +++ b/lib/generators/spree_analytics_trackers/install/install_generator.rb @@ -8,6 +8,7 @@ def add_javascripts return unless File.file?(javascripts_path) append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/add_to_cart_analytics\n" + append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/remove_from_cart_analytics\n" end def add_migrations