ba_spree_google_analytics 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +10 -0
  3. data/README.md +105 -0
  4. data/Rakefile +25 -0
  5. data/app/.gitkeep +0 -0
  6. data/app/assets/config/spree_google_analytics_manifest.js +5 -0
  7. data/app/assets/images/integration_icons/google-analytics-logo.png +0 -0
  8. data/app/controllers/.gitkeep +0 -0
  9. data/app/controllers/spree_google_analytics/store_controller_decorator.rb +20 -0
  10. data/app/helpers/spree_google_analytics/base_helper.rb +229 -0
  11. data/app/javascript/spree_google_analytics/application.js +16 -0
  12. data/app/javascript/spree_google_analytics/controllers/spree_google_analytics_controller.js +41 -0
  13. data/app/models/.gitkeep +0 -0
  14. data/app/models/spree/integrations/google_analytics.rb +21 -0
  15. data/app/presenters/spree_google_analytics/checkout_presenter.rb +12 -0
  16. data/app/presenters/spree_google_analytics/order_presenter.rb +59 -0
  17. data/app/presenters/spree_google_analytics/payment_presenter.rb +7 -0
  18. data/app/presenters/spree_google_analytics/product_presenter.rb +54 -0
  19. data/app/serializers/.gitkeep +0 -0
  20. data/app/services/.gitkeep +0 -0
  21. data/app/views/spree/admin/integrations/forms/_google_analytics.html.erb +22 -0
  22. data/app/views/spree_google_analytics/_add_to_cart.html.erb +15 -0
  23. data/app/views/spree_google_analytics/_add_to_wishlist.erb +15 -0
  24. data/app/views/spree_google_analytics/_body_end.html.erb +125 -0
  25. data/app/views/spree_google_analytics/_body_start.html.erb +12 -0
  26. data/app/views/spree_google_analytics/_cart.html.erb +6 -0
  27. data/app/views/spree_google_analytics/_checkout_complete.html.erb +31 -0
  28. data/app/views/spree_google_analytics/_head.html.erb +57 -0
  29. data/app/views/spree_google_analytics/_product.html.erb +15 -0
  30. data/app/views/spree_google_analytics/_remove_from_cart.html.erb +15 -0
  31. data/config/importmap.rb +6 -0
  32. data/config/initializers/spree.rb +18 -0
  33. data/config/locales/en.yml +13 -0
  34. data/config/routes.rb +3 -0
  35. data/lib/ba_spree_google_analytics.rb +5 -0
  36. data/lib/generators/spree_google_analytics/install/install_generator.rb +20 -0
  37. data/lib/spree_google_analytics/configuration.rb +13 -0
  38. data/lib/spree_google_analytics/engine.rb +39 -0
  39. data/lib/spree_google_analytics/factories.rb +3 -0
  40. data/lib/spree_google_analytics/testing_support/factories/google_analytics_integration.rb +7 -0
  41. data/lib/spree_google_analytics/version.rb +7 -0
  42. data/lib/spree_google_analytics.rb +5 -0
  43. data/vendor/javascript/.keep +0 -0
  44. data/vendor/stylesheets/.keep +0 -0
  45. metadata +146 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6dd44703666ee47d616fe61162caabf687875a327db212846a1e76a4ab678660
