Skip to content

Commit

Permalink
Merge pull request #55 from nnande/dispatch_segment_and_google_analyt…
Browse files Browse the repository at this point in the history
…ics_trackers_after_turbolinks_load

Dispatch segment and google analytics trackers after turbolinks load
  • Loading branch information
damianlegawiec authored Dec 3, 2020
2 parents 38c1712 + 377287d commit 38e58ca
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 102 deletions.
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 app/views/spree/shared/trackers/google_analytics/_purchase.js.erb
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 app/views/spree/shared/trackers/segment/_cart_viewed.js.erb
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 %>

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 app/views/spree/shared/trackers/segment/_order_complete.js.erb
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 %>
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 app/views/spree/shared/trackers/segment/_product_list_viewed.js.erb
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 app/views/spree/shared/trackers/segment/_product_viewed.js.erb
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 %>
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 %>

0 comments on commit 38e58ca

Please sign in to comment.