spree_analytics_trackers 1.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.bundle/config +2 -0
  3. data/.gitignore +21 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +2 -0
  6. data/.travis.yml +44 -0
  7. data/Appraisals +11 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE +26 -0
  10. data/README.md +77 -0
  11. data/Rakefile +21 -0
  12. data/app/assets/javascripts/spree/frontend/add_to_cart_analytics.js +50 -0
  13. data/app/assets/javascripts/spree/frontend/remove_from_cart_analytics.js +34 -0
  14. data/app/controllers/spree/admin/trackers_controller.rb +14 -0
  15. data/app/controllers/spree_analytics_trackers/store_controller_decorator.rb +14 -0
  16. data/app/helpers/spree/trackers_helper.rb +86 -0
  17. data/app/models/spree/tracker.rb +31 -0
  18. data/app/models/spree_analytics_trackers/user_decorator.rb +9 -0
  19. data/app/overrides/add_analytics_to_admin_sidebar.rb +8 -0
  20. data/app/overrides/add_cart_viewed_to_orders_edit.rb +10 -0
  21. data/app/overrides/add_checkout_step_viewed_to_checkout_edit.rb +18 -0
  22. data/app/overrides/add_google_analytics_page_view_to_spree_application.rb +10 -0
  23. data/app/overrides/add_google_analytics_to_spree_application.rb +9 -0
  24. data/app/overrides/add_google_purchase_to_orders_show.rb +9 -0
  25. data/app/overrides/add_order_complete_to_orders_show.rb +10 -0
  26. data/app/overrides/add_product_list_filtered_to_taxons_show.rb +10 -0
  27. data/app/overrides/add_product_list_viewed_to_products_list.rb +10 -0
  28. data/app/overrides/add_product_viewed_to_products_show.rb +10 -0
  29. data/app/overrides/add_products_searched_to_products_list.rb +10 -0
  30. data/app/overrides/add_segment_initializer_to_layout.rb +10 -0
  31. data/app/overrides/add_segment_page_viewed_to_layout.rb +10 -0
  32. data/app/presenters/spree_analytics_trackers/base_presenter.rb +14 -0
  33. data/app/presenters/spree_analytics_trackers/segment/order_presenter.rb +32 -0
  34. data/app/presenters/spree_analytics_trackers/segment/product_presenter.rb +40 -0
  35. data/app/views/spree/admin/shared/_analytics_sidebar_menu.html.erb +3 -0
  36. data/app/views/spree/admin/trackers/_form.html.erb +42 -0
  37. data/app/views/spree/admin/trackers/edit.html.erb +13 -0
  38. data/app/views/spree/admin/trackers/index.html.erb +42 -0
  39. data/app/views/spree/admin/trackers/new.html.erb +13 -0
  40. data/app/views/spree/shared/trackers/google_analytics/_checkout_step_viewed.js.erb +19 -0
  41. data/app/views/spree/shared/trackers/google_analytics/_initializer.js.erb +25 -0
  42. data/app/views/spree/shared/trackers/google_analytics/_page_viewed.js.erb +13 -0
  43. data/app/views/spree/shared/trackers/google_analytics/_purchase.js.erb +22 -0
  44. data/app/views/spree/shared/trackers/segment/_cart_viewed.js.erb +27 -0
  45. data/app/views/spree/shared/trackers/segment/_checkout_step_viewed.js.erb +21 -0
  46. data/app/views/spree/shared/trackers/segment/_initializer.js.erb +7 -0
  47. data/app/views/spree/shared/trackers/segment/_order_complete.js.erb +11 -0
  48. data/app/views/spree/shared/trackers/segment/_page_viewed.js.erb +7 -0
  49. data/app/views/spree/shared/trackers/segment/_product_list_filtered.js.erb +23 -0
  50. data/app/views/spree/shared/trackers/segment/_product_list_viewed.js.erb +19 -0
  51. data/app/views/spree/shared/trackers/segment/_product_viewed.js.erb +12 -0
  52. data/app/views/spree/shared/trackers/segment/_products_searched.js.erb +10 -0
  53. data/bin/rails +7 -0
  54. data/config/locales/en.yml +18 -0
  55. data/config/locales/zh-TW.yml +19 -0
  56. data/config/routes.rb +5 -0
  57. data/db/migrate/20171013160337_create_spree_trackers.rb +20 -0
  58. data/db/migrate/20200721163729_add_store_id_to_spree_trackers.rb +14 -0
  59. data/gemfiles/.bundle/config +2 -0
  60. data/gemfiles/spree_4_1.gemfile +9 -0
  61. data/gemfiles/spree_master.gemfile +9 -0
  62. data/lib/generators/spree_analytics_trackers/install/install_generator.rb +28 -0
  63. data/lib/spree_analytics_trackers.rb +5 -0
  64. data/lib/spree_analytics_trackers/engine.rb +20 -0
  65. data/lib/spree_analytics_trackers/factories.rb +10 -0
  66. data/lib/spree_analytics_trackers/version.rb +17 -0
  67. data/spec/features/admin/configuration/analytics_tracker_spec.rb +61 -0
  68. data/spec/features/products_spec.rb +315 -0
  69. data/spec/fixtures/thinking-cat.jpg +0 -0
  70. data/spec/models/spree/tracker_spec.rb +32 -0
  71. data/spec/presenters/spree_analytics_trackers/segment/order_presenter_spec.rb +34 -0
  72. data/spec/spec_helper.rb +18 -0
  73. data/spec/support/custom_products.rb +26 -0
  74. data/spree_analytics_trackers.gemspec +31 -0
  75. metadata +98 -200
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48c88ea88bf027af900280e3c8b35acf53278b197fd1b5d52009a6c281745727
4
- data.tar.gz: 93b920ab1f3a819701c598af63a6714986d90653f524dabf49610b1079c20e78
3
+ metadata.gz: 502cf1fa8e70cb9ceb4e1b24ec08c257cb5314249e5ab3403aa61ae463fb69fc
4
+ data.tar.gz: 4c44fd4f1c8172a51499a1e4bcef65538e185ad2c8ace20728a05c28e8347542
5
5
  SHA512:
