spree_analytics_trackers 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +2 -0
  5. data/.travis.yml +44 -0
  6. data/Appraisals +11 -0
  7. data/Gemfile +7 -0
  8. data/LICENSE +26 -0
  9. data/README.md +77 -0
  10. data/Rakefile +21 -0
  11. data/app/assets/javascripts/spree/frontend/add_to_cart_analytics.js +46 -0
  12. data/app/controllers/spree/admin/trackers_controller.rb +6 -0
  13. data/app/controllers/spree_analytics_trackers/store_controller_decorator.rb +14 -0
  14. data/app/helpers/spree/trackers_helper.rb +70 -0
  15. data/app/models/spree/tracker.rb +25 -0
  16. data/app/models/spree_analytics_trackers/user_decorator.rb +9 -0
  17. data/app/overrides/add_analytics_to_admin_sidebar.rb +8 -0
  18. data/app/overrides/add_cart_viewed_to_orders_edit.rb +10 -0
  19. data/app/overrides/add_checkout_step_viewed_to_checkout_edit.rb +18 -0
  20. data/app/overrides/add_google_analytics_page_view_to_spree_application.rb +18 -0
  21. data/app/overrides/add_google_analytics_to_spree_application.rb +17 -0
  22. data/app/overrides/add_google_purchase_to_orders_show.rb +9 -0
  23. data/app/overrides/add_order_complete_to_orders_show.rb +10 -0
  24. data/app/overrides/add_product_list_filtered_to_taxons_show.rb +10 -0
  25. data/app/overrides/add_product_list_viewed_to_products_list.rb +10 -0
  26. data/app/overrides/add_product_viewed_to_products_show.rb +10 -0
  27. data/app/overrides/add_products_searched_to_products_list.rb +10 -0
  28. data/app/overrides/add_segment_initializer_to_layout.rb +18 -0
  29. data/app/overrides/add_segment_page_viewed_to_layout.rb +18 -0
  30. data/app/presenters/spree_analytics_trackers/base_presenter.rb +14 -0
  31. data/app/presenters/spree_analytics_trackers/segment/order_presenter.rb +32 -0
  32. data/app/presenters/spree_analytics_trackers/segment/product_presenter.rb +40 -0
  33. data/app/views/spree/admin/shared/_analytics_sidebar_menu.html.erb +3 -0
  34. data/app/views/spree/admin/trackers/_form.html.erb +36 -0
  35. data/app/views/spree/admin/trackers/edit.html.erb +13 -0
  36. data/app/views/spree/admin/trackers/index.html.erb +40 -0
  37. data/app/views/spree/admin/trackers/new.html.erb +13 -0
  38. data/app/views/spree/shared/trackers/google_analytics/_checkout_step_viewed.js.erb +21 -0
  39. data/app/views/spree/shared/trackers/google_analytics/_initializer.js.erb +11 -0
  40. data/app/views/spree/shared/trackers/google_analytics/_page_viewed.js.erb +13 -0
  41. data/app/views/spree/shared/trackers/google_analytics/_purchase.js.erb +19 -0
  42. data/app/views/spree/shared/trackers/segment/_cart_viewed.js.erb +22 -0
  43. data/app/views/spree/shared/trackers/segment/_checkout_step_viewed.js.erb +14 -0
  44. data/app/views/spree/shared/trackers/segment/_initializer.js.erb +7 -0
  45. data/app/views/spree/shared/trackers/segment/_order_complete.js.erb +8 -0
  46. data/app/views/spree/shared/trackers/segment/_page_viewed.js.erb +7 -0
  47. data/app/views/spree/shared/trackers/segment/_product_list_filtered.js.erb +21 -0
  48. data/app/views/spree/shared/trackers/segment/_product_list_viewed.js.erb +17 -0
  49. data/app/views/spree/shared/trackers/segment/_product_viewed.js.erb +10 -0
  50. data/app/views/spree/shared/trackers/segment/_products_searched.js.erb +12 -0
  51. data/bin/rails +7 -0
  52. data/config/locales/en.yml +18 -0
  53. data/config/locales/zh-TW.yml +19 -0
  54. data/config/routes.rb +5 -0
  55. data/db/migrate/20171013160337_create_spree_trackers.rb +20 -0
  56. data/gemfiles/.bundle/config +2 -0
  57. data/gemfiles/spree_4_1.gemfile +9 -0
  58. data/gemfiles/spree_master.gemfile +9 -0
  59. data/lib/generators/spree_analytics_trackers/install/install_generator.rb +27 -0
  60. data/lib/spree_analytics_trackers.rb +5 -0
  61. data/lib/spree_analytics_trackers/engine.rb +20 -0
  62. data/lib/spree_analytics_trackers/factories.rb +9 -0
  63. data/lib/spree_analytics_trackers/version.rb +18 -0
  64. data/spec/features/admin/configuration/analytics_tracker_spec.rb +52 -0
  65. data/spec/features/products_spec.rb +315 -0
  66. data/spec/fixtures/thinking-cat.jpg +0 -0
  67. data/spec/models/spree/tracker_spec.rb +17 -0
  68. data/spec/presenters/spree_analytics_trackers/segment/order_presenter_spec.rb +34 -0
  69. data/spec/spec_helper.rb +18 -0
  70. data/spec/support/custom_products.rb +26 -0
  71. data/spree_analytics_trackers.gemspec +31 -0
  72. metadata +202 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6428462a2ec00ca1334068af805efecf38b751293f86623e818aca577cb7df14
