Module: SpreeGoogleAnalytics::BaseHelper

Defined in:
app/helpers/spree_google_analytics/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#google_analytics_add_shipping_info_json(shipment) ⇒ Object



11
12
13
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 11

def google_analytics_add_shipping_info_json(shipment)
  SpreeGoogleAnalytics::CheckoutPresenter.new(order: shipment.order).call.merge({ shipping_tier: shipment.shipping_method&.name }).to_json.html_safe
end

#google_analytics_add_to_wishlist_event_json(variant) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 126

def google_analytics_add_to_wishlist_event_json(variant)
  if use_gtm?
    google_analytics_gtm_add_to_wishlist_event_json(variant)
  else
    {
      currency: current_currency,
      value: variant.amount_in(current_currency),
      items: [
        SpreeGoogleAnalytics::ProductPresenter.new(
          store_name: current_store.name,
          resource: variant,
          quantity: 1
        ).call
      ]
    }.to_json.html_safe
  end
end

#google_analytics_cart_event_json(line_item, quantity, position) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 66

def google_analytics_cart_event_json(line_item, quantity, position)
  if use_gtm?
    google_analytics_gtm_cart_event_json(line_item, quantity, position)
  else
    {
      currency: line_item.currency,
      value: line_item.amount.to_f,
      items: [
        SpreeGoogleAnalytics::ProductPresenter.new(
          resource: line_item,
          quantity: quantity,
          position: position
        ).call
      ]
    }.to_json.html_safe
  end
end

#google_analytics_checkout_jsonObject



161
162
163
164
165
166
167
168
169
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 161

def google_analytics_checkout_json
  return unless @order.present?

  if use_gtm?
    google_analytics_gtm_checkout_json
  else
    @google_analytics_checkout_json ||= SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call.to_json.html_safe
  end
end

#google_analytics_client_typeObject



3
4
5
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 3

def google_analytics_client_type
  @google_analytics_client_type ||= store_integration('google_analytics')&.preferred_client
end

#google_analytics_gtm_add_to_wishlist_event_json(variant) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 144

def google_analytics_gtm_add_to_wishlist_event_json(variant)
  {
    event: 'add_to_wishlist',
    ecommerce: {
      currency: current_currency,
      value: variant.amount_in(current_currency),
      items: [
        SpreeGoogleAnalytics::ProductPresenter.new(
          store_name: current_store.name,
          resource: variant,
          quantity: 1
        ).call
      ]
    }
  }.to_json.html_safe
end

#google_analytics_gtm_cart_event_json(line_item, quantity, position) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 84

def google_analytics_gtm_cart_event_json(line_item, quantity, position)
  {
    event: 'add_to_cart',
    ecommerce: {
      currency: line_item.currency,
      value: line_item.amount.to_f,
      items: [
        SpreeGoogleAnalytics::ProductPresenter.new(
          resource: line_item,
          quantity: quantity,
          position: position
        ).call
      ]
    }
  }.to_json.html_safe
end

#google_analytics_gtm_checkout_jsonObject



171
172
173
174
175
176
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 171

def google_analytics_gtm_checkout_json
  @google_analytics_gtm_checkout_json ||= {
    event: 'begin_checkout',
    ecommerce: SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call
  }.to_json.html_safe
end

#google_analytics_gtm_payment_jsonObject



205
206
207
208
209
210
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 205

def google_analytics_gtm_payment_json
  @google_analytics_gtm_payment_json ||= {
    event: 'add_payment_info',
    ecommerce: SpreeGoogleAnalytics::PaymentPresenter.new(order: @order).call
  }.to_json.html_safe
end

#google_analytics_gtm_purchase_jsonObject



188
189
190
191
192
193
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 188

def google_analytics_gtm_purchase_json
  @google_analytics_gtm_purchase_json ||= {
    event: 'purchase',
    ecommerce: SpreeGoogleAnalytics::OrderPresenter.new(order: @order).call
  }.to_json.html_safe
