-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 #3582 from nebulab/kennyadsl/guides/google-analytics
Guides: add basic Google Analytics integration
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
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
66 changes: 66 additions & 0 deletions
66
guides/source/developers/integrations/google-analytics.html.md
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Integrate Google Analytics into Solidus | ||
|
||
This guide explains how to set up Google Analytics Enhanced Ecommerce with gtag.js | ||
in Solidus. Please note that the instructions below assume that all Solidus views have | ||
been copied in the host application, as described in the [storefront | ||
customization guide][storefront-customization-guide]. | ||
|
||
|
||
Copy/paste the following code at `app/views/layouts/spree_application.html.rb`, | ||
immediately after the `<head>` tag. | ||
Replace `GA_MEASUREMENT_ID` with the ID of the Google Analytics property to which | ||
you want to send data. | ||
|
||
```html | ||
<!-- Global site tag (gtag.js) - Google Analytics --> | ||
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', 'GA_MEASUREMENT_ID'); | ||
<%= yield :gtag %> | ||
</script> | ||
``` | ||
|
||
This will send a page view event to your Google Analytics account for each page visited | ||
by your customers. | ||
|
||
To also send information about completed orders to Google Analytics, add the following | ||
code at `app/views/spree/orders/show.html.erb`: | ||
|
||
```erb | ||
<% if order_just_completed?(@order) %> | ||
<% content_for :gtag do %> | ||
<% gtag_purchase_items = [] %> | ||
<% order.line_items.each do |line_item| %> | ||
<% gtag_purchase_items.push({ | ||
'name': line_item.variant.name, | ||
'id': line_item.variant.sku, | ||
'price': line_item.total, | ||
'variant': line_item.variant.options_text, | ||
'quantity': line_item.quantity | ||
}) %> | ||
<% end %> | ||
gtag('event', 'purchase', { | ||
"transaction_id": '<%= order.number %>', | ||
"affiliation": '<%= current_store.name %>', | ||
"value": '<%= order.total %>', | ||
"currency": '<%= order.currency %>', | ||
"tax": '<%= order.tax_total %>', | ||
"shipping": '<%= order.ship_total %>', | ||
"items": <%= raw gtag_purchase_items.to_json %> | ||
}); | ||
<% end %> | ||
<% end %> | ||
``` | ||
|
||
Please, use [Enhanced ecommerce with gtag.js][gtag-documentation] as reference if you want to change | ||
information sent or you need to send other events to Google Analytics. | ||
|
||
[storefront-customization-guide]: ../customizations/customizing-storefront.html | ||
[gtag-documentation]: https://developers.google.com/analytics/devguides/collection/gtagjs/enhanced-ecommerce | ||
|