4
+ data.tar.gz: 1bd89f20f2f4d945ecafa7b843dbcb2a167811858cc7f6e2ab16a3fb59980d27
5
+ SHA512:
6
+ metadata.gz: c42aed4a692ede105d5b39335baa86a02aab94168186a6221dbce0edcb452d15eec696556639834d30c7dfefdca4408c3a9c116f6ea14f171f8520da0953b35e
7
+ data.tar.gz: d38032fb537f05202f967c7777e30d2ed2f696b2733a39bd20fe560c9ec2e4024f9588dcd9b0a4e1d9ddd30aa2d66712d6aced167e0e7b8948f732529125dc67
@@ -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.0.rc1'
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,46 @@
1
+ //= require spree/frontend
2
+
3
+ function gaAddToCart(variant, quantity) {
4
+ gtag(
5
+ 'event',
6
+ 'add_to_cart',
7
+ {
8
+ id: variant.sku,
9
+ name: variant.name,
10
+ category: variant.category,
11
+ variant: variant.options_text,
12
+ brand: variant.brand,
13
+ price: variant.price,
14
+ quantity: quantity
15
+ }
16
+ );
17
+ }
18
+
19
+ function segmentAddtoCart(variant, quantity, currency) {
20
+ analytics.track('Product Added', {
21
+ product_id: variant.id,
22
+ sku: variant.sku,
23
+ category: variant.category,
24
+ name: variant.name,
25
+ brand: variant.brand,
26
+ price: variant.price,
27
+ currency: currency,
28
+ quantity: quantity
29
+ });
30
+ }
31
+
32
+ Spree.ready(function(){
33
+ $('body').on('product_add_to_cart', function(event) {
34
+ var variant = event.variant
35
+ var quantity = event.quantity_increment
36
+ var currency = event.cart.currency
37
+
38
+ if (typeof gtag !== 'undefined') {
39
+ gaAddToCart(variant, quantity)
40
+ }
41
+
42
+ if (typeof analytics !== 'undefined') {
43
+ segmentAddtoCart(variant, quantity, currency)
44
+ }
45
+ })
46
+ });
@@ -0,0 +1,6 @@
1
+ module Spree
2
+ module Admin
3
+ class TrackersController < ResourceController
4
+ end
5
+ end
6
+ 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,70 @@
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
+ end
70
+ end
@@ -0,0 +1,25 @@
1
+ module Spree
2
+ class Tracker < Spree::Base
3
+ TRACKING_ENGINES = %i(google_analytics segment).freeze
4
+ enum engine: TRACKING_ENGINES
5
+
6
+ after_commit :clear_cache
7
+
8
+ validates :analytics_id, presence: true, uniqueness: { scope: :engine, case_sensitive: false }
9
+
10
+ scope :active, -> { where(active: true) }
11
+
12
+ def self.current(engine = TRACKING_ENGINES.first)
13
+ tracker = Rails.cache.fetch("current_tracker/#{engine}") do
14
+ send(engine).active.first
15
+ end
16
+ tracker.analytics_id.present? ? tracker : nil if tracker
17
+ end
18
+
19
+ def clear_cache
20
+ TRACKING_ENGINES.each do |engine|
21
+ Rails.cache.delete("current_tracker/#{engine}")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module SpreeAnalyticsTrackers
2
+ module UserDecorator
3
+ def analytics_id
4
+ id
5
+ end
6
+ end
7
+ end
8
+
9
+ ::Spree.user_class.prepend(::SpreeAnalyticsTrackers::UserDecorator)
@@ -0,0 +1,8 @@
1
+ if Gem.loaded_specs['spree_core'].version >= Gem::Version.create('3.5.0')
2
+ Deface::Override.new(
3
+ virtual_path: 'spree/admin/shared/sub_menu/_configuration',
4
+ name: 'add_analytics_to_admin_sidebar',
5
+ insert_bottom: '[data-hook="admin_configurations_sidebar_menu"]',
6
+ partial: 'spree/admin/shared/analytics_sidebar_menu'
7
+ )
8
+ end
@@ -0,0 +1,10 @@
1
+ spree_version = Gem.loaded_specs['spree_core'].version
2
+ unless spree_version >= Gem::Version.create('3.4.0') && spree_version < Gem::Version.create('3.5.0')
3
+ Deface::Override.new(
4
+ virtual_path: 'spree/orders/edit',
5
+ name: 'add_cart_viewed_to_orders_edit',
6
+ insert_top: '[data-hook="cart_container"]',
7
+ partial: 'spree/shared/trackers/segment/cart_viewed.js',
8
+ original: 'e0a3dcdf759c4ec9fad085cd8a5360c8d68167f5'
9
+ )
10
+ end
@@ -0,0 +1,18 @@
1
+ spree_version = Gem.loaded_specs['spree_core'].version
2
+ unless spree_version >= Gem::Version.create('3.4.0') && spree_version < Gem::Version.create('3.5.0')
3
+ Deface::Override.new(
4
+ virtual_path: 'spree/checkout/edit',
5
+ name: 'add_segment_checkout_step_viewed_to_checkout_edit',
6
+ insert_after: '[data-hook="checkout_content"]',
7
+ partial: 'spree/shared/trackers/segment/checkout_step_viewed.js',
8
+ original: '806d2120a1ca1050fb093962fee10363c0944b0a'
9
+ )
10
+
11
+ Deface::Override.new(
12
+ virtual_path: 'spree/checkout/edit',
13
+ name: 'add_ga_checkout_step_viewed_to_checkout_edit',
14
+ insert_after: '[data-hook="checkout_content"]',
15
+ partial: 'spree/shared/trackers/google_analytics/checkout_step_viewed.js',
16
+ original: '0ac5c18a2db215bf8ba30470a0e5d69e00d0313a'
17
+ )
18
+ end