workarea-google_tag_manager 4.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rubocop.yml +37 -0
  4. data/CHANGELOG.md +100 -0
  5. data/Gemfile +14 -0
  6. data/README.md +339 -0
  7. data/Rakefile +65 -0
  8. data/app/assets/javascripts/workarea/storefront/google_tag_manager/modules/adapter.js +233 -0
  9. data/app/views/workarea/storefront/google_tag_manager/_no_script.html.haml +3 -0
  10. data/app/views/workarea/storefront/google_tag_manager/_script.html.haml +3 -0
  11. data/bin/rails +18 -0
  12. data/config/initializers/appends.rb +16 -0
  13. data/config/initializers/workarea.rb +3 -0
  14. data/lib/workarea/google_tag_manager.rb +60 -0
  15. data/lib/workarea/google_tag_manager/engine.rb +8 -0
  16. data/lib/workarea/google_tag_manager/version.rb +5 -0
  17. data/script/admin_ci +9 -0
  18. data/script/ci +11 -0
  19. data/script/core_ci +9 -0
  20. data/script/plugins_ci +9 -0
  21. data/script/storefront_ci +9 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/bin/bundle +3 -0
  24. data/test/dummy/bin/rails +4 -0
  25. data/test/dummy/bin/rake +4 -0
  26. data/test/dummy/bin/setup +34 -0
  27. data/test/dummy/bin/update +29 -0
  28. data/test/dummy/config.ru +5 -0
  29. data/test/dummy/config/application.rb +28 -0
  30. data/test/dummy/config/boot.rb +5 -0
  31. data/test/dummy/config/cable.yml +9 -0
  32. data/test/dummy/config/environment.rb +5 -0
  33. data/test/dummy/config/environments/development.rb +54 -0
  34. data/test/dummy/config/environments/production.rb +86 -0
  35. data/test/dummy/config/environments/test.rb +43 -0
  36. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  37. data/test/dummy/config/initializers/assets.rb +11 -0
  38. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  40. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  41. data/test/dummy/config/initializers/inflections.rb +16 -0
  42. data/test/dummy/config/initializers/mime_types.rb +4 -0
  43. data/test/dummy/config/initializers/new_framework_defaults.rb +21 -0
  44. data/test/dummy/config/initializers/session_store.rb +3 -0
  45. data/test/dummy/config/initializers/workarea.rb +5 -0
  46. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/test/dummy/config/locales/en.yml +23 -0
  48. data/test/dummy/config/puma.rb +47 -0
  49. data/test/dummy/config/routes.rb +5 -0
  50. data/test/dummy/config/secrets.yml +22 -0
  51. data/test/dummy/config/spring.rb +6 -0
  52. data/test/dummy/lib/assets/.keep +0 -0
  53. data/test/dummy/log/.keep +0 -0
  54. data/test/javascripts/fixtures/payloads.js +231 -0
  55. data/test/javascripts/google_tag_manager_spec.js +382 -0
  56. data/test/javascripts/spec_helper.js +5 -0
  57. data/test/teaspoon_env.rb +10 -0
  58. data/test/test_helper.rb +10 -0
  59. data/workarea-google_tag_manager.gemspec +18 -0
  60. metadata +151 -0
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ require 'rdoc/task'
9
+
10
+ RDoc::Task.new(:rdoc) do |rdoc|
11
+ rdoc.rdoc_dir = 'rdoc'
12
+ rdoc.title = 'Tasker'
13
+ rdoc.options << '--line-numbers'
14
+ rdoc.rdoc_files.include('README.md')
15
+ rdoc.rdoc_files.include('lib/**/*.rb')
16
+ end
17
+
18
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
19
+ load 'rails/tasks/engine.rake'
20
+ load 'rails/tasks/statistics.rake'
21
+ load 'workarea/changelog.rake'
22
+
23
+ require 'rake/testtask'
24
+
25
+ Rake::TestTask.new(:test) do |t|
26
+ t.libs << 'lib'
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
33
+
34
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
35
+ require 'workarea/google_tag_manager/version'
36
+
37
+ desc "Release version #{Workarea::GoogleTagManager::VERSION} of the gem"
38
+ task :release do
39
+ host = "https://#{ENV['BUNDLE_GEMS__WEBLINC__COM']}@gems.weblinc.com"
40
+
41
+ #Rake::Task['workarea:changelog'].execute
42
+ #system 'git add CHANGELOG.md'
43
+ #system 'git commit -m "Update CHANGELOG"'
44
+ #system 'git push origin HEAD'
45
+
46
+ system "git tag -a v#{Workarea::GoogleTagManager::VERSION} -m 'Tagging #{Workarea::GoogleTagManager::VERSION}'"
47
+ system 'git push --tags'
48
+
49
+ system "gem build workarea-google_tag_manager.gemspec"
50
+ system "gem push workarea-google_tag_manager-#{Workarea::GoogleTagManager::VERSION}.gem"
51
+ system "gem push workarea-google_tag_manager-#{Workarea::GoogleTagManager::VERSION}.gem --host #{host}"
52
+ system "rm workarea-google_tag_manager-#{Workarea::GoogleTagManager::VERSION}.gem"
53
+ end
54
+
55
+
56
+ desc 'Run the JavaScript tests'
57
+ ENV['TEASPOON_RAILS_ENV'] = File.expand_path('../test/dummy/config/environment', __FILE__)
58
+ task teaspoon: 'app:teaspoon'
59
+
60
+ desc 'Start a server at http://localhost:3000/teaspoon for JavaScript tests'
61
+ task :teaspoon_server do
62
+ Dir.chdir("test/dummy")
63
+ teaspoon_env = File.expand_path('../test/teaspoon_env.rb', __FILE__)
64
+ system "RAILS_ENV=test TEASPOON_ENV=#{teaspoon_env} rails s"
65
+ end
@@ -0,0 +1,233 @@
1
+ WORKAREA.analytics.registerAdapter('googleTagManager', function () {
2
+ 'use strict';
3
+
4
+ var products,
5
+
6
+ checkoutStep = function (payload, step) {
7
+ products = [];
8
+
9
+ _.each(payload.items, function (impression) {
10
+ products.push({
11
+ 'id': impression.product_id,
12
+ 'name': impression.product_name,
13
+ 'category': impression.category,
14
+ 'variant': impression.sku,
15
+ 'price': impression.price,
16
+ 'quantity': impression.quantity
17
+ });
18
+ });
19
+
20
+ window.dataLayer.push({
21
+ 'event': 'checkout',
22
+ 'ecommerce': {
23
+ 'checkout': {
24
+ 'actionField': { 'step': step },
25
+ 'products': products
26
+ }
27
+ }
28
+ });
29
+ };
30
+
31
+ window.dataLayer = window.dataLayer || [];
32
+
33
+ return {
34
+ 'categoryView': function (payload) {
35
+ window.dataLayer.push({
36
+ 'event': 'categoryView',
37
+ 'name': payload.name,
38
+ 'sort': payload.sort,
39
+ 'page': payload.page
40
+ });
41
+ },
42
+
43
+ 'searchResultsView': function (payload) {
44
+ window.dataLayer.push({
45
+ 'event': 'searchResultsView',
46
+ 'terms': payload.terms,
47
+ 'sort': payload.sort,
48
+ 'page': payload.page
49
+ });
50
+ },
51
+
52
+ 'productList': function (payload) {
53
+ var impressions = [];
54
+
55
+ _.each(payload.impressions, function (impression) {
56
+ impressions.push({
57
+ 'id': impression.id,
58
+ 'price': impression.price,
59
+ 'name': impression.name,
60
+ 'category': impression.category,
61
+ 'variant': impression.sku,
62
+ 'list': payload.name,
63
+ 'position': impression.position
64
+ });
65
+ });
66
+
67
+ window.dataLayer.push({
68
+ 'ecommerce': {
69
+ 'impressions': impressions
70
+ }
71
+ });
72
+ },
73
+
74
+ 'productClick': function (payload) {
75
+ window.dataLayer.push({
76
+ 'event': 'productClick',
77
+ 'ecommerce': {
78
+ 'click': {
79
+ 'actionField': {'list': payload.list},
80
+ 'products': [{
81
+ 'name': payload.name,
82
+ 'id': payload.id,
83
+ 'price': payload.price,
84
+ 'category': payload.category,
85
+ 'variant': payload.sku,
86
+ 'position': payload.position
87
+ }]
88
+ }
89
+ }
90
+ });
91
+ },
92
+
93
+ 'productQuickview': function (payload) {
94
+ window.dataLayer.push({
95
+ 'event': 'quickview',
96
+ 'ecommerce': {
97
+ 'detail': {
98
+ 'actionField': '',
99
+ 'products': [{
100
+ 'name': payload.name,
101
+ 'id': payload.id,
102
+ 'price': payload.price,
103
+ 'category': payload.category,
104
+ 'variant': payload.sku
105
+ }]
106
+ }
107
+ }
108
+ });
109
+ },
110
+
111
+ 'productView': function (payload) {
112
+ window.dataLayer.push({
113
+ 'ecommerce': {
114
+ 'detail': {
115
+ 'actionField': '',
116
+ 'products': [{
117
+ 'name': payload.name,
118
+ 'id': payload.id,
119
+ 'price': payload.price,
120
+ 'category': payload.category,
121
+ 'variant': payload.sku
122
+ }]
123
+ }
124
+ }
125
+ });
126
+ },
127
+
128
+ 'addToCart': function (payload) {
129
+ window.dataLayer.push({
130
+ 'event': 'addToCart',
131
+ 'ecommerce': {
132
+ 'add': {
133
+ 'products': [{
134
+ 'id': payload.id,
135
+ 'name': payload.name,
136
+ 'category': payload.category,
137
+ 'variant': payload.sku,
138
+ 'price': payload.price,
139
+ 'quantity': payload.quantity
140
+ }]
141
+ }
142
+ }
143
+ });
144
+ },
145
+
146
+ 'removeFromCart': function (payload) {
147
+ window.dataLayer.push({
148
+ 'event': 'removeFromCart',
149
+ 'ecommerce': {
150
+ 'remove': {
151
+ 'products': [{
152
+ 'id': payload.id,
153
+ 'name': payload.name,
154
+ 'category': payload.category,
155
+ 'variant': payload.sku,
156
+ 'price': payload.price,
157
+ 'quantity': payload.quantity
158
+ }]
159
+ }
160
+ }
161
+ });
162
+ },
163
+
164
+ 'cartView': function (payload) {
165
+ checkoutStep(payload, 1);
166
+ },
167
+
168
+ 'checkoutAddressesView': function (payload) {
169
+ checkoutStep(payload, 2);
170
+ },
171
+
172
+ 'checkoutShippingView': function (payload) {
173
+ checkoutStep(payload, 3);
174
+ },
175
+
176
+ 'checkoutShippingServiceSelected': function (payload) {
177
+ window.dataLayer.push({
178
+ 'event': 'checkoutOption',
179
+ 'ecommerce': {
180
+ 'checkout_option': {
181
+ 'actionField': {'step': 3, 'option': payload.name}
182
+ }
183
+ }
184
+ });
185
+ },
186
+
187
+ 'checkoutPaymentView': function (payload) {
188
+ checkoutStep(payload, 4);
189
+ },
190
+
191
+ 'checkoutPaymentSelected': function (payload) {
192
+ window.dataLayer.push({
193
+ 'event': 'checkoutOption',
194
+ 'ecommerce': {
195
+ 'checkout_option': {
196
+ 'actionField': {'step': 4, 'option': payload.type}
197
+ }
198
+ }
199
+ });
200
+ },
201
+
202
+ 'checkoutOrderPlaced': function (payload) {
203
+ products = [];
204
+
205
+ _.each(payload.items, function (impression) {
206
+ products.push({
207
+ 'name': impression.product_name,
208
+ 'id': impression.product_id,
209
+ 'price': impression.price,
210
+ 'category': impression.category,
211
+ 'variant': impression.sku,
212
+ 'quantity': impression.quantity
213
+ });
214
+ });
215
+
216
+ window.dataLayer.push({
217
+ 'ecommerce': {
218
+ 'purchase': {
219
+ 'actionField': {
220
+ 'id': payload.id,
221
+ 'affiliation': payload.site_name,
222
+ 'revenue': payload.total_price,
223
+ 'tax': payload.tax_total,
224
+ 'shipping': payload.shipping_total,
225
+ 'coupon': payload.promo_codes.join(',')
226
+ },
227
+ 'products': products
228
+ }
229
+ }
230
+ });
231
+ }
232
+ };
233
+ });
@@ -0,0 +1,3 @@
1
+ - if Workarea::GoogleTagManager.container_id.present?
2
+ %noscript
3
+ %iframe{ src: "//www.googletagmanager.com/ns.html?id=#{Workarea::GoogleTagManager.container_id}", height: 0, width: 0, style: 'display:none;visibility:hidden'}
@@ -0,0 +1,3 @@
1
+ - if Workarea::GoogleTagManager.container_id.present?
2
+ :javascript
3
+ (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer', "#{Workarea::GoogleTagManager.container_id}");
data/bin/rails ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path('..', __dir__)
6
+ ENGINE_PATH = File.expand_path('../lib/google_tag_manager/engine', __dir__)
7
+
8
+ # Set up gems listed in the Gemfile.
9
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
10
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
11
+
12
+ require 'action_controller/railtie'
13
+ require 'action_view/railtie'
14
+ require 'action_mailer/railtie'
15
+ require 'rails/test_unit/railtie'
16
+ require 'sprockets/railtie'
17
+
18
+ require 'rails/engine/commands'
@@ -0,0 +1,16 @@
1
+ module Workarea
2
+ Plugin.append_javascripts(
3
+ 'storefront.modules',
4
+ 'workarea/storefront/google_tag_manager/modules/adapter'
5
+ )
6
+
7
+ Plugin.append_partials(
8
+ 'storefront.document_head',
9
+ 'workarea/storefront/google_tag_manager/script'
10
+ )
11
+
12
+ Plugin.append_partials(
13
+ 'storefront.javascript',
14
+ 'workarea/storefront/google_tag_manager/no_script'
15
+ )
16
+ end
@@ -0,0 +1,3 @@
1
+ Workarea.configure do |config|
2
+ config.google_tag_manager = ActiveSupport::Configurable::Configuration.new
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'workarea'
2
+ require 'workarea/storefront'
3
+ require 'workarea/admin'
4
+
5
+ require 'workarea/google_tag_manager/engine'
6
+ require 'workarea/google_tag_manager/version'
7
+
8
+ module Workarea
9
+ module GoogleTagManager
10
+ # TODO: Remove this method and fallback to secrets configuration in the next
11
+ # major, config via rails configuration is preferable and can be referenced
12
+ # directly in the view
13
+ def self.configure_container
14
+ if Workarea.config.google_tag_manager_container_id.present?
15
+ self.container_id = Workarea.config.google_tag_manager_container_id
16
+ elsif Rails.application.secrets.google_tag_manager.present?
17
+ secrets = Rails.application.secrets.google_tag_manager.deep_symbolize_keys
18
+
19
+ self.container_id = secrets[:container_id]
20
+ end
21
+ end
22
+
23
+ def self.container_id
24
+ if Workarea.config.google_tag_manager.container_id.present?
25
+ Workarea.config.google_tag_manager.container_id
26
+ elsif Rails.application.secrets.google_tag_manager.present?
27
+ message = <<~WARN
28
+ Setting Google Tag Manager containter id via secrets is deprecated.
29
+ Set your container_id via Workarea.config
30
+
31
+ Workarea.configure do |config|
32
+ config.google_tag_manager.container_id = YOUR_CONTAINER_ID
33
+ end
34
+ WARN
35
+ ActiveSupport::Deprecation.warn(message)
36
+
37
+ Rails.application.secrets.google_tag_manager[:container_id]
38
+ else
39
+ @container_id
40
+ end
41
+ end
42
+
43
+ def self.container_id=(container_id)
44
+ message = <<~WARN
45
+ Workarea::GoogleTagManager.container_id= is deprecated. Set your container_id
46
+ via Workarea.config
47
+
48
+ Workarea.configure do |config|
49
+ config.google_tag_manager.container_id = YOUR_CONTAINER_ID
50
+ end
51
+ WARN
52
+ ActiveSupport::Deprecation.warn(message)
53
+ @container_id = container_id
54
+ end
55
+
56
+ def self.config
57
+ Workarea.config.google_tag_manager
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,8 @@
1
+ module Workarea
2
+ module GoogleTagManager
3
+ class Engine < ::Rails::Engine
4
+ include Workarea::Plugin
5
+ isolate_namespace Workarea::GoogleTagManager
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module Workarea
2
+ module GoogleTagManager
3
+ VERSION = "4.3.1"
4
+ end
5
+ end
data/script/admin_ci ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+
3
+ gem install bundler
4
+ bundle update
5
+
6
+ CI_GEM_PATH="$(bundle show workarea-ci)"
7
+ . ${CI_GEM_PATH}/exe/workarea-set-ci-env
8
+
9
+ bin/rails app:workarea:test:admin
data/script/ci ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env bash
2
+
3
+ gem install bundler
4
+ bundle update
5
+
6
+ CI_GEM_PATH="$(bundle show workarea-ci)"
7
+ . ${CI_GEM_PATH}/exe/workarea-set-ci-env
8
+
9
+ bundle exec workarea-lint
10
+ bundle exec workarea-audit
11
+ bin/rails test test/**/*_test.rb