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



9
10
11
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 9

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



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

def google_analytics_add_to_wishlist_event_json(variant)
  {
    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_cart_event_json(line_item, quantity, position) ⇒ Object



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

def google_analytics_cart_event_json(line_item, quantity, position)
  {
    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_checkout_jsonObject



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

def google_analytics_checkout_json
  return unless @order.present?

  @google_analytics_checkout_json ||= SpreeGoogleAnalytics::CheckoutPresenter.new(order: @order).call.to_json.html_safe
end

#google_analytics_payment_jsonObject



24
25
26
27
28
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 24

def google_analytics_payment_json
  return unless @order.present?

  @google_analytics_payment_json ||= SpreeGoogleAnalytics::PaymentPresenter.new(order: @order).call.to_json.html_safe
end

#google_analytics_purchase_jsonObject



30
31
32
33
34
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 30

def google_analytics_purchase_json
  return unless @order.present?

  @google_analytics_purchase_json ||= SpreeGoogleAnalytics::OrderPresenter.new(order: @order).call.to_json.html_safe
end

#google_analytics_user_properties_json(user) ⇒ Object



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

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_item_json(variant = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/spree_google_analytics/base_helper.rb', line 36

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

  {
    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