From a7f566083f015029b41a9bfdf8599a6dcff9e177 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Thu, 18 Apr 2019 19:06:36 +0000 Subject: [PATCH 01/26] Update README.md --- README.md | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 96ccd72..caa936e 100644 --- a/README.md +++ b/README.md @@ -18,18 +18,9 @@ rack middleware that can be hooked up to multiple services and exposing them in fashion. It comes in two parts, the first one is the actual middleware that you can add to the middleware stack the second part are the service-handlers that you're going to use in your application. It's easy to add your own [custom handlers](#custom-handlers), -but to get you started we're shipping support for the following services out of the box: - -* [Google Global Site Tag](#google-global) -* [Google Analytics](#google-analytics) -* [Google Adwords Conversion](#google-adwords-conversion) -* [Google Tag Manager](#google-tag-manager) -* [Facebook](#facebook) -* [Visual Website Optimizer (VWO)](#visual-website-optimizer-vwo) -* [GoSquared](#gosquared) -* [Criteo](#criteo) -* [Zanox](#zanox) -* [Hotjar](#hotjar) +but to get you started we're shipping support for the services [mentioned below](#services) +out of the box: + ## Respecting the Do Not Track (DNT) HTTP header @@ -112,6 +103,8 @@ request.env['tracker'] = { } ``` +## Services + ### Google Global Site Tag (gtag.js) * `:anonymize_ip` - sets the tracker to remove the last octet from all IP addresses, see https://developers.google.com/analytics/devguides/collection/gtagjs/ip-anonymization for details. From 5ddc22c134df62eda0535a0ebb046eacdf550db2 Mon Sep 17 00:00:00 2001 From: Chris Coffey Date: Thu, 27 Jun 2019 11:31:48 -0400 Subject: [PATCH 02/26] Add Hubspot integration --- README.md | 12 +++++++++++- lib/rack/tracker.rb | 1 + lib/rack/tracker/hubspot/hubspot.rb | 2 ++ lib/rack/tracker/hubspot/template/hubspot.erb | 1 + lib/rack/tracker/version.rb | 2 +- spec/handler/hubspot_spec.rb | 11 +++++++++++ spec/integration/hubspot_integration_spec.rb | 19 +++++++++++++++++++ spec/support/metal_controller.rb | 4 ++++ 8 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 lib/rack/tracker/hubspot/hubspot.rb create mode 100644 lib/rack/tracker/hubspot/template/hubspot.erb create mode 100644 spec/handler/hubspot_spec.rb create mode 100644 spec/integration/hubspot_integration_spec.rb diff --git a/README.md b/README.md index caa936e..515465c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ rack middleware that can be hooked up to multiple services and exposing them in fashion. It comes in two parts, the first one is the actual middleware that you can add to the middleware stack the second part are the service-handlers that you're going to use in your application. It's easy to add your own [custom handlers](#custom-handlers), -but to get you started we're shipping support for the services [mentioned below](#services) +but to get you started we're shipping support for the services [mentioned below](#services) out of the box: @@ -594,6 +594,16 @@ tracker do |t| end ``` +### Hubspot + +[Hubspot](https://www.hubspot.com/) + +``` +config.middleware.use(Rack::Tracker) do + handler :hubspot, { site_id: '1234' } +end +``` + ### Custom Handlers diff --git a/lib/rack/tracker.rb b/lib/rack/tracker.rb index c3a72f4..6cce2fb 100644 --- a/lib/rack/tracker.rb +++ b/lib/rack/tracker.rb @@ -24,6 +24,7 @@ require "rack/tracker/zanox/zanox" require "rack/tracker/hotjar/hotjar" require "rack/tracker/bing/bing" +require "rack/tracker/hubspot/hubspot" module Rack class Tracker diff --git a/lib/rack/tracker/hubspot/hubspot.rb b/lib/rack/tracker/hubspot/hubspot.rb new file mode 100644 index 0000000..162b07c --- /dev/null +++ b/lib/rack/tracker/hubspot/hubspot.rb @@ -0,0 +1,2 @@ +class Rack::Tracker::Hubspot < Rack::Tracker::Handler +end diff --git a/lib/rack/tracker/hubspot/template/hubspot.erb b/lib/rack/tracker/hubspot/template/hubspot.erb new file mode 100644 index 0000000..ecf9556 --- /dev/null +++ b/lib/rack/tracker/hubspot/template/hubspot.erb @@ -0,0 +1 @@ + diff --git a/lib/rack/tracker/version.rb b/lib/rack/tracker/version.rb index c48fbdb..bd778d2 100644 --- a/lib/rack/tracker/version.rb +++ b/lib/rack/tracker/version.rb @@ -1,5 +1,5 @@ module Rack class Tracker - VERSION = '1.9.0' + VERSION = '1.9.1' end end diff --git a/spec/handler/hubspot_spec.rb b/spec/handler/hubspot_spec.rb new file mode 100644 index 0000000..ea36178 --- /dev/null +++ b/spec/handler/hubspot_spec.rb @@ -0,0 +1,11 @@ +RSpec.describe Rack::Tracker::Hubspot do + + def env + { misc: '42' } + end + + it 'will be placed in the head' do + expect(described_class.position).to eq(:head) + expect(described_class.new(env).position).to eq(:head) + end +end diff --git a/spec/integration/hubspot_integration_spec.rb b/spec/integration/hubspot_integration_spec.rb new file mode 100644 index 0000000..4414aa4 --- /dev/null +++ b/spec/integration/hubspot_integration_spec.rb @@ -0,0 +1,19 @@ +require 'support/capybara_app_helper' + +RSpec.describe "Hubspot Integration" do + before do + setup_app(action: :hubspot) do |tracker| + tracker.handler :hubspot, { site_id: '123456' } + end + + visit '/' + end + + + subject { page } + + it "embeds the site-specifc script tag" do + expect(page).to have_xpath("//script", id: "hs-script-loader" ) + expect(page.find("script")[:src]).to eq("//js.hs-scripts.com/123456.js") + end +end diff --git a/spec/support/metal_controller.rb b/spec/support/metal_controller.rb index a666165..13f0a61 100644 --- a/spec/support/metal_controller.rb +++ b/spec/support/metal_controller.rb @@ -117,6 +117,10 @@ def hotjar render "metal/index" end + def hubspot + render "metal/index" + end + def bing render "metal/index" end From 68eab20b49f5d4de6683a20487fddd0834a28e0d Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Thu, 27 Jun 2019 21:03:39 +0200 Subject: [PATCH 03/26] =?UTF-8?q?version=20up=20=F0=9F=8D=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ lib/rack/tracker/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2622f17..063ede4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.10.0 + + * [ENHANCEMENT] Hubspot integration #136 (thx @ChrisCoffey) + # 1.9.0 * [ENHANCEMENT] Integration for Bing tracking #131 (thx @pcraston) diff --git a/lib/rack/tracker/version.rb b/lib/rack/tracker/version.rb index bd778d2..b8a0953 100644 --- a/lib/rack/tracker/version.rb +++ b/lib/rack/tracker/version.rb @@ -1,5 +1,5 @@ module Rack class Tracker - VERSION = '1.9.1' + VERSION = '1.10.0' end end From 57395d4e18993ab7dbc1207bbc6345ad0a03c154 Mon Sep 17 00:00:00 2001 From: Maris Veide Date: Wed, 10 Jul 2019 13:47:02 +0300 Subject: [PATCH 04/26] #137: Accepting all passed attributes for google_global tracking --- lib/rack/tracker/google_global/google_global.rb | 4 ++-- spec/handler/google_global_spec.rb | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/rack/tracker/google_global/google_global.rb b/lib/rack/tracker/google_global/google_global.rb index 4214ea3..36fb37b 100644 --- a/lib/rack/tracker/google_global/google_global.rb +++ b/lib/rack/tracker/google_global/google_global.rb @@ -12,10 +12,10 @@ def params class Event < OpenStruct PREFIXED_PARAMS = %i[category label] LITERAL_PARAMS = %i[value] - PARAMS = PREFIXED_PARAMS + LITERAL_PARAMS + SKIP_PARAMS = %i[action] def params - Hash[to_h.slice(*PARAMS).map { |key, value| [param_key(key), value] }] + Hash[to_h.except(*SKIP_PARAMS).map { |key, value| [param_key(key), value] }] end private diff --git a/spec/handler/google_global_spec.rb b/spec/handler/google_global_spec.rb index dcf6fcc..0858020 100644 --- a/spec/handler/google_global_spec.rb +++ b/spec/handler/google_global_spec.rb @@ -210,13 +210,15 @@ def env 'action' => 'login', 'category' => 'engagement', 'label' => 'Github', - 'value' => 5 } + 'value' => 5, + 'transaction_id' => 1001, + } ] }} end it "will show event" do - expect(subject).to match(%r{gtag\('event', 'login', {\"event_category\":\"engagement\",\"event_label\":\"Github\",\"value\":5}\);}) + expect(subject).to match(%r{gtag\('event', 'login', {\"event_category\":\"engagement\",\"event_label\":\"Github\",\"value\":5,\"transaction_id\":1001}\);}) end end end From 349d2087f3918a6b99f0051957e341f1d54306ff Mon Sep 17 00:00:00 2001 From: Maris Veide Date: Thu, 11 Jul 2019 10:27:04 +0300 Subject: [PATCH 05/26] #137: Removed unused constant --- lib/rack/tracker/google_global/google_global.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/rack/tracker/google_global/google_global.rb b/lib/rack/tracker/google_global/google_global.rb index 36fb37b..0631794 100644 --- a/lib/rack/tracker/google_global/google_global.rb +++ b/lib/rack/tracker/google_global/google_global.rb @@ -11,7 +11,6 @@ def params class Event < OpenStruct PREFIXED_PARAMS = %i[category label] - LITERAL_PARAMS = %i[value] SKIP_PARAMS = %i[action] def params From d2c6afc689656143886cf2ef53d101612a42aa28 Mon Sep 17 00:00:00 2001 From: Abby Sassel <3883855+sassela@users.noreply.github.com> Date: Wed, 31 Jul 2019 08:49:47 +0200 Subject: [PATCH 06/26] Add support for Drift (#139) * Add support for Drift * Add usage docs --- README.md | 9 ++++++++ lib/rack/tracker.rb | 1 + lib/rack/tracker/drift/drift.rb | 2 ++ lib/rack/tracker/drift/template/drift.erb | 26 ++++++++++++++++++++++ spec/handler/drift_spec.rb | 10 +++++++++ spec/integration/drift_integration_spec.rb | 18 +++++++++++++++ spec/support/metal_controller.rb | 4 ++++ 7 files changed, 70 insertions(+) create mode 100644 lib/rack/tracker/drift/drift.rb create mode 100644 lib/rack/tracker/drift/template/drift.erb create mode 100644 spec/handler/drift_spec.rb create mode 100644 spec/integration/drift_integration_spec.rb diff --git a/README.md b/README.md index 515465c..a0fdd89 100644 --- a/README.md +++ b/README.md @@ -604,6 +604,15 @@ config.middleware.use(Rack::Tracker) do end ``` +### Drift + +[Drift](https://www.drift.com/) + +``` +config.middleware.use(Rack::Tracker) do + handler :drift, account_id: 'DRIFT_ID' +end +``` ### Custom Handlers diff --git a/lib/rack/tracker.rb b/lib/rack/tracker.rb index 6cce2fb..2d3dc91 100644 --- a/lib/rack/tracker.rb +++ b/lib/rack/tracker.rb @@ -25,6 +25,7 @@ require "rack/tracker/hotjar/hotjar" require "rack/tracker/bing/bing" require "rack/tracker/hubspot/hubspot" +require "rack/tracker/drift/drift" module Rack class Tracker diff --git a/lib/rack/tracker/drift/drift.rb b/lib/rack/tracker/drift/drift.rb new file mode 100644 index 0000000..fb7fe34 --- /dev/null +++ b/lib/rack/tracker/drift/drift.rb @@ -0,0 +1,2 @@ +class Rack::Tracker::Drift < Rack::Tracker::Handler +end diff --git a/lib/rack/tracker/drift/template/drift.erb b/lib/rack/tracker/drift/template/drift.erb new file mode 100644 index 0000000..1b0d12a --- /dev/null +++ b/lib/rack/tracker/drift/template/drift.erb @@ -0,0 +1,26 @@ + diff --git a/spec/handler/drift_spec.rb b/spec/handler/drift_spec.rb new file mode 100644 index 0000000..5f96374 --- /dev/null +++ b/spec/handler/drift_spec.rb @@ -0,0 +1,10 @@ +RSpec.describe Rack::Tracker::Drift do + def env + { foo: 'bar' } + end + + it 'will be placed in the head' do + expect(described_class.position).to eq(:head) + expect(described_class.new(env).position).to eq(:head) + end +end diff --git a/spec/integration/drift_integration_spec.rb b/spec/integration/drift_integration_spec.rb new file mode 100644 index 0000000..3192d2c --- /dev/null +++ b/spec/integration/drift_integration_spec.rb @@ -0,0 +1,18 @@ +require 'support/capybara_app_helper' + +RSpec.describe 'Drift Integration' do + before do + setup_app(action: :drift) do |tracker| + tracker.handler :drift, account_id: 'DRIFT_ID' + end + + visit '/' + end + + subject { page } + + it 'embeds the script with account_id' do + expect(page.find('script')).to have_content('js.driftt.com') + expect(page.find('script')).to have_content('DRIFT_ID') + end +end diff --git a/spec/support/metal_controller.rb b/spec/support/metal_controller.rb index 13f0a61..644b6b7 100644 --- a/spec/support/metal_controller.rb +++ b/spec/support/metal_controller.rb @@ -124,4 +124,8 @@ def hubspot def bing render "metal/index" end + + def drift + render "metal/index" + end end From 21e6d1c27be5f33ce988b209ee29c03ba3742f92 Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Wed, 31 Jul 2019 08:56:07 +0200 Subject: [PATCH 07/26] fresh new release with new integration --- CHANGELOG.md | 4 ++++ lib/rack/tracker/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 063ede4..6d63fc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.11.0 + + * [ENHANCEMENT] Add support for Drift #139 (thx @sassela) + # 1.10.0 * [ENHANCEMENT] Hubspot integration #136 (thx @ChrisCoffey) diff --git a/lib/rack/tracker/version.rb b/lib/rack/tracker/version.rb index b8a0953..dc87117 100644 --- a/lib/rack/tracker/version.rb +++ b/lib/rack/tracker/version.rb @@ -1,5 +1,5 @@ module Rack class Tracker - VERSION = '1.10.0' + VERSION = '1.11.0' end end From 997b90fa6884d6ff6e8368144d782c6a3a70a145 Mon Sep 17 00:00:00 2001 From: Abby Sassel <3883855+sassela@users.noreply.github.com> Date: Thu, 1 Aug 2019 10:36:15 +0100 Subject: [PATCH 08/26] Uncaught ReferenceError Fix: wrap Drift account ID in quotes (#140) * Uncaught ReferenceError Fix: wrap Drift account ID in quotes * Update Drift integration test to verify fix --- lib/rack/tracker/drift/template/drift.erb | 2 +- spec/integration/drift_integration_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rack/tracker/drift/template/drift.erb b/lib/rack/tracker/drift/template/drift.erb index 1b0d12a..e046404 100644 --- a/lib/rack/tracker/drift/template/drift.erb +++ b/lib/rack/tracker/drift/template/drift.erb @@ -22,5 +22,5 @@ } }(); drift.SNIPPET_VERSION = '0.3.1'; -drift.load(<%= options[:account_id] %>); +drift.load('<%= options[:account_id] %>'); diff --git a/spec/integration/drift_integration_spec.rb b/spec/integration/drift_integration_spec.rb index 3192d2c..498538f 100644 --- a/spec/integration/drift_integration_spec.rb +++ b/spec/integration/drift_integration_spec.rb @@ -13,6 +13,6 @@ it 'embeds the script with account_id' do expect(page.find('script')).to have_content('js.driftt.com') - expect(page.find('script')).to have_content('DRIFT_ID') + expect(page.find('script')).to have_content('drift.load(\'DRIFT_ID\')') end end From a9f9eda5e40d4c12ea8bea1e781fb91ec3d41763 Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Thu, 1 Aug 2019 19:55:10 +0200 Subject: [PATCH 09/26] bugfix release --- CHANGELOG.md | 4 ++++ lib/rack/tracker/version.rb | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d63fc4..885993c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.11.1 + + * [BUGFIX] Uncaught ReferenceError Fix: wrap Drift account ID in quotes #140 (thx @sassela) + # 1.11.0 * [ENHANCEMENT] Add support for Drift #139 (thx @sassela) diff --git a/lib/rack/tracker/version.rb b/lib/rack/tracker/version.rb index dc87117..ce6f23f 100644 --- a/lib/rack/tracker/version.rb +++ b/lib/rack/tracker/version.rb @@ -1,5 +1,5 @@ module Rack class Tracker - VERSION = '1.11.0' + VERSION = '1.11.1' end end From 0616d5d088ded1541983688cb16367c6c6e0529c Mon Sep 17 00:00:00 2001 From: Chris Coffey Date: Sun, 25 Aug 2019 12:39:26 -0400 Subject: [PATCH 10/26] Allow disabling explicit pageview sends for GA (#141) * Enable disabling explicit pageview sends for GA * Bump version and update Changelog+Docs --- CHANGELOG.md | 4 ++++ README.md | 1 + .../tracker/google_analytics/google_analytics.rb | 5 +++++ .../template/google_analytics.erb | 2 +- lib/rack/tracker/version.rb | 2 +- spec/handler/google_analytics_spec.rb | 16 ++++++++++++++++ 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 885993c..6523463 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.11.2 + + * [ENHANCEMENT] Allows disabling the Google Analytics pageview send. Defaults to true. + # 1.11.1 * [BUGFIX] Uncaught ReferenceError Fix: wrap Drift account ID in quotes #140 (thx @sassela) diff --git a/README.md b/README.md index a0fdd89..469998a 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ end * `:enhanced_ecommerce` - Enables [Enhanced Ecommerce Tracking](https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce) * `:optimize` - pass [Google Optimize container ID](https://support.google.com/360suite/optimize/answer/6262084#example-combined-snippet) as value (e.g. `optimize: 'GTM-1234'`). * `:pageview_url_script` - a String containing a custom js script evaluating to the url that shoudl be given to the pageview event. Default to `window.location.pathname + window.location.search`. +* `:explicit_pageview` - A boolean that controls whether to send the `pageview` event on pageload. This defaults to true. #### Events diff --git a/lib/rack/tracker/google_analytics/google_analytics.rb b/lib/rack/tracker/google_analytics/google_analytics.rb index a18d1e0..f33663b 100644 --- a/lib/rack/tracker/google_analytics/google_analytics.rb +++ b/lib/rack/tracker/google_analytics/google_analytics.rb @@ -2,6 +2,11 @@ class Rack::Tracker::GoogleAnalytics < Rack::Tracker::Handler self.allowed_tracker_options = [:cookie_domain, :user_id] + def initialize(env, options = {}) + options[:explicit_pageview] = true if !options.has_key?(:explicit_pageview) + super(env, options) + end + class Send < OpenStruct def initialize(attrs = {}) attrs.reverse_merge!(type: 'event') diff --git a/lib/rack/tracker/google_analytics/template/google_analytics.erb b/lib/rack/tracker/google_analytics/template/google_analytics.erb index 000ff7c..673a671 100644 --- a/lib/rack/tracker/google_analytics/template/google_analytics.erb +++ b/lib/rack/tracker/google_analytics/template/google_analytics.erb @@ -38,7 +38,7 @@ <% if options[:ecommerce] && ecommerce_events.any? %> ga('ecommerce:send'); <% end %> -<% if tracker %> +<% if tracker && options[:explicit_pageview] %> ga('send', 'pageview', <%= pageview_url_script %>); <% end %> diff --git a/lib/rack/tracker/version.rb b/lib/rack/tracker/version.rb index ce6f23f..371b0c0 100644 --- a/lib/rack/tracker/version.rb +++ b/lib/rack/tracker/version.rb @@ -1,5 +1,5 @@ module Rack class Tracker - VERSION = '1.11.1' + VERSION = '1.11.2' end end diff --git a/spec/handler/google_analytics_spec.rb b/spec/handler/google_analytics_spec.rb index 5d46e8d..6951a02 100644 --- a/spec/handler/google_analytics_spec.rb +++ b/spec/handler/google_analytics_spec.rb @@ -284,5 +284,21 @@ def env expect(subject.pageview_url_script).to eql ("{ 'page': location.pathname + location.search + location.hash }") end end + + context 'with explicit_pageview disabled' do + subject { described_class.new(env, {tracker: 'afake', explicit_pageview: false }).render } + + it 'does not send a pageview event' do + expect(subject).not_to include %q{ga('send', 'pageview',} + end + end + + context 'defaults to sending the pageview event' do + subject { described_class.new(env, {tracker: 'afake'}).render } + + it 'does not send a pageview event' do + expect(subject).to include "ga('send', 'pageview'" + end + end end end From 8695ac65036017217722a7a428a5871deb297353 Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Sun, 25 Aug 2019 18:40:59 +0200 Subject: [PATCH 11/26] version up --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6523463..480708d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ -# 1.11.2 +# 1.12.0 - * [ENHANCEMENT] Allows disabling the Google Analytics pageview send. Defaults to true. + * [ENHANCEMENT] Allows disabling the Google Analytics pageview send. Defaults to true #131 (thx @ChrisCoffey) # 1.11.1 From f554f692be869ff937a4ea61eb8907e5630915f4 Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Sun, 25 Aug 2019 19:23:40 +0200 Subject: [PATCH 12/26] =?UTF-8?q?=F0=9F=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 480708d..e444877 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 1.12.0 +# 1.11.2 * [ENHANCEMENT] Allows disabling the Google Analytics pageview send. Defaults to true #131 (thx @ChrisCoffey) From 09b46c67a6fc31109ae81a9c9ae757628727899e Mon Sep 17 00:00:00 2001 From: glaszig Date: Mon, 30 Sep 2019 09:20:33 +0200 Subject: [PATCH 13/26] test on rails 6.0 (#144) * test on rails 6.0 * do not test rails 6.0 on ruby < 2.5 on travis * test on jruby-9.2.7.0, do not test rails 6 on jruby-9.1.10.0 --- .travis.yml | 10 ++++++++++ Gemfile.rails-6.0 | 6 ++++++ 2 files changed, 16 insertions(+) create mode 100644 Gemfile.rails-6.0 diff --git a/.travis.yml b/.travis.yml index b754765..3c8e02f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,8 +6,18 @@ rvm: - 2.5.3 - 2.6.1 - jruby-9.1.10.0 + - jruby-9.2.7.0 gemfile: - Gemfile - Gemfile.rails-3.2 - Gemfile.rails-4.2 - Gemfile.rails-5.2 + - Gemfile.rails-6.0 +matrix: + exclude: + - rvm: 2.3.8 + gemfile: Gemfile.rails-6.0 + - rvm: 2.4.5 + gemfile: Gemfile.rails-6.0 + - rvm: jruby-9.1.10.0 + gemfile: Gemfile.rails-6.0 diff --git a/Gemfile.rails-6.0 b/Gemfile.rails-6.0 new file mode 100644 index 0000000..ee3fb05 --- /dev/null +++ b/Gemfile.rails-6.0 @@ -0,0 +1,6 @@ +source "https://rubygems.org" + +gemspec + +gem 'activesupport', '~> 6.0.0' +gem 'actionpack', '~> 6.0.0' From c10ea3ecbe4e8bab6af07a0bed9260d6bfa7c1c4 Mon Sep 17 00:00:00 2001 From: Mohan Zhang Date: Fri, 8 Nov 2019 04:46:01 -0400 Subject: [PATCH 14/26] Add support for Heap (#147) --- README.md | 12 ++++++++++++ lib/rack/tracker.rb | 1 + lib/rack/tracker/heap/heap.rb | 2 ++ lib/rack/tracker/heap/template/heap.erb | 4 ++++ lib/rack/tracker/version.rb | 2 +- spec/handler/heap_spec.rb | 10 ++++++++++ spec/integration/heap_integration_spec.rb | 17 +++++++++++++++++ spec/support/metal_controller.rb | 4 ++++ 8 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 lib/rack/tracker/heap/heap.rb create mode 100644 lib/rack/tracker/heap/template/heap.erb create mode 100644 spec/handler/heap_spec.rb create mode 100644 spec/integration/heap_integration_spec.rb diff --git a/README.md b/README.md index 469998a..e1b7927 100644 --- a/README.md +++ b/README.md @@ -615,6 +615,18 @@ config.middleware.use(Rack::Tracker) do end ``` +### Heap + +[Heap](https://heap.io/). Heap has Projects (e.g. "Main") which have multiple +Environments (e.g. "Production" or "Development"). `env_id` is therefore the numerical ID +that represents the Environment. See Settings -> Projects -> Environments in your dashboard. + +``` +config.middleware.use(Rack::Tracker) do + handler :heap, env_id: 'HEAP_ID' +end +``` + ### Custom Handlers Tough we give you handlers for a few tracking services right out of the box, you might diff --git a/lib/rack/tracker.rb b/lib/rack/tracker.rb index 2d3dc91..ed0a07e 100644 --- a/lib/rack/tracker.rb +++ b/lib/rack/tracker.rb @@ -26,6 +26,7 @@ require "rack/tracker/bing/bing" require "rack/tracker/hubspot/hubspot" require "rack/tracker/drift/drift" +require "rack/tracker/heap/heap" module Rack class Tracker diff --git a/lib/rack/tracker/heap/heap.rb b/lib/rack/tracker/heap/heap.rb new file mode 100644 index 0000000..dac9efb --- /dev/null +++ b/lib/rack/tracker/heap/heap.rb @@ -0,0 +1,2 @@ +class Rack::Tracker::Heap < Rack::Tracker::Handler +end diff --git a/lib/rack/tracker/heap/template/heap.erb b/lib/rack/tracker/heap/template/heap.erb new file mode 100644 index 0000000..b3ac665 --- /dev/null +++ b/lib/rack/tracker/heap/template/heap.erb @@ -0,0 +1,4 @@ + diff --git a/lib/rack/tracker/version.rb b/lib/rack/tracker/version.rb index 371b0c0..fb24a29 100644 --- a/lib/rack/tracker/version.rb +++ b/lib/rack/tracker/version.rb @@ -1,5 +1,5 @@ module Rack class Tracker - VERSION = '1.11.2' + VERSION = '1.12.0' end end diff --git a/spec/handler/heap_spec.rb b/spec/handler/heap_spec.rb new file mode 100644 index 0000000..7e48df4 --- /dev/null +++ b/spec/handler/heap_spec.rb @@ -0,0 +1,10 @@ +RSpec.describe Rack::Tracker::Heap do + def env + { foo: 'bar' } + end + + it 'will be placed in the head' do + expect(described_class.position).to eq(:head) + expect(described_class.new(env).position).to eq(:head) + end +end diff --git a/spec/integration/heap_integration_spec.rb b/spec/integration/heap_integration_spec.rb new file mode 100644 index 0000000..ff866a1 --- /dev/null +++ b/spec/integration/heap_integration_spec.rb @@ -0,0 +1,17 @@ +require 'support/capybara_app_helper' + +RSpec.describe "Heap Integration" do + before do + setup_app(action: :heap) do |tracker| + tracker.handler :heap, { env_id: '12341234' } + end + + visit '/' + end + + subject { page } + + it 'embeds the script with site_id' do + expect(page).to have_content('heap.load("12341234");') + end +end diff --git a/spec/support/metal_controller.rb b/spec/support/metal_controller.rb index 644b6b7..f22447e 100644 --- a/spec/support/metal_controller.rb +++ b/spec/support/metal_controller.rb @@ -128,4 +128,8 @@ def bing def drift render "metal/index" end + + def heap + render "metal/index" + end end From f3d4e65dee9527136c7613940f5cee16fba91f88 Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Thu, 14 Nov 2019 10:21:19 +0100 Subject: [PATCH 15/26] add integration spec for callable behaviour --- .../template/google_analytics.erb | 4 ++-- spec/integration/rails_integration_spec.rb | 23 +++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/rack/tracker/google_analytics/template/google_analytics.erb b/lib/rack/tracker/google_analytics/template/google_analytics.erb index 673a671..5aa0159 100644 --- a/lib/rack/tracker/google_analytics/template/google_analytics.erb +++ b/lib/rack/tracker/google_analytics/template/google_analytics.erb @@ -1,5 +1,5 @@ - +<% end %> diff --git a/spec/integration/rails_integration_spec.rb b/spec/integration/rails_integration_spec.rb index 368efbe..ec932f0 100644 --- a/spec/integration/rails_integration_spec.rb +++ b/spec/integration/rails_integration_spec.rb @@ -1,10 +1,19 @@ require 'support/capybara_app_helper' RSpec.describe "Rails Integration" do + let(:callable_skip_inject) do + lambda do |env| + # check for anything in the env hash to decide + # if you return the `tracker id` or `nil` to skip injection + nil + end + end + before do setup_app(action: :index) do |tracker| tracker.handler :track_all_the_things, { custom_key: 'SomeKey123' } tracker.handler :another_handler, { custom_key: 'AnotherKey42' } + tracker.handler :google_analytics, { tracker: callable_skip_inject } end visit '/' @@ -17,15 +26,15 @@ Metal Layout - - + +

welcome to metal#index

- + HTML From e22fb0f40762d1f7fb5f81e482f1631019ea83c1 Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Thu, 14 Nov 2019 10:23:32 +0100 Subject: [PATCH 16/26] heap release :sunny: --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e444877..3643e28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.12.0 + +* [ENHANCEMENT] Add support for Heap #147 (thx @mohanzhang) + # 1.11.2 * [ENHANCEMENT] Allows disabling the Google Analytics pageview send. Defaults to true #131 (thx @ChrisCoffey) From 42b8e5101ade142932e2a0e5dc96a81afff40ba4 Mon Sep 17 00:00:00 2001 From: glaszig Date: Sun, 8 Dec 2019 10:56:21 +0100 Subject: [PATCH 17/26] google global: better empty tracker handling (#142) * google global: better empty tracker handling * google global: improve integration test * google global: collect trackers only once * google global: improve tracker option validation and collection logic * google global: test if callable gets called * code style * google global: simplified invalid tracker check * warn about empty google global tracker --- .../tracker/google_global/google_global.rb | 29 +++++++++++++++++-- .../google_global/template/google_global.erb | 2 +- .../google_global_integration_spec.rb | 27 ++++++++++++----- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/rack/tracker/google_global/google_global.rb b/lib/rack/tracker/google_global/google_global.rb index 0631794..5ccb817 100644 --- a/lib/rack/tracker/google_global/google_global.rb +++ b/lib/rack/tracker/google_global/google_global.rb @@ -35,9 +35,7 @@ def events end def trackers - options[:trackers].map { |tracker| - tracker[:id].respond_to?(:call) ? tracker.merge(id: tracker[:id].call(env)) : tracker - }.reject { |tracker| tracker[:id].nil? } + @_trackers ||= build_trackers end def set_options @@ -46,6 +44,31 @@ def set_options private + def build_trackers + options[:trackers].map(&method(:call_tracker)).reject(&method(:invalid_tracker?)) + end + + def call_tracker(tracker) + if tracker[:id].respond_to?(:call) + tracker.merge(id: tracker[:id].call(env)) + else + tracker + end + end + + def invalid_tracker?(tracker) + if tracker[:id].to_s.strip == '' + $stdout.puts <<~WARN + WARNING: One of the trackers specified for Rack::Tracker handler 'google_global' is empty. + Trackers: #{options[:trackers]} + WARN + + true + else + false + end + end + def build_set_options value = options[:set] value.respond_to?(:call) ? value.call(env) : value diff --git a/lib/rack/tracker/google_global/template/google_global.erb b/lib/rack/tracker/google_global/template/google_global.erb index b6141a8..9103419 100644 --- a/lib/rack/tracker/google_global/template/google_global.erb +++ b/lib/rack/tracker/google_global/template/google_global.erb @@ -1,4 +1,4 @@ -<% if trackers %> +<% if trackers.any? %> <% end %> From 590bc95a0220f7249327fd3e662d92471274408c Mon Sep 17 00:00:00 2001 From: Ignacy Kasperowicz Date: Mon, 24 Feb 2020 13:46:07 +0100 Subject: [PATCH 19/26] Make middleware thread safe --- lib/rack/tracker.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/rack/tracker.rb b/lib/rack/tracker.rb index ed0a07e..e30cf98 100644 --- a/lib/rack/tracker.rb +++ b/lib/rack/tracker.rb @@ -38,6 +38,10 @@ def initialize(app, &block) end def call(env) + dup._call(env) + end + + def _call(env) @status, @headers, @body = @app.call(env) return [@status, @headers, @body] unless html? response = Rack::Response.new([], @status, @headers) From f599f7197fe2de0f1385c4690a0b3d8a6611bd8a Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 25 Mar 2020 05:58:16 +0000 Subject: [PATCH 20/26] Use local variables in rack middleware to prevent instance state changes (#151) see: https://github.com/railslove/rack-tracker/pull/150 Co-authored-by: Marco --- lib/rack/tracker.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/rack/tracker.rb b/lib/rack/tracker.rb index e30cf98..4e10d87 100644 --- a/lib/rack/tracker.rb +++ b/lib/rack/tracker.rb @@ -42,10 +42,10 @@ def call(env) end def _call(env) - @status, @headers, @body = @app.call(env) - return [@status, @headers, @body] unless html? - response = Rack::Response.new([], @status, @headers) + status, headers, body = @app.call(env) + return [status, headers, body] unless headers['Content-Type'] =~ /html/ + response = Rack::Response.new([], status, headers) env[EVENT_TRACKING_KEY] ||= {} if session = env["rack.session"] @@ -56,16 +56,14 @@ def _call(env) session[EVENT_TRACKING_KEY] = env[EVENT_TRACKING_KEY] end - @body.each { |fragment| response.write inject(env, fragment) } - @body.close if @body.respond_to?(:close) + body.each { |fragment| response.write inject(env, fragment) } + body.close if body.respond_to?(:close) response.finish end private - def html?; @headers['Content-Type'] =~ /html/; end - def inject(env, response) duplicated_response = response.dup @handlers.each(env) do |handler| From 1ad0e20e03b8a6fc1230c773c47505ca929258ea Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Fri, 27 Mar 2020 06:12:14 +0100 Subject: [PATCH 21/26] version up --- CHANGELOG.md | 5 +++++ lib/rack/tracker/version.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3643e28..2e06e08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.12.1 + +* [ENHANCEMENT] Use local variables to prevent instance state #151 (thx @bumi) +* [ENHANCEMENT] Make middleware thread safe #150 (thx @kspe) + # 1.12.0 * [ENHANCEMENT] Add support for Heap #147 (thx @mohanzhang) diff --git a/lib/rack/tracker/version.rb b/lib/rack/tracker/version.rb index fb24a29..7d12101 100644 --- a/lib/rack/tracker/version.rb +++ b/lib/rack/tracker/version.rb @@ -1,5 +1,5 @@ module Rack class Tracker - VERSION = '1.12.0' + VERSION = '1.12.1' end end From bba79d6fb87fdbb3f90da63ffee955aaafa5fdc9 Mon Sep 17 00:00:00 2001 From: yutoji Date: Wed, 8 Jul 2020 11:35:14 +0900 Subject: [PATCH 22/26] Use leftmost match to inject head to avoid one line html bug If the response html has no new line after tag, the gtm script tag will be placed on a invalid place. --- .../google_tag_manager/google_tag_manager.rb | 2 +- spec/handler/google_tag_manager_spec.rb | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/rack/tracker/google_tag_manager/google_tag_manager.rb b/lib/rack/tracker/google_tag_manager/google_tag_manager.rb index 2db9d2b..1a05489 100644 --- a/lib/rack/tracker/google_tag_manager/google_tag_manager.rb +++ b/lib/rack/tracker/google_tag_manager/google_tag_manager.rb @@ -10,7 +10,7 @@ def inject(response) # Sub! is enough, in well formed html there's only one head or body tag. # Block syntax need to be used, otherwise backslashes in input will mess the output. # @see http://stackoverflow.com/a/4149087/518204 and https://github.com/railslove/rack-tracker/issues/50 - response.sub! %r{} do |m| + response.sub! %r{} do |m| m.to_s << self.render_head end response.sub! %r{} do |m| diff --git a/spec/handler/google_tag_manager_spec.rb b/spec/handler/google_tag_manager_spec.rb index ed1e147..d86568e 100644 --- a/spec/handler/google_tag_manager_spec.rb +++ b/spec/handler/google_tag_manager_spec.rb @@ -32,4 +32,27 @@ def env end end + describe '#inject' do + subject { handler_object.inject(example_response) } + let(:handler_object) { described_class.new(env, container: 'somebody') } + + before do + allow(handler_object).to receive(:render_head).and_return('') + allow(handler_object).to receive(:render_body).and_return('') + end + + context 'with one line html response' do + let(:example_response) { "" } + + it 'will have render_head content in head tag' do + expect(subject).to match(%r{.*.*}) + end + + it 'will have render_body content in body tag' do + expect(subject).to match(%r{.*.*}) + end + + end + end + end From 20009badc7d2bcd35d15adfad159602af372527c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Pierzcha=C5=82a?= Date: Wed, 10 Mar 2021 18:27:13 +0100 Subject: [PATCH 23/26] hotwired/turbo support in GTM --- .../template/google_tag_manager_head.erb | 7 +++++++ spec/integration/google_tag_manager_integration_spec.rb | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/rack/tracker/google_tag_manager/template/google_tag_manager_head.erb b/lib/rack/tracker/google_tag_manager/template/google_tag_manager_head.erb index 3b059c0..cd930d0 100644 --- a/lib/rack/tracker/google_tag_manager/template/google_tag_manager_head.erb +++ b/lib/rack/tracker/google_tag_manager/template/google_tag_manager_head.erb @@ -17,6 +17,13 @@ <% end %> dataLayer.push({'event':'pageView','virtualUrl': url}); }); + document.addEventListener('turbo:load', function(event) { + var url = event.detail.url; + <% if events.any? %> + dataLayer.push(<%= events.map(&:write).join(', ') %>); + <% end %> + dataLayer.push({'event':'pageView','virtualUrl': url}); + }); <% end %> (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], diff --git a/spec/integration/google_tag_manager_integration_spec.rb b/spec/integration/google_tag_manager_integration_spec.rb index f8fe375..9d2b5aa 100644 --- a/spec/integration/google_tag_manager_integration_spec.rb +++ b/spec/integration/google_tag_manager_integration_spec.rb @@ -23,7 +23,7 @@ expect(page.find("body")).to have_xpath '//body/noscript/iframe[@src="https://www.googletagmanager.com/ns.html?id=GTM-ABCDEF"]' end - it "embeds the turbolinks observer if requested" do + it "embeds turbolinks and turbo observers if requested" do visit '/' expect(page.find("head")).to_not have_content "turbolinks:load" setup_app(action: :google_tag_manager) do |tracker| @@ -31,5 +31,6 @@ end visit '/' expect(page.find("head")).to have_content "turbolinks:load" + expect(page.find("head")).to have_content "turbo:load" end -end \ No newline at end of file +end From 7e1592a030f486cc17cb2bb1f7ef6967fb94415d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Pierzcha=C5=82a?= Date: Thu, 11 Mar 2021 15:09:41 +0100 Subject: [PATCH 24/26] Prevent duplicate events --- .../template/google_tag_manager_head.erb | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/rack/tracker/google_tag_manager/template/google_tag_manager_head.erb b/lib/rack/tracker/google_tag_manager/template/google_tag_manager_head.erb index cd930d0..5032e60 100644 --- a/lib/rack/tracker/google_tag_manager/template/google_tag_manager_head.erb +++ b/lib/rack/tracker/google_tag_manager/template/google_tag_manager_head.erb @@ -1,30 +1,25 @@ <% if container %> - <% unless options[:turbolinks] %> - <% if events.any? %> - - <% end %> - <% end %> - + + <% if events.any? %> + + <% end %> + + <% end %> From de0da83dd0316ac6dc7f24faaab9b428751bdfa0 Mon Sep 17 00:00:00 2001 From: Marco Schaden Date: Mon, 12 Jul 2021 07:55:53 +0200 Subject: [PATCH 26/26] turbo release --- CHANGELOG.md | 5 +++++ lib/rack/tracker/version.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e06e08..4398e32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.13.0 + +* [ENHANCEMENT] hotwired/turbo support #160 (thx @wrozka) +* [BUGFIX] Use leftmost match for gtm tag injection #156 (thx @yutoji) + # 1.12.1 * [ENHANCEMENT] Use local variables to prevent instance state #151 (thx @bumi) diff --git a/lib/rack/tracker/version.rb b/lib/rack/tracker/version.rb index 7d12101..3cf2d99 100644 --- a/lib/rack/tracker/version.rb +++ b/lib/rack/tracker/version.rb @@ -1,5 +1,5 @@ module Rack class Tracker - VERSION = '1.12.1' + VERSION = '1.13.0' end end