6
- metadata.gz: 8a4693c38576c0bd279305648343fbe87d82be9d0583a94e4c1368266fb8374377e414006aa7eed9f919d9d4ca2a06f3515f3bc6e382158a2937cb68b4f9e561
7
- data.tar.gz: 758080aec33605cfb61d6517ff477b0db916fb6bea5ecba2609ca655d61a338d3edf8cba8fe0fc75cb12b3edd1aa070cc702c60399d46e20c3ed4472ab5ffc94
6
+ metadata.gz: 7824a5e18c3a7d0cd5afdb714f5ef4f727acfbc13e542ad7d33451b5478db03a530ae17dacb48a2dfc799084b3fd4a266417a6c6dfa2dd58d6d98a01c4625c2e
7
+ data.tar.gz: 54f3aeb09eb35e8c0a285778b5e2c91a57dcdd5971b618343a006fcc8a5dbaf7073fbcf902d540c52788d630848992f8e83ff4ec5511dde26295ea64a9ffa959
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_JOBS: "8"
@@ -0,0 +1,21 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .localeapp/locales
7
+ .project
8
+ coverage
9
+ default
10
+ Gemfile.lock
11
+ tmp
12
+ nbproject
13
+ pkg
14
+ *.sw?
15
+ spec/dummy
16
+ .rvmrc
17
+ .sass-cache
18
+ public/spree
19
+ .ruby-version
20
+ .ruby-gemset
21
+ gemfiles/*.gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ -r spec_helper
3
+ -f documentation
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
@@ -0,0 +1,44 @@
1
+ os: linux
2
+ dist: bionic
3
+
4
+ addons:
5
+ apt:
6
+ sources:
7
+ - google-chrome
8
+ packages:
9
+ - google-chrome-stable
10
+
11
+ services:
12
+ - mysql
13
+ - postgresql
14
+
15
+ language: ruby
16
+
17
+ rvm:
18
+ - 2.5
19
+ - 2.6
20
+
21
+ env:
22
+ - DB=mysql
23
+ - DB=postgres
24
+
25
+ gemfile:
26
+ - gemfiles/spree_4_1.gemfile
27
+ - gemfiles/spree_master.gemfile
28
+
29
+ jobs:
30
+ allow_failures:
31
+ - gemfile: gemfiles/spree_master.gemfile
32
+
33
+ before_install:
34
+ - mysql -u root -e "GRANT ALL ON *.* TO 'travis'@'%';"
35
+
36
+ before_script:
37
+ - CHROME_MAIN_VERSION=`google-chrome-stable --version | sed -E 's/(^Google Chrome |\.[0-9]+ )//g'`
38
+ - CHROMEDRIVER_VERSION=`curl -s "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$CHROME_MAIN_VERSION"`
39
+ - curl "https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip" -O
40
+ - unzip chromedriver_linux64.zip -d ~/bin
41
+
42
+ script:
43
+ - bundle exec rake test_app
44
+ - bundle exec rake spec
@@ -0,0 +1,11 @@
1
+ appraise 'spree-4-1' do
2
+ gem 'spree', '~> 4.1.0'
3
+ gem 'spree_auth_devise', '~> 4.1.0'
4
+ gem 'rails-controller-testing'
5
+ end
6
+
7
+ appraise 'spree-master' do
8
+ gem 'spree', github: 'spree/spree', branch: 'master'
9
+ gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: 'master'
10
+ gem 'rails-controller-testing'
11
+ end
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'spree', github: 'spree/spree', branch: 'master'
4
+ gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: 'master'
5
+ gem 'rails-controller-testing'
6
+
7
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2017-2020 Spark Solutions Sp. z o.o.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification,
5
+ are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name Spree nor the names of its contributors may be used to
13
+ endorse or promote products derived from this software without specific
14
+ prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,77 @@
1
+ # Spree Analytics Trackers
2
+
3
+ [![Build Status](https://travis-ci.org/spree-contrib/spree_analytics_trackers.svg?branch=master)](https://travis-ci.org/spree-contrib/spree_analytics_trackers)
4
+
5
+ This extensions adds support for [Google Analytics Enhanced Ecommerce](https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce)
6
+ and [Segment Ecommerce](https://segment.com/docs/spec/ecommerce/v2/) tracking to [Spree Commerce](https://spreecommerce.org) stores.
7
+
8
+ Via Segment.com you can plug-in also a lot of additional vendors and services like:
9
+
10
+ * Mixpanel
11
+ * Kissmetrics
12
+ * Google Tag Manager
13
+ * Facebook Pixels
14
+ * Google Adwords remarketing
15
+ * [and hundreds others](https://segment.com/catalog#integrations/all)
16
+
17
+ No additional development required.
18
+
19
+ ## Installation
20
+
21
+ 1. Add this extension to your Gemfile with this line:
22
+
23
+ ```ruby
24
+ gem 'spree_analytics_trackers', '~> 2.0'
25
+ ```
26
+
27
+ 2. Install the gem using Bundler:
28
+
29
+ ```ruby
30
+ bundle install
31
+ ```
32
+
33
+ 3. Copy & run migrations
34
+
35
+ ```ruby
36
+ bundle exec rails g spree_analytics_trackers:install
37
+ ```
38
+
39
+ 4. Restart your server
40
+
41
+ If your server was running, restart it so that it can find the assets properly.
42
+
43
+ ## Testing
44
+
45
+ First bundle your dependencies, then run `rake`. `rake` will default to building the dummy app if it does not exist, then it will run specs. The dummy app can be regenerated by using `rake test_app`.
46
+
47
+ ```shell
48
+ bundle
49
+ bundle exec rake
50
+ ```
51
+
52
+ When testing your applications integration with this extension you may use it's factories.
53
+ Simply add this require statement to your spec_helper:
54
+
55
+ ```ruby
56
+ require 'spree_analytics_trackers/factories'
57
+ ```
58
+
59
+ ## License
60
+
61
+ Spree Analytics Trackers is copyright © 2017-2020
62
+ [Spark Solutions Sp. z o.o.][spark]. It is free software,
63
+ and may be redistributed under the terms specified in the
64
+ [LICENCE](LICENSE) file.
65
+
66
+ [LICENSE]: https://github.com/spree-contrib/spree_analytics_trackers/blob/master/LICENSE
67
+
68
+ ## About Spark Solutions
69
+
70
+ [![Spark Solutions](http://sparksolutions.co/wp-content/uploads/2015/01/logo-ss-tr-221x100.png)][spark]
71
+
72
+ Spree Analytics Trackers is maintained by [Spark Solutions Sp. z o.o.](http://sparksolutions.co?utm_source=github).
73
+
74
+ We are passionate about open source software.
75
+ We are [available for hire][spark].
76
+
77
+ [spark]:http://sparksolutions.co?utm_source=github
@@ -0,0 +1,21 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ require 'spree/testing_support/extension_rake'
6
+
7
+ RSpec::Core::RakeTask.new
8
+
9
+ task :default do
10
+ if Dir["spec/dummy"].empty?
11
+ Rake::Task[:test_app].invoke
12
+ Dir.chdir("../../")
13
+ end
14
+ Rake::Task[:spec].invoke
15
+ end
16
+
17
+ desc 'Generates a dummy app for testing'
18
+ task :test_app do
19
+ ENV['LIB_NAME'] = 'spree_analytics_trackers'
20
+ Rake::Task['extension:test_app'].invoke
21
+ end
@@ -0,0 +1,50 @@
1
+ //= require spree/frontend
2
+
3
+ function gaAddToCart(variant, quantity) {
4
+ clearGAplugins();
5
+ var price = typeof variant.price === 'object' ? variant.price.amount : variant.price
6
+ gtag(
7
+ 'event',
8
+ 'add_to_cart',
9
+ {
10
+ items: [{
11
+ id: variant.sku,
12
+ name: variant.name,
13
+ category: variant.category,
14
+ variant: variant.options_text,
15
+ brand: variant.brand,
16
+ price: price,
17
+ quantity: quantity
18
+ }]
19
+ }
20
+ );
21
+ }
22
+
23
+ function segmentAddtoCart(variant, quantity, currency) {
24
+ analytics.track('Product Added', {
25
+ product_id: variant.id,
26
+ sku: variant.sku,
27
+ category: variant.category,
28
+ name: variant.name,
29
+ brand: variant.brand,
30
+ price: variant.price,
31
+ currency: currency,
32
+ quantity: quantity
33
+ });
34
+ }
35
+
36
+ Spree.ready(function(){
37
+ $('body').on('product_add_to_cart', function(event) {
38
+ var variant = event.variant
39
+ var quantity = event.quantity_increment
40
+ var currency = event.cart.currency
41
+
42
+ if (typeof gtag !== 'undefined') {
43
+ gaAddToCart(variant, quantity)
44
+ }
45
+
46
+ if (typeof analytics !== 'undefined') {
47
+ segmentAddtoCart(variant, quantity, currency)
48
+ }
49
+ })
50
+ });
@@ -0,0 +1,34 @@
1
+ //= require spree/frontend
2
+
3
+ function gaRemoveFromCart(variant) {
4
+ clearGAplugins();
5
+ gtag(
6
+ 'event',
7
+ 'remove_from_cart',
8
+ {
9
+ items: [{
10
+ id: variant.sku,
11
+ name: variant.name,
12
+ price: variant.price,
13
+ variant: variant.options,
14
+ quantity: variant.quantity
15
+ }]
16
+ }
17
+ );
18
+ }
19
+
20
+ Spree.ready(function(){
21
+ $('body').on('product_remove_from_cart', function(event) {
22
+ var variant = {
23
+ sku: event.variant_sku,
24
+ name: event.variant_name,
25
+ price: event.variant_price,
26
+ options: event.variant_options,
27
+ quantity: event.variant_quantity
28
+ }
29
+
30
+ if (typeof gtag !== 'undefined') {
31
+ gaRemoveFromCart(variant)
32
+ }
33
+ })
34
+ })
@@ -0,0 +1,14 @@
1
+ module Spree
2
+ module Admin
3
+ class TrackersController < ResourceController
4
+ before_action :load_data
5
+
6
+ private
7
+
8
+ def load_data
9
+ @engines = Spree::Tracker.engines.keys.sort.map { |k| [k.humanize, k] }
10
+ @stores = Spree::Store.all
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module SpreeAnalyticsTrackers
2
+ module StoreControllerDecorator
3
+ def self.included(base)
4
+ base.include ::Spree::BaseHelper
5
+ base.include ::Spree::TrackersHelper
6
+
7
+ base.helper_method *::Spree::TrackersHelper.public_instance_methods
8
+ end
9
+ end
10
+ end
11
+
12
+ if defined?(Spree::StoreController)
13
+ ::Spree::StoreController.include(::SpreeAnalyticsTrackers::StoreControllerDecorator)
14
+ end
@@ -0,0 +1,86 @@
1
+ module Spree
2
+ module TrackersHelper
3
+ def product_for_segment(product, optional = {})
4
+ cache_key = [
5
+ 'spree-segment-product',
6
+ I18n.locale,
7
+ current_currency,
8
+ product.cache_key_with_version
9
+ ].compact.join('/')
10
+
11
+ product_hash = Rails.cache.fetch(cache_key) do
12
+ {
13
+ product_id: product.id,
14
+ sku: product.sku,
15
+ category: product.category&.name,
16
+ name: product.name,
17
+ brand: product.brand&.name,
18
+ price: product.price_in(current_currency).amount&.to_f,
19
+ currency: current_currency,
20
+ url: spree.product_url(product)
21
+ }
22
+ end
23
+
24
+ product_hash.tap do |hash|
25
+ hash[:image_url] = default_image_for_product_or_variant(product)
26
+ end.merge(optional).to_json.html_safe
27
+ end
28
+
29
+ def ga_line_item(line_item)
30
+ variant = line_item.variant
31
+
32
+ cache_key = [
33
+ 'spree-ga-line-item',
34
+ I18n.locale,
35
+ current_currency,
36
+ line_item.cache_key_with_version,
37
+ variant.cache_key_with_version
38
+ ].compact.join('/')
39
+
40
+ Rails.cache.fetch(cache_key) do
41
+ product = line_item.product
42
+ {
43
+ id: variant.sku,
44
+ name: variant.name,
45
+ category: product.category&.name,
46
+ variant: variant.options_text,
47
+ brand: product.brand&.name,
48
+ quantity: line_item.quantity,
49
+ price: variant.price_in(current_currency).amount&.to_f
50
+ }.to_json.html_safe
51
+ end
52
+ end
53
+
54
+ def filtering_param_present?(param)
55
+ params.key?(param) && params.fetch(param).present?
56
+ end
57
+
58
+ def any_filtering_params?
59
+ filtering_params.any? { |p| filtering_param_present?(p) }
60
+ end
61
+
62
+ def filtering_params_with_values
63
+ params_with_values = {}
64
+ filtering_params.each do |param|
65
+ params_with_values[param] = params.fetch(param) if filtering_param_present?(param)
66
+ end
67
+ params_with_values
68
+ end
69
+
70
+ def segment_tracker
71
+ @segment_tracker ||= Spree::Tracker.current(:segment, current_store)
72
+ end
73
+
74
+ def segment_enabled?
75
+ segment_tracker.present?
76
+ end
77
+
78
+ def ga_tracker
79
+ @ga_tracker ||= Spree::Tracker.current(:google_analytics, current_store)
80
+ end
81
+
82
+ def ga_enabled?
83
+ ga_tracker.present?
84
+ end
85
+ end
86
+ end