rack_google_tag_manager 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc8af5dcf4324b0118bf19a3d9f99fd7fd9945de
4
+ data.tar.gz: 16105a7d9ea57be1bdfec07318252ddc2bf26d37
5
+ SHA512:
6
+ metadata.gz: b61054a57157c2a83f147dffb57912d45982f59cee1705528657be4cc2fac4145721e2868006272cc67fe5668b53ff84093310a0d3b081187a4ca7a0541b7ff8
7
+ data.tar.gz: a3835c2c0784e0794bc53fef0e66ba6061eeb3d260c3b838db3d2750f39d29ae9dd5717aa4c95439bd4f1a9888ca47942f0af7d2c00b36f6460b4b483cfe780f
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 2.0.0
3
+ - 2.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### v1.0.0, 2014-12-12
2
+
3
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack_google_tag_manager.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 On the Beach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Rack Google Tag Manager [![Build Status](https://travis-ci.org/onthebeach/rack_google_tag_manager.svg)](https://travis-ci.org/onthebeach/rack_google_tag_manager)
2
+
3
+ Rack Middleware to inject [Google Tag Manager](https://developers.google.com/tag-manager/)
4
+ code snippet into all html responses with body tag for rack based applications.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'rack_google_tag_manager'
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ ## Usage
17
+
18
+ ### Rails
19
+
20
+ Add this line to your application's config/application.rb:
21
+
22
+ config.middleware.use 'Rack::GoogleTagManager', tacker: 'GTM-XXXXXX'
23
+
24
+ To pass inforamtion to GTM from your application you can do it with a JavaScript
25
+ array called *dataLayer*. There are two ways to use it:
26
+
27
+ 1. `dataLayer = [{ 'data': 'myData' }]`
28
+ 2. `dataLayer.push({ 'data': 'myData' })`
29
+
30
+ The first one will overwrite all existing values that were previously
31
+ passed to the service, while the second will append the new values to
32
+ the existing ones.
33
+
34
+ In order for the `push` API so be usable, the *dataLayer* must first be
35
+ declared as an array, the GTM JavaScript snippet inserted at the
36
+ top of the body will declare it for us.
37
+
38
+ This initial declaration must occur before snippet. Since the snippet
39
+ must appear at the very top of the body, then the *dataLayer* declaration
40
+ must be the very first item in the body.
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( http://github.com/onthebeach/rack_google_tag_manager/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class GoogleTagManager
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,62 @@
1
+ require 'rack'
2
+
3
+ module Rack
4
+ class GoogleTagManager
5
+ def initialize(app, tracker: tracker)
6
+ @app, @tracker = app, tracker
7
+ end
8
+
9
+ def call(env)
10
+ dup._call(env)
11
+ end
12
+
13
+ def _call(env)
14
+ @status, @headers, @body = app.call(env)
15
+ return [@status, @headers, @body] unless html_response?
16
+
17
+ response = Rack::Response.new([], @status, @headers)
18
+ @body.each { |fragment| response.write(inject_in(fragment)) }
19
+
20
+ @body.close if @body.respond_to?(:close)
21
+
22
+ response.finish
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :app, :tracker
28
+
29
+ def html_response?
30
+ @headers['Content-Type'] =~ %r{text/html}
31
+ end
32
+
33
+ def inject_in(response)
34
+ response.sub(open_body_tag_regex, "\\0\n#{container_snippet}")
35
+ end
36
+
37
+ def open_body_tag_regex
38
+ %r{<body.*?>}im
39
+ end
40
+
41
+ def container_snippet
42
+ @container_snippet ||= <<-EOS
43
+ <!-- Google Tag Manager -->
44
+ <noscript>
45
+ <iframe src="//www.googletagmanager.com/ns.html?id=#{tracker}"
46
+ height="0" width="0" style="display:none;visibility:hidden">
47
+ </iframe>
48
+ </noscript>
49
+
50
+ <script>
51
+ dataLayer = [];
52
+ (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
53
+ new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
54
+ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
55
+ '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
56
+ })(window, document, 'script', 'dataLayer', '#{tracker}');
57
+ </script>
58
+ <!-- End Google Tag Manager -->
59
+ EOS
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,2 @@
1
+ require 'rack/google_tag_manager'
2
+ require 'rack/google_tag_manager/version'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/google_tag_manager/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rack_google_tag_manager'
8
+ spec.version = Rack::GoogleTagManager::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.authors = ['Ancor Cruz']
11
+ spec.email = ['hello@ancorcruz.com']
12
+ spec.summary = %q{Rack Middleware to inject Google Tag Manager code snippet into server html responses for rack based applications}
13
+ spec.description = %q{Rack Middleware to inject Google Tag Manager code snippet into server html responses for rack based applications}
14
+ spec.homepage = 'https://github.com/onthebeach/rack_google_tag_manager'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'rack'
26
+ spec.add_development_dependency 'rack-test'
27
+ spec.add_development_dependency 'simplecov'
28
+ end
data/spec/dummy/app.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Dummy
2
+ class App
3
+ def call(env)
4
+ [200, {"Content-Type" => "text/html"}, response]
5
+ end
6
+
7
+ private
8
+
9
+ def response
10
+ [::File.read(::File.expand_path('../response.html', __FILE__))]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ require 'rack_google_tag_manager'
2
+ require_relative 'app'
3
+
4
+ use Rack::GoogleTagManager, tracker: 'GTM-M2014'
5
+ use Rack::Lint
6
+ run Dummy::App.new
@@ -0,0 +1,9 @@
1
+ <html>
2
+ <head>
3
+ <title>My Dummy app!</title>
4
+ </head>
5
+
6
+ <body class='main'>
7
+ <h1>There is some stuff</h1>
8
+ </body>
9
+ </html>
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::GoogleTagManager do
4
+ include Rack::Test::Methods
5
+
6
+ let(:tracker_id) { 'GTM-M2014' }
7
+ let(:app) { Rack::Builder.parse_file(app_rackup_file).first }
8
+ let(:app_rackup_file) {
9
+ ::File.expand_path('../../dummy/config.ru', __FILE__)
10
+ }
11
+
12
+ before { get '/' }
13
+
14
+ it { expect(last_response).to be_ok }
15
+
16
+ it 'includes google tag manager container snippet' do
17
+ expect(last_response).to match(%r[<body.*?>.*#{tracker_id}.*<h1>.*<\/body>]m)
18
+ end
19
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ module Rack
4
+ describe GoogleTagManager do
5
+ let(:tracker_id) { 'GTM-FAKEID' }
6
+ let(:middleware) { described_class.new(mock_app, tracker: tracker_id) }
7
+ let(:request) { Rack::MockRequest.new(middleware) }
8
+
9
+ context 'for a text/html response' do
10
+ subject { request.get('/').body }
11
+
12
+ it 'adds the container snippet after body tag' do
13
+ expect(subject).to match(%r[<body.*?>.*#{tracker_id}.*Works!<\/body>]m)
14
+ end
15
+
16
+ it 'includes the dataLayer declaration' do
17
+ expect(subject).to include('dataLayer = []')
18
+ end
19
+ end
20
+
21
+ context 'for a text/html response with uppercase html tags' do
22
+ subject { request.get('/uppercase_tags.html').body }
23
+
24
+ it 'adds the container snippet after body tag' do
25
+ expect(subject).to match(%r[<BODY.*?>.*#{tracker_id}.*Works!<\/BODY>]m)
26
+ end
27
+ end
28
+
29
+ context 'for a text/html response without body tag' do
30
+ subject { request.get('/fragment.html').body }
31
+
32
+ it 'does not add the container snippet' do
33
+ expect(subject).not_to include(tracker_id)
34
+ end
35
+
36
+ it 'does not modify the response' do
37
+ expect(subject).to eq(mock_html_fragment)
38
+ end
39
+ end
40
+
41
+ context 'for no text/html responses' do
42
+ subject { request.get('/test.json').body }
43
+
44
+ it 'does not add the container snippet' do
45
+ expect(subject).not_to include(tracker_id)
46
+ end
47
+
48
+ it 'does not modify the response' do
49
+ expect(subject).to eq(mock_json_response)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,14 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'simplecov'
4
+ SimpleCov.start do
5
+ add_filter '/spec/'
6
+ end
7
+
8
+ require 'rack_google_tag_manager'
9
+ require 'support/mock_app_helper'
10
+ require 'rack/test'
11
+
12
+ RSpec.configure do |config|
13
+ include MockAppHelper
14
+ end
@@ -0,0 +1,35 @@
1
+ module MockAppHelper
2
+ def mock_app
3
+ -> (env) {
4
+ request = Rack::Request.new(env)
5
+ case request.path
6
+ when '/' then
7
+ [200, { 'Content-Type' => 'text/html' }, [mock_html_response]]
8
+ when '/test.json' then
9
+ [200, { 'Content-Type' => 'application/json' }, [mock_json_response]]
10
+ when '/fragment.html' then
11
+ [200, { 'Content-Type' => 'text/html' }, [mock_html_fragment]]
12
+ when '/uppercase_tags.html' then
13
+ [200, { 'Content-Type' => 'text/html' }, [mock_uppercase_tags_response]]
14
+ else
15
+ [404, 'Not found']
16
+ end
17
+ }
18
+ end
19
+
20
+ def mock_html_response
21
+ '<body class="main">Works!</body>'
22
+ end
23
+
24
+ def mock_html_fragment
25
+ '<div>it is a fragment of html</div>'
26
+ end
27
+
28
+ def mock_json_response
29
+ '{"json":"here"}'
30
+ end
31
+
32
+ def mock_uppercase_tags_response
33
+ '<BODY>Works!</BODY>'
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_google_tag_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ancor Cruz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Rack Middleware to inject Google Tag Manager code snippet into server
98
+ html responses for rack based applications
99
+ email:
100
+ - hello@ancorcruz.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - CHANGELOG.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - lib/rack/google_tag_manager.rb
113
+ - lib/rack/google_tag_manager/version.rb
114
+ - lib/rack_google_tag_manager.rb
115
+ - rack_google_tag_manager.gemspec
116
+ - spec/dummy/app.rb
117
+ - spec/dummy/config.ru
118
+ - spec/dummy/response.html
119
+ - spec/integration/rack_google_tag_manager_spec.rb
120
+ - spec/lib/rack_google_tag_manager_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/support/mock_app_helper.rb
123
+ homepage: https://github.com/onthebeach/rack_google_tag_manager
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.2.2
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Rack Middleware to inject Google Tag Manager code snippet into server html
147
+ responses for rack based applications
148
+ test_files:
149
+ - spec/dummy/app.rb
150
+ - spec/dummy/config.ru
151
+ - spec/dummy/response.html
152
+ - spec/integration/rack_google_tag_manager_spec.rb
153
+ - spec/lib/rack_google_tag_manager_spec.rb
154
+ - spec/spec_helper.rb
155
+ - spec/support/mock_app_helper.rb