-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from nnande/dispatch_segment_and_google_analyt…
…ics_trackers_after_turbolinks_load Dispatch segment and google analytics trackers after turbolinks load
- Loading branch information
Showing
9 changed files
with
123 additions
and
102 deletions.
There are no files selected for viewing
29 changes: 15 additions & 14 deletions
29
app/views/spree/shared/trackers/google_analytics/_checkout_step_viewed.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
<% if @order.present? && !@order.completed? && Spree::Tracker.current(:google_analytics, current_store) %> | ||
<script> | ||
if (typeof gtag !== 'undefined') { | ||
clearGAplugins(); | ||
gtag('event', 'checkout_progress', { | ||
checkout_step: '<%= (@order.checkout_steps.index(@order.state) + 1) %>', | ||
items: [ | ||
<% @order.line_items.each do |line_item| %> | ||
<%= ga_line_item(line_item) %>, | ||
<% end %> | ||
], | ||
|
||
coupon: '<%= @order.promo_code %>', | ||
checkout_option: '<%= @order.state %>' | ||
}); | ||
}; | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof gtag !== 'undefined') { | ||
clearGAplugins(); | ||
gtag('event', 'checkout_progress', { | ||
checkout_step: '<%= (@order.checkout_steps.index(@order.state) + 1) %>', | ||
items: [ | ||
<% @order.line_items.each do |line_item| %> | ||
<%= ga_line_item(line_item) %>, | ||
<% end %> | ||
], | ||
coupon: '<%= @order.promo_code %>', | ||
checkout_option: '<%= @order.state %>' | ||
}); | ||
}; | ||
}); | ||
</script> | ||
<% end %> |
34 changes: 18 additions & 16 deletions
34
app/views/spree/shared/trackers/google_analytics/_purchase.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
<% if @order.present? && order_just_completed?(@order) && Spree::Tracker.current(:google_analytics, current_store) %> | ||
<script> | ||
if (typeof gtag !== 'undefined') { | ||
clearGAplugins(); | ||
gtag('event', 'purchase', { | ||
transaction_id: '<%= @order.number %>', | ||
value: <%= @order.total&.to_f %>, | ||
currency: '<%= @order.currency %>', | ||
tax: <%= @order.tax_total&.to_f %>, | ||
shipping: <%= @order.shipment_total&.to_f %>, | ||
coupon: '<%= @order.promo_code %>', | ||
items: [ | ||
<% @order.line_items.each do |line_item| %> | ||
<%= ga_line_item(line_item) %>, | ||
<% end %> | ||
] | ||
}); | ||
} | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof gtag !== 'undefined') { | ||
clearGAplugins(); | ||
gtag('event', 'purchase', { | ||
transaction_id: '<%= @order.number %>', | ||
value: <%= @order.total&.to_f %>, | ||
currency: '<%= @order.currency %>', | ||
tax: <%= @order.tax_total&.to_f %>, | ||
shipping: <%= @order.shipment_total&.to_f %>, | ||
coupon: '<%= @order.promo_code %>', | ||
items: [ | ||
<% @order.line_items.each do |line_item| %> | ||
<%= ga_line_item(line_item) %>, | ||
<% end %> | ||
] | ||
}); | ||
} | ||
}); | ||
</script> | ||
<% end %> |
39 changes: 22 additions & 17 deletions
39
app/views/spree/shared/trackers/segment/_cart_viewed.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
<% if @order.present? && !@order.complete? && Spree::Tracker.current(:segment, current_store) %> | ||
<script> | ||
if (typeof analytics !== 'undefined') { | ||
var segmentCartJson = { | ||
cart_id: '<%= @order.number %>', | ||
products: [ | ||
<% @order.line_items.each_with_index do |line_item, index| %> | ||
<%= product_for_segment( | ||
line_item.product, | ||
position: index+1, | ||
quantity: line_item.quantity, | ||
image: default_image_for_product_or_variant(line_item.variant) | ||
) %>, | ||
<% end %> | ||
] | ||
} | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof analytics !== 'undefined') { | ||
var segmentCartJson = { | ||
cart_id: '<%= @order.number %>', | ||
products: [ | ||
<% @order.line_items.each_with_index do |line_item, index| %> | ||
<%= | ||
product_for_segment( | ||
line_item.product, | ||
position: index+1, | ||
quantity: line_item.quantity, | ||
image: default_image_for_product_or_variant(line_item.variant) | ||
) | ||
%>, | ||
<% end %> | ||
] | ||
} | ||
|
||
analytics.track('Cart Viewed', segmentCartJson); | ||
analytics.page('Cart Viewed', segmentCartJson); | ||
} | ||
analytics.track('Cart Viewed', segmentCartJson); | ||
analytics.page('Cart Viewed', segmentCartJson); | ||
} | ||
}); | ||
</script> | ||
<% end %> | ||
|
27 changes: 17 additions & 10 deletions
27
app/views/spree/shared/trackers/segment/_checkout_step_viewed.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
<% if @order.present? && !@order.completed? && Spree::Tracker.current(:segment, current_store) %> | ||
<script> | ||
if (typeof analytics !== 'undefined') { | ||
analytics.track('Checkout Step Viewed', { | ||
checkout_id: '<%= @order.number %>', | ||
step: '<%= (@order.checkout_steps.index(@order.state) + 1) %>' | ||
}); | ||
analytics.page('Checkout Step Viewed', { | ||
checkout_id: '<%= @order.number %>', | ||
step: '<%= (@order.checkout_steps.index(@order.state) + 1) %>' | ||
}); | ||
} | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof analytics !== 'undefined') { | ||
analytics.track('Checkout Step Viewed', | ||
{ | ||
checkout_id: '<%= @order.number %>', | ||
step: '<%= (@order.checkout_steps.index(@order.state) + 1) %>' | ||
} | ||
); | ||
|
||
analytics.page('Checkout Step Viewed', | ||
{ | ||
checkout_id: '<%= @order.number %>', | ||
step: '<%= (@order.checkout_steps.index(@order.state) + 1) %>' | ||
} | ||
); | ||
} | ||
}); | ||
</script> | ||
<% end %> |
10 changes: 6 additions & 4 deletions
10
app/views/spree/shared/trackers/segment/_order_complete.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
<% if @order.present? && order_just_completed?(@order) && Spree::Tracker.current(:segment, current_store) %> | ||
<% order_json = SpreeAnalyticsTrackers::Segment::OrderPresenter.new(@order).to_json %> | ||
<script> | ||
if (typeof analytics !== 'undefined') { | ||
analytics.page('Order Completed', <%= order_json.html_safe %>); | ||
analytics.track('Order Completed', <%= order_json.html_safe %>); | ||
} | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof analytics !== 'undefined') { | ||
analytics.page('Order Completed', <%= order_json.html_safe %>); | ||
analytics.track('Order Completed', <%= order_json.html_safe %>); | ||
} | ||
}); | ||
</script> | ||
<% end %> |
34 changes: 18 additions & 16 deletions
34
app/views/spree/shared/trackers/segment/_product_list_filtered.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
<% if any_filtering_params? && Spree::Tracker.current(:segment, current_store) %> | ||
<script> | ||
if (typeof analytics !== 'undefined') { | ||
var segmentProductListFilteredJson = { | ||
filters: [ | ||
<% filtering_params_with_values.each do |type, value| %> | ||
<%= { type: type, value: value } .to_json.html_safe %>, | ||
<% end %> | ||
], | ||
products: [ | ||
<% @products.each_with_index do |product, index| %> | ||
<%= product_for_segment(product, position: index+1) %>, | ||
<% end %> | ||
] | ||
} | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof analytics !== 'undefined') { | ||
var segmentProductListFilteredJson = { | ||
filters: [ | ||
<% filtering_params_with_values.each do |type, value| %> | ||
<%= { type: type, value: value } .to_json.html_safe %>, | ||
<% end %> | ||
], | ||
products: [ | ||
<% @products.each_with_index do |product, index| %> | ||
<%= product_for_segment(product, position: index + 1) %>, | ||
<% end %> | ||
] | ||
} | ||
|
||
analytics.track('Product List Filtered', segmentProductListFilteredJson); | ||
analytics.page('Product List Filtered', segmentProductListFilteredJson); | ||
} | ||
analytics.track('Product List Filtered', segmentProductListFilteredJson); | ||
analytics.page('Product List Filtered', segmentProductListFilteredJson); | ||
} | ||
}); | ||
</script> | ||
<% end %> |
26 changes: 14 additions & 12 deletions
26
app/views/spree/shared/trackers/segment/_product_list_viewed.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
<% if defined?(products) && products.present? && Spree::Tracker.current(:segment, current_store) %> | ||
<script> | ||
if (typeof analytics !== 'undefined') { | ||
var segmentProductListViewedJson = { | ||
category: '<%= @taxon&.name %>', | ||
products: [ | ||
<% products.each_with_index do |product, index| %> | ||
<%= product_for_segment(product, position: index+1, image: default_image_for_product_or_variant(product)) %>, | ||
<% end %> | ||
] | ||
} | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof analytics !== 'undefined') { | ||
var segmentProductListViewedJson = { | ||
category: '<%= @taxon&.name %>', | ||
products: [ | ||
<% products.each_with_index do |product, index| %> | ||
<%= product_for_segment(product, position: index+1, image: default_image_for_product_or_variant(product)) %>, | ||
<% end %> | ||
] | ||
} | ||
|
||
analytics.track('Product List Viewed', segmentProductListViewedJson); | ||
analytics.page('Product List Viewed', segmentProductListViewedJson); | ||
} | ||
analytics.track('Product List Viewed', segmentProductListViewedJson); | ||
analytics.page('Product List Viewed', segmentProductListViewedJson); | ||
} | ||
}); | ||
</script> | ||
<% end %> |
12 changes: 7 additions & 5 deletions
12
app/views/spree/shared/trackers/segment/_product_viewed.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
<% if @product.present? && Spree::Tracker.current(:segment, current_store) %> | ||
<script> | ||
if (typeof analytics !== 'undefined') { | ||
var segmentProductViewed = <%= product_for_segment(@product, image: default_image_for_product_or_variant(@product)) %>; | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof analytics !== 'undefined') { | ||
var segmentProductViewed = <%= product_for_segment(@product, image: default_image_for_product_or_variant(@product)) %>; | ||
|
||
analytics.page('Product Viewed', segmentProductViewed); | ||
analytics.track('Product Viewed', segmentProductViewed); | ||
} | ||
analytics.page('Product Viewed', segmentProductViewed); | ||
analytics.track('Product Viewed', segmentProductViewed); | ||
} | ||
}); | ||
</script> | ||
<% end %> |
14 changes: 6 additions & 8 deletions
14
app/views/spree/shared/trackers/segment/_products_searched.js.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
<% if params.key?(:keywords) && params[:keywords].present? && Spree::Tracker.current(:segment, current_store) %> | ||
<script> | ||
if (typeof analytics !== 'undefined') { | ||
analytics.track('Products Searched', { | ||
query: '<%= params[:keywords] %>', | ||
}); | ||
analytics.page('Products Searched', { | ||
query: '<%= params[:keywords] %>', | ||
}); | ||
} | ||
window.addEventListener('turbolinks:load', function() { | ||
if (typeof analytics !== 'undefined') { | ||
analytics.track('Products Searched', { query: '<%= params[:keywords] %>' }); | ||
analytics.page('Products Searched', { query: '<%= params[:keywords] %>' }); | ||
} | ||
}); | ||
</script> | ||
<% end %> |