end

#google_analytics_gtm_remove_from_cart_event_json(line_item, quantity, position) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 109

def google_analytics_gtm_remove_from_cart_event_json(line_item, quantity, position)
  {
    event: 'remove_from_cart',
    ecommerce: {
      currency: line_item.currency,
      value: line_item.amount.to_f,
      items: [
        SpreeGoogleAnalytics::ProductPresenter.new(
          resource: line_item,
          quantity: quantity,
          position: position
        ).call
      ]
    }
  }.to_json.html_safe
end

#google_analytics_gtm_view_cart_jsonObject



222
223
224
225
226
227
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 222

def google_analytics_gtm_view_cart_json
  @google_analytics_gtm_view_cart_json ||= {
    event: 'view_cart',
    ecommerce: SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call
  }.to_json.html_safe
end

#google_analytics_gtm_view_item_json(variant) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 49

def google_analytics_gtm_view_item_json(variant)
  {
    event: 'view_item',
    ecommerce: {
      currency: current_currency,
      value: variant.amount_in(current_currency).to_f,
      items: [
        SpreeGoogleAnalytics::ProductPresenter.new(
          store_name: current_store.name,
          resource: variant,
          quantity: 1
        ).call
      ]
    }
  }.to_json.html_safe
end

#google_analytics_payment_jsonObject



195
196
197
198
199
200
201
202
203
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 195

def google_analytics_payment_json
  return unless @order.present?

  if use_gtm?
    google_analytics_gtm_payment_json
  else
    @google_analytics_payment_json ||= SpreeGoogleAnalytics::PaymentPresenter.new(order: @order).call.to_json.html_safe
  end
end

#google_analytics_purchase_jsonObject



178
179
180
181
182
183
184
185
186
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 178

def google_analytics_purchase_json
  return unless @order.present?

  if use_gtm?
    google_analytics_gtm_purchase_json
  else
    @google_analytics_purchase_json ||= SpreeGoogleAnalytics::OrderPresenter.new(order: @order).call.to_json.html_safe
  end
end

#google_analytics_remove_from_cart_event_json(line_item, quantity, position) ⇒ Object



101
102
103
104
105
106
107
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 101

def google_analytics_remove_from_cart_event_json(line_item, quantity, position)
  if use_gtm?
    google_analytics_gtm_remove_from_cart_event_json(line_item, quantity, position)
  else
    google_analytics_cart_event_json(line_item, quantity, position)
  end
end

#google_analytics_user_properties_json(user) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 15

def google_analytics_user_properties_json(user)
  return unless user.present?

  {
    sign_in_count: user.,
    created_at: user.created_at&.iso8601.to_s,
    completed_orders_count: user.orders.complete.count,
    completed_orders_total: user.orders.complete.sum(:total).to_f
  }.to_json.html_safe
end

#google_analytics_view_cart_jsonObject



212
213
214
215
216
217
218
219
220
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 212

def google_analytics_view_cart_json
  return unless @order.present?

  if use_gtm?
    google_analytics_gtm_view_cart_json
  else
    @google_analytics_view_cart_json ||= SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call.to_json.html_safe
  end
end

#google_analytics_view_item_json(variant = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 28

def google_analytics_view_item_json(variant = nil)
  variant ||= @selected_variant || @variant_from_options || @product&.default_variant
  return unless variant.present?

  if use_gtm?
    google_analytics_gtm_view_item_json(variant)
  else
    {
      currency: current_currency,
      value: variant.amount_in(current_currency).to_f,
      items: [
        SpreeGoogleAnalytics::ProductPresenter.new(
          store_name: current_store.name,
          resource: variant,
          quantity: 1
        ).call
      ]
    }.to_json.html_safe
  end
end

#use_gtm?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 7

def use_gtm?
  google_analytics_client_type == 'gtm'
end