4
+ data.tar.gz: 01b4fd620157874f6f26e859fde56bc871c279f951168b6339900625c8550a84
5
+ SHA512:
6
+ metadata.gz: 92561ca79edd0e6621bf1576d1689f0521d5fb0b9a0f4244b54bca5c7bf5e28968824933fa9bf795963df982a9246c9fb42a645ef67294e2ed9d7cd48966b21a
7
+ data.tar.gz: 9fa63bbb89a021037680225f1340c66484f197e409eee4b396f6e5f6650b9ccefeeb17a3fa58d668a333072061039e92e711ebbc6f8d6a676a25c279005c6948
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vendo Connect Inc.
4
+ Copyright (c) 2026 be agile Co., Ltd.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ > [!NOTE]
2
+ > For users using our default headless [storefront](https://github.com/spree/storefront), you don't need to install this extension, follow [Storefront README](https://github.com/spree/storefront) to setup GA/GTM via ENV variables.
3
+
4
+
5
+ # Google Analytics 4 integration for Spree Commerce
6
+
7
+ This is a Google Analytics 4 extension for [Spree Rails Storefront](https://github.com/spree/spree-rails-storefront).
8
+
9
+ This [Google Analytics 4 integration for Spree Commerce](https://spreecommerce.org/docs/integrations/analytics/google-analytics) allows you to track user behavior, sales performance, and marketing effectiveness across your store.
10
+
11
+ With minimal setup required, you can gain valuable insights into how visitors interact with your site, which can help you make informed decisions to improve conversions, user experience, and overall business strategy.
12
+
13
+ > [!NOTE]
14
+ > To set up the Google Analytics integration, you must have a Google Analytics account and an associated property.
15
+
16
+ ## Event Tracking
17
+
18
+ By default, the following events will be tracked in Google Analytics 4:
19
+ - page_view
20
+ - first_visit
21
+ - session_start
22
+ - user_engagement
23
+ - search
24
+ - add_payment_info
25
+ - add_shipping_info
26
+ - add_to_cart
27
+ - add_to_wishlist
28
+ - begin_checkout
29
+ - purchase
30
+ - remove_from_cart
31
+ - view_cart
32
+ - view_item
33
+
34
+ ## Installation
35
+
36
+ 1. Add this extension to your Gemfile with this line:
37
+
38
+ ```ruby
39
+ bundle add spree_google_analytics
40
+ ```
41
+
42
+ 2. Run the install generator
43
+
44
+ ```ruby
45
+ bundle exec rails g spree_google_analytics:install
46
+ ```
47
+
48
+ 3. Restart your server
49
+
50
+ If your server was running, restart it so that it can find the assets properly.
51
+
52
+ ## Setup
53
+
54
+ Please follow [our setup guide](https://spreecommerce.org/docs/integrations/analytics/google-analytics) to start tracking all events in Google Analytics dashboard.
55
+
56
+ ## Developing
57
+
58
+ 1. Create a dummy app
59
+
60
+ ```bash
61
+ bundle update
62
+ bundle exec rake test_app
63
+ ```
64
+
65
+ 2. Add your new code
66
+ 3. Run tests
67
+
68
+ ```bash
69
+ bundle exec rspec
70
+ ```
71
+
72
+ When testing your applications integration with this extension you may use it's factories.
73
+ Simply add this require statement to your spec_helper:
74
+
75
+ ```ruby
76
+ require 'spree_google_analytics/factories'
77
+ ```
78
+
79
+ ## Releasing a new version
80
+
81
+ ```shell
82
+ bundle exec gem bump -p -t
83
+ bundle exec gem release
84
+ ```
85
+
86
+ For more options please see [gem-release README](https://github.com/svenfuchs/gem-release)
87
+
88
+ ## Contributing
89
+
90
+ If you'd like to contribute, please take a look at the
91
+ [instructions](CONTRIBUTING.md) for installing dependencies and crafting a good
92
+ pull request.
93
+
94
+ Copyright (c) 2025 Vendo Connect Inc., Vendo Sp. z o.o., released under the MIT License
95
+
96
+ ## Join the Community
97
+
98
+ [Join our Slack](https://slack.spreecommerce.org) to meet other 6k+ community members and get some support.
99
+
100
+ ## Need more support?
101
+
102
+ [Contact us](https://spreecommerce.org/contact/) for enterprise support and custom development services. We offer:
103
+ * migrations and upgrades,
104
+ * delivering your Spree application,
105
+ * optimizing your Spree stack.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ require 'spree/testing_support/extension_rake'
6
+
7
+ RSpec::Core::RakeTask.new
8
+
9
+ task :default do
10
+ if Dir['spec/dummy'].empty?
11
+ Rake::Task[:test_app].invoke
12
+ Dir.chdir('../../')
13
+ end
14
+ Rake::Task[:spec].invoke
15
+ end
16
+
17
+ desc 'Generates a dummy app for testing'
18
+ task :test_app do
19
+ ENV['LIB_NAME'] = 'spree_google_analytics'
20
+ Rake::Task['extension:test_app'].execute(
21
+ install_storefront: true,
22
+ install_admin: true
23
+ )
24
+ system({ 'BUNDLE_GEMFILE' => File.expand_path('Gemfile', __dir__) }, 'bundle exec rails g spree_posts:install')
25
+ end
data/app/.gitkeep ADDED
File without changes
@@ -0,0 +1,5 @@
1
+ //= link_tree ../images
2
+ //= link spree_google_analytics/application.js
3
+ //= link_tree ../../javascript/spree_google_analytics/controllers .js
4
+ //= link_tree ../../../vendor/javascript .js
5
+ //= link_tree ../../../vendor/stylesheets .css
File without changes
@@ -0,0 +1,20 @@
1
+ module SpreeGoogleAnalytics
2
+ module StoreControllerDecorator
3
+ def self.prepended(base)
4
+ base.helper SpreeGoogleAnalytics::BaseHelper
5
+ end
6
+ end
7
+ end
8
+
9
+ Spree::StoreController.prepend(SpreeGoogleAnalytics::StoreControllerDecorator) if defined?(Spree::StoreController)
10
+
11
+ # include in Devise controllers
12
+ if defined?(Spree::UserSessionsController)
13
+ Spree::UserSessionsController.prepend(SpreeGoogleAnalytics::StoreControllerDecorator)
14
+ end
15
+ if defined?(Spree::UserRegistrationsController)
16
+ Spree::UserRegistrationsController.prepend(SpreeGoogleAnalytics::StoreControllerDecorator)
17
+ end
18
+ if defined?(Spree::UserPasswordsController)
19
+ Spree::UserPasswordsController.prepend(SpreeGoogleAnalytics::StoreControllerDecorator)
20
+ end
@@ -0,0 +1,229 @@
1
+ module SpreeGoogleAnalytics
2
+ module BaseHelper
3
+ def google_analytics_client_type
4
+ @google_analytics_client_type ||= store_integration('google_analytics')&.preferred_client
5
+ end
6
+
7
+ def use_gtm?
8
+ google_analytics_client_type == 'gtm'
9
+ end
10
+
11
+ def google_analytics_add_shipping_info_json(shipment)
12
+ SpreeGoogleAnalytics::CheckoutPresenter.new(order: shipment.order).call.merge({ shipping_tier: shipment.shipping_method&.name }).to_json.html_safe
13
+ end
14
+
15
+ def google_analytics_user_properties_json(user)
16
+ return unless user.present?
17
+
18
+ {
19
+ sign_in_count: user.sign_in_count,
20
+ created_at: user.created_at&.iso8601.to_s,
21
+ completed_orders_count: user.orders.complete.count,
22
+ completed_orders_total: user.orders.complete.sum(:total).to_f
23
+ }.to_json.html_safe
24
+ end
25
+
26
+
27
+
28
+ def google_analytics_view_item_json(variant = nil)
29
+ variant ||= @selected_variant || @variant_from_options || @product&.default_variant
30
+ return unless variant.present?
31
+
32
+ if use_gtm?
33
+ google_analytics_gtm_view_item_json(variant)
34
+ else
35
+ {
36
+ currency: current_currency,
37
+ value: variant.amount_in(current_currency).to_f,
38
+ items: [
39
+ SpreeGoogleAnalytics::ProductPresenter.new(
40
+ store_name: current_store.name,
41
+ resource: variant,
42
+ quantity: 1
43
+ ).call
44
+ ]
45
+ }.to_json.html_safe
46
+ end
47
+ end
48
+
49
+ def google_analytics_gtm_view_item_json(variant)
50
+ {
51
+ event: 'view_item',
52
+ ecommerce: {
53
+ currency: current_currency,
54
+ value: variant.amount_in(current_currency).to_f,
55
+ items: [
56
+ SpreeGoogleAnalytics::ProductPresenter.new(
57
+ store_name: current_store.name,
58
+ resource: variant,
59
+ quantity: 1
60
+ ).call
61
+ ]
62
+ }
63
+ }.to_json.html_safe
64
+ end
65
+
66
+ def google_analytics_cart_event_json(line_item, quantity, position)
67
+ if use_gtm?
68
+ google_analytics_gtm_cart_event_json(line_item, quantity, position)
69
+ else
70
+ {
71
+ currency: line_item.currency,
72
+ value: line_item.amount.to_f,
73
+ items: [
74
+ SpreeGoogleAnalytics::ProductPresenter.new(
75
+ resource: line_item,
76
+ quantity: quantity,
77
+ position: position
78
+ ).call
79
+ ]
80
+ }.to_json.html_safe
81
+ end
82
+ end
83
+
84
+ def google_analytics_gtm_cart_event_json(line_item, quantity, position)
85
+ {
86
+ event: 'add_to_cart',
87
+ ecommerce: {
88
+ currency: line_item.currency,
89
+ value: line_item.amount.to_f,
90
+ items: [
91
+ SpreeGoogleAnalytics::ProductPresenter.new(
92
+ resource: line_item,
93
+ quantity: quantity,
94
+ position: position
95
+ ).call
96
+ ]
97
+ }
98
+ }.to_json.html_safe
99
+ end
100
+
101
+ def google_analytics_remove_from_cart_event_json(line_item, quantity, position)
102
+ if use_gtm?
103
+ google_analytics_gtm_remove_from_cart_event_json(line_item, quantity, position)
104
+ else
105
+ google_analytics_cart_event_json(line_item, quantity, position)
106
+ end
107
+ end
108
+
109
+ def google_analytics_gtm_remove_from_cart_event_json(line_item, quantity, position)
110
+ {
111
+ event: 'remove_from_cart',
112
+ ecommerce: {
113
+ currency: line_item.currency,
114
+ value: line_item.amount.to_f,
115
+ items: [
116
+ SpreeGoogleAnalytics::ProductPresenter.new(
117
+ resource: line_item,
118
+ quantity: quantity,
119
+ position: position
120
+ ).call
121
+ ]
122
+ }
123
+ }.to_json.html_safe
124
+ end
125
+
126
+ def google_analytics_add_to_wishlist_event_json(variant)
127
+ if use_gtm?
128
+ google_analytics_gtm_add_to_wishlist_event_json(variant)
129
+ else
130
+ {
131
+ currency: current_currency,
132
+ value: variant.amount_in(current_currency),
133
+ items: [
134
+ SpreeGoogleAnalytics::ProductPresenter.new(
135
+ store_name: current_store.name,
136
+ resource: variant,
137
+ quantity: 1
138
+ ).call
139
+ ]
140
+ }.to_json.html_safe
141
+ end
142
+ end
143
+
144
+ def google_analytics_gtm_add_to_wishlist_event_json(variant)
145
+ {
146
+ event: 'add_to_wishlist',
147
+ ecommerce: {
148
+ currency: current_currency,
149
+ value: variant.amount_in(current_currency),
150
+ items: [
151
+ SpreeGoogleAnalytics::ProductPresenter.new(
152
+ store_name: current_store.name,
153
+ resource: variant,
154
+ quantity: 1
155
+ ).call
156
+ ]
157
+ }
158
+ }.to_json.html_safe
159
+ end
160
+
161
+ def google_analytics_checkout_json
162
+ return unless @order.present?
163
+
164
+ if use_gtm?
165
+ google_analytics_gtm_checkout_json
166
+ else
167
+ @google_analytics_checkout_json ||= SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call.to_json.html_safe
168
+ end
169
+ end
170
+
171
+ def google_analytics_gtm_checkout_json
172
+ @google_analytics_gtm_checkout_json ||= {
173
+ event: 'begin_checkout',
174
+ ecommerce: SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call
175
+ }.to_json.html_safe
176
+ end
177
+
178
+ def google_analytics_purchase_json
179
+ return unless @order.present?
180
+
181
+ if use_gtm?
182
+ google_analytics_gtm_purchase_json
183
+ else
184
+ @google_analytics_purchase_json ||= SpreeGoogleAnalytics::OrderPresenter.new(order: @order).call.to_json.html_safe
185
+ end
186
+ end
187
+
188
+ def google_analytics_gtm_purchase_json
189
+ @google_analytics_gtm_purchase_json ||= {
190
+ event: 'purchase',
191
+ ecommerce: SpreeGoogleAnalytics::OrderPresenter.new(order: @order).call
192
+ }.to_json.html_safe
193
+ end
194
+
195
+ def google_analytics_payment_json
196
+ return unless @order.present?
197
+
198
+ if use_gtm?
199
+ google_analytics_gtm_payment_json
200
+ else
201
+ @google_analytics_payment_json ||= SpreeGoogleAnalytics::PaymentPresenter.new(order: @order).call.to_json.html_safe
202
+ end
203
+ end
204
+
205
+ def google_analytics_gtm_payment_json
206
+ @google_analytics_gtm_payment_json ||= {
207
+ event: 'add_payment_info',
208
+ ecommerce: SpreeGoogleAnalytics::PaymentPresenter.new(order: @order).call
209
+ }.to_json.html_safe
210
+ end
211
+
212
+ def google_analytics_view_cart_json
213
+ return unless @order.present?
214
+
215
+ if use_gtm?
216
+ google_analytics_gtm_view_cart_json
217
+ else
218
+ @google_analytics_view_cart_json ||= SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call.to_json.html_safe
219
+ end
220
+ end
221
+
222
+ def google_analytics_gtm_view_cart_json
223
+ @google_analytics_gtm_view_cart_json ||= {
224
+ event: 'view_cart',
225
+ ecommerce: SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call
226
+ }.to_json.html_safe
227
+ end
228
+ end
229
+ end
@@ -0,0 +1,16 @@
1
+ import '@hotwired/turbo-rails'
2
+ import { Application } from '@hotwired/stimulus'
3
+
4
+ let application
5
+
6
+ if (typeof window.Stimulus === "undefined") {
7
+ application = Application.start()
8
+ application.debug = false
9
+ window.Stimulus = application
10
+ } else {
11
+ application = window.Stimulus
12
+ }
13
+
14
+ import SpreeGoogleAnalyticsController from 'spree_google_analytics/controllers/spree_google_analytics_controller'
15
+
16
+ application.register('spree_google_analytics', SpreeGoogleAnalyticsController)
@@ -0,0 +1,41 @@
1
+ import { Controller } from "@hotwired/stimulus";
2
+
3
+ // [#1063] Turbo Drive 遷移での page_view 計測。
4
+ //
5
+ // Spree のストアフロントは Turbo Drive でページ遷移するため、_head 内の
6
+ // gtag('config', ID) は初回ロード時に一度実行されるだけで、リンククリックによる
7
+ // 遷移では再実行されない。そのため標準の自動 page_view では初回ページしか
8
+ // 計測できない。
9
+ //
10
+ // このコントローラは turbo:load を購読し、遷移のたびに page_view を手動送信する。
11
+ // _head 側で send_page_view: false にしてあるので、初回ロードも含めてここから
12
+ // 一元的に送る(初回の二重送信は起きない)。
13
+ export default class extends Controller {
14
+ connect() {
15
+ // body 差し替えで connect が複数回呼ばれても多重購読しないようガード。
16
+ if (window.__spreeGAPageviewBound) return;
17
+ window.__spreeGAPageviewBound = true;
18
+
19
+ this.sendPageView = this.sendPageView.bind(this);
20
+ document.addEventListener("turbo:load", this.sendPageView);
21
+ }
22
+
23
+ sendPageView() {
24
+ const payload = {
25
+ page_location: window.location.href,
26
+ page_path: window.location.pathname + window.location.search,
27
+ page_title: document.title,
28
+ };
29
+
30
+ // GA4 (gtag.js)
31
+ if (typeof window.gtag === "function") {
32
+ window.gtag("event", "page_view", payload);
33
+ return;
34
+ }
35
+
36
+ // Google Tag Manager (dataLayer)
37
+ if (window.dataLayer && typeof window.dataLayer.push === "function") {
38
+ window.dataLayer.push({ event: "page_view", ...payload });
39
+ }
40
+ }
41
+ }
File without changes
@@ -0,0 +1,21 @@
1
+ module Spree
2
+ module Integrations
3
+ class GoogleAnalytics < Spree::Integration
4
+ preference :measurement_id, :string
5
+ preference :google_tag_manager_id, :string
6
+ preference :client, :string, default: 'ga4'
7
+
8
+ validates :preferred_measurement_id, presence: true, if: -> { preferred_client == 'ga4' }
9
+ validates :preferred_google_tag_manager_id, presence: true, if: -> { preferred_client == 'gtm' }
10
+ validates :preferred_client, inclusion: { in: %w[ga4 gtm] }
11
+
12
+ def self.integration_group
13
+ 'analytics'
14
+ end
15
+
16
+ def self.icon_path
17
+ 'integration_icons/google-analytics-logo.png'
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module SpreeGoogleAnalytics
2
+ class CheckoutPresenter < OrderPresenter
3
+ def call
4
+ {
5
+ currency: @order.currency,
6
+ value: @order.analytics_subtotal,
7
+ coupon: try_coupon_code,
8
+ items: products(@order)
9
+ }.merge(try_debug_mode)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,59 @@
1
+ module SpreeGoogleAnalytics
2
+ class OrderPresenter
3
+ include Spree::BaseHelper
4
+
5
+ def initialize(order:)
6
+ @order = order
7
+ end
8
+
9
+ def call
10
+ {
11
+ currency: @order.currency,
12
+ transaction_id: @order.number,
13
+ value: @order.analytics_subtotal,
14
+ coupon: try_coupon_code,
15
+ shipping: @order.shipment_total.to_f,
16
+ tax: @order.additional_tax_total.to_f,
17
+ items: products(@order)
18
+ }.merge(try_debug_mode)
19
+ .merge(gift_card_attributes)
20
+ end
21
+
22
+ private
23
+
24
+ def products(order)
25
+ return [] if order.line_items.empty?
26
+
27
+ product_includes = %i[taxons]
28
+ product_includes << :vendor if defined?(Spree::Vendor)
29
+
30
+ @products ||= order.line_items.includes(variant: { product: product_includes }).map.with_index do |line_item, index|
31
+ ProductPresenter.new(
32
+ resource: line_item,
33
+ quantity: line_item.quantity,
34
+ position: index + 1
35
+ ).call
36
+ end
37
+ end
38
+
39
+ def try_debug_mode
40
+ ENV['GA_DEBUG_MODE'].present? ? { debug_mode: 1 } : {}
41
+ end
42
+
43
+ def try_coupon_code
44
+ (@order.coupon_code || @order.try(:gift_card).try(:code)).to_s
45
+ end
46
+
47
+ def gift_card_attributes
48
+ return {} unless defined?(Spree::GiftCard)
49
+ return {} unless @order.gift_card.present?
50
+
51
+ {
52
+ gift_card_code: @order.gift_card&.code.to_s.upcase,
53
+ gift_card_amount: @order.gift_card_total.to_f,
54
+ gift_card_balance: @order.gift_card&.amount_remaining.to_f,
55
+ total_minus_store_credits: @order.total_minus_store_credits.to_f
56
+ }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ module SpreeGoogleAnalytics
2
+ class PaymentPresenter < CheckoutPresenter
3
+ def call
4
+ super.merge({ payment_type: @order.payments&.last&.payment_method&.name })
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,54 @@
1
+ module SpreeGoogleAnalytics
2
+ class ProductPresenter
3
+ include Spree::BaseHelper
4
+ include Spree::ProductsHelper
5
+
6
+ # @param resource [Spree::LineItem, Spree::Variant]
7
+ # @param quantity [Integer]
8
+ # @param position [Integer]
9
+ # @param store_name [String]
10
+ def initialize(resource:, quantity:, position: nil, store_name: nil)
11
+ @resource = resource
12
+ @quantity = quantity
13
+ @position = position
14
+ @store_name = store_name
15
+ end
16
+
17
+ # @return [Hash]
18
+ def call
19
+ {
20
+ item_id: @resource.sku.presence || @resource.product_id,
21
+ item_name: @resource.name,
22
+ affiliation: @resource.respond_to?(:order) && @resource.order&.store.present? ? @resource.order.store.name : @store_name,
23
+ discount: 0.00,
24
+ index: @position.to_s,
25
+ item_brand: @resource.product.present? ? @resource.product.brand_name.to_s : '',
26
+ item_category: taxons[0].to_s,
27
+ item_category2: taxons[1].to_s,
28
+ item_category3: taxons[2].to_s,
29
+ item_category4: taxons[3].to_s,
30
+ item_category5: taxons[4].to_s,
31
+ item_list_id: '',
32
+ item_list_name: '',
33
+ item_variant: @resource.try(:options_text),
34
+ location_id: '',
35
+ price: @resource.price.to_f,
36
+ quantity: @quantity
37
+ }.merge(try_promotion_codes_applied_to_line_item)
38
+ end
39
+
40
+ private
41
+
42
+ def try_promotion_codes_applied_to_line_item
43
+ return {} if @resource.class != Spree::LineItem
44
+
45
+ { coupon: @resource.adjustments.promotion.map do |adjustment|
46
+ adjustment.source.promotion.code
47
+ end.compact.uniq.join(',') }
48
+ end
49
+
50
+ def taxons
51
+ @taxons ||= @resource.product.main_taxon&.pretty_name.to_s.split('->').map(&:strip)
52
+ end
53
+ end
54
+ end
File without changes
File without changes
@@ -0,0 +1,22 @@
1
+ <div class="card mb-4">
2
+ <div class="card-body" data-controller="reveal">
3
+ <% if @integration.persisted? %>
4
+ <div class="alert alert-warning">
5
+ <%= Spree.t('admin.integrations.google_analytics.reports_warning') %>
6
+ </div>
7
+ <% end %>
8
+
9
+ <%= form.spree_collection_select "preferred_client", [['Google Analytics 4', 'ga4'], ['Google Tag Manager', 'gtm']], :last, :first, { selected: @integration.preferred_client, label: Spree.t('admin.integrations.google_analytics.client'), help: Spree.t('admin.integrations.google_analytics.client_help') }, { data: { action: 'change->reveal#toggle' } } %>
10
+
11
+ <div class="<%= class_names(hidden: @integration.preferred_client == 'gtm') %>" data-reveal-target="item">
12
+ <%= preference_field(@integration, form, 'measurement_id', i18n_scope: 'admin.integrations.google_analytics') %>
13
+ <%= external_link_to 'Where to find my Measurement ID?', 'https://support.google.com/analytics/answer/12270356' %>
14
+ </div>
15
+
16
+ <div class="<%= class_names(hidden: @integration.preferred_client == 'ga4') %>" data-reveal-target="item">
17
+ <%= preference_field(@integration, form, 'google_tag_manager_id', i18n_scope: 'admin.integrations.google_analytics') %>
18
+ <%= external_link_to 'Where to find my Google Tag Manager ID?', 'https://support.google.com/tagmanager/answer/6107028' %>
19
+ </div>
20
+ </div>
21
+ </div>
22
+
@@ -0,0 +1,15 @@
1
+ <% if store_integration('google_analytics').present? %>
2
+ <script>
3
+ <% safely do %>
4
+ <% if use_gtm? %>
5
+ if (window.dataLayer) {
6
+ dataLayer.push(<%= google_analytics_cart_event_json(@line_item, @line_item.quantity, @order.line_items.pluck(:id).index(@line_item.id) + 1) %>);
7
+ }
8
+ <% else %>
9
+ if (window.gtag) {
10
+ gtag('event', 'add_to_cart', <%= google_analytics_cart_event_json(@line_item, @line_item.quantity, @order.line_items.pluck(:id).index(@line_item.id) + 1) %>);
11
+ }
12
+ <% end %>
13
+ <% end %>
14
+ </script>
15
+ <% end %>