gtm_on_rails 0.1.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: 14ab2f80c367244061aeffbdc9ddf196e66efd36
4
+ data.tar.gz: 0312a7c4f439098bfec363eb704c0b052ac39224
5
+ SHA512:
6
+ metadata.gz: e27af578d0c8688a9b7877f7cad83b0a1902be5a9437b0c4da0065fe9e8458f710dead53f447e72554d57df51079260ee5ec22d8bd65d5d1b0a7582917e8cdca
7
+ data.tar.gz: 93e5c22b8d86a7627cc390d4641ee29029293d1d3681ed21d5c4d1cb8fc7da52600513785b9821e05890aa1949bbeefc10bca64a043587a1821157394bc840e9
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 ykogure
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # GTMonRails
2
+ GTMonRails enables you to integrate Google Tag Manager easy.
3
+
4
+ *Read this in other languages: [日本語](README.ja.md)*
5
+
6
+ ## Installation
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gtm_on_rails'
11
+ ```
12
+
13
+ And then execute:
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+
18
+ Or install it yourself as:
19
+ ```bash
20
+ $ gem install gtm_on_rails
21
+ ```
22
+
23
+ ## Configure
24
+ #### container_id
25
+ #### data_layer_limit_byte_size
26
+ #### send_controller_and_action_in_data_layer
27
+ #### rescue_when_error_occurred
28
+
29
+ ## License
30
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'GtmOnRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,7 @@
1
+ <% begin %>
2
+ <%= data_layer.print_on_js %>
3
+ <%-
4
+ rescue StandardError => e
5
+ raise e unless GtmOnRails.config.rescue_when_error_occurred
6
+ end
7
+ -%>
@@ -0,0 +1,4 @@
1
+ <!-- Google Tag Manager (noscript) -->
2
+ <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<%= GtmOnRails.config.container_id %>"
3
+ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
4
+ <!-- End Google Tag Manager (noscript) -->
@@ -0,0 +1,7 @@
1
+ <!-- Google Tag Manager -->
2
+ <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
3
+ new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
4
+ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
5
+ 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
6
+ })(window,document,'script','dataLayer','<%= GtmOnRails.config.container_id %>');</script>
7
+ <!-- End Google Tag Manager -->
@@ -0,0 +1,22 @@
1
+ module GtmOnRails
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Creates a GTM on Rails initializer to your application."
7
+
8
+ def copy_initializer
9
+ template "gtm_on_rails.rb", "config/initializers/gtm_on_rails.rb"
10
+ end
11
+
12
+ def insert_javascript_tag
13
+ inject_into_file "app/views/layouts/application.html.erb", after: /<head[\s]?[^>]*>/ do
14
+ "\n <%= render_gtm_on_rails_tag_in_head %>\n"
15
+ end
16
+ inject_into_file "app/views/layouts/application.html.erb", after: /<body[\s]?[^>]*>/ do
17
+ "\n <%= render_gtm_on_rails_tag_in_body %>\n"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ GtmOnRails.configure do |config|
2
+ # Your Google Tag Manager Container ID
3
+ config.container_id = "Replacing GTM-XXXX with your container ID"
4
+
5
+ # DataLayer is limited by size less than 8,192 bytes in once post,
6
+ # so default limit afford 7,000 bytes with an margin.
7
+ config.data_layer_limit_byte_size = 7000
8
+
9
+ # Settings that send google tag manager controller and action name by dataLayer.
10
+ # Be careful using this, if you enable this option, controller and action name output in html source code.
11
+ config.send_controller_and_action_in_data_layer = false
12
+
13
+ # If somthing error occurred when output tags, subsequent tag's output is stopped and run subsequent processing.
14
+ # The point is, dataLayer error won't affect displaying website, if this option is enable.
15
+ config.rescue_when_error_occurred = false
16
+ end
@@ -0,0 +1,9 @@
1
+ module GtmOnRails
2
+ class Config
3
+ # Variables detail is writen in lib/generators/templates/gtm_on_rails.rb.
4
+ attr_accessor :container_id
5
+ attr_accessor :data_layer_limit_byte_size
6
+ attr_accessor :send_controller_and_action_in_data_layer
7
+ attr_accessor :rescue_when_error_occurred
8
+ end
9
+ end
@@ -0,0 +1,32 @@
1
+ module GtmOnRails
2
+ module Controllers
3
+ module InitializeDataLayer
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ if Rails::VERSION::MAJOR == 3
8
+ before_filter :initialize_data_layer
9
+ else
10
+ before_action :initialize_data_layer
11
+ end
12
+ helper_method :data_layer
13
+ end
14
+
15
+ def data_layer
16
+ @gtm_on_rails_data_layer
17
+ end
18
+
19
+ private
20
+ def initialize_data_layer
21
+ @gtm_on_rails_data_layer = GtmOnRails::DataLayer.new
22
+
23
+ if GtmOnRails.config.send_controller_and_action_in_data_layer
24
+ @gtm_on_rails_data_layer.push({
25
+ controller: controller_name,
26
+ action: action_name
27
+ })
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ require 'gtm_on_rails/config'
2
+ require 'gtm_on_rails/controllers/initialize_data_layer'
3
+ require 'gtm_on_rails/models/data_layer'
4
+ require 'gtm_on_rails/models/data_layer_object'
5
+ require 'gtm_on_rails/models/data_layer_event'
6
+ require 'gtm_on_rails/tag_helper'
7
+
8
+ module GtmOnRails
9
+ def self.config
10
+ @config ||= GtmOnRails::Config.new
11
+ end
12
+
13
+ def self.configure(&block)
14
+ yield(config) if block_given?
15
+ end
16
+
17
+ class Engine < ::Rails::Engine
18
+ isolate_namespace GtmOnRails
19
+
20
+ initializer 'gtm_on_rails.action_controller_extension' do
21
+ ActiveSupport.on_load :action_controller do
22
+ include GtmOnRails::Controllers::InitializeDataLayer
23
+ end
24
+ end
25
+ initializer 'gtm_on_rails.action_view_helpers' do
26
+ ActiveSupport.on_load :action_view do
27
+ include GtmOnRails::TagHelper
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ # Class to take a roll as dataLayer of javascript's variable
2
+ module GtmOnRails
3
+ class DataLayer
4
+ include ActionView::Helpers::TagHelper
5
+
6
+ attr_reader :objects
7
+
8
+ def initialize(*args)
9
+ options = args.extract_options!
10
+ @objects = args # @objects is instances of GTM::DataLayerObject
11
+ end
12
+
13
+ def push(objects)
14
+ objects = [objects].flatten
15
+ objects.each do |object|
16
+ case object
17
+ when Hash
18
+ @objects << GtmOnRails::DataLayerObject.new(object)
19
+ when GtmOnRails::DataLayerObject
20
+ @objects << object
21
+ end
22
+ end
23
+ end
24
+
25
+ def print_on_js
26
+ js_codes = []
27
+
28
+ js_codes << "var dataLayer = dataLayer || [];"
29
+ @objects.each do |data_layer_object|
30
+ # dataLayer size limit exception
31
+ size = data_layer_object.to_json.bytesize
32
+ if size > GtmOnRails.config.data_layer_limit_byte_size
33
+ raise ArgumentError.new("DataLayer bytesize is over limit #{GtmOnRails.config.data_layer_limit_byte_size} bytes. Size is #{size} bytes.")
34
+ end
35
+
36
+ js_codes << "dataLayer.push(#{data_layer_object.to_json});"
37
+ end
38
+
39
+ return content_tag(:script, js_codes.join.html_safe)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ # Class to take a roll as javascript's object in dataLayer for google tag manager event
2
+ module GtmOnRails
3
+ class DataLayerEvent < GtmOnRails::DataLayerObject
4
+ def initialize(event_name, **args)
5
+ @data = args.merge(event: event_name).with_indifferent_access
6
+ end
7
+
8
+ def event_name
9
+ @data[:event]
10
+ end
11
+ def event_name=(arg)
12
+ @data[:event] = arg
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ # Class to take a roll as javascript's object in dataLayer
2
+ module GtmOnRails
3
+ class DataLayerObject
4
+ attr_accessor :data
5
+
6
+ def initialize(**args)
7
+ @data = args.with_indifferent_access
8
+ end
9
+
10
+ def add(**hash)
11
+ @data.merge!(hash)
12
+ end
13
+
14
+ def to_json
15
+ @data.to_json
16
+ end
17
+
18
+ def method_missing(method, *args, &block)
19
+ if @data.has_key?(method)
20
+ @data[method]
21
+ else
22
+ super
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ module GtmOnRails
2
+ module TagHelper
3
+ def render_gtm_on_rails_tag_in_head
4
+ tags = []
5
+ tags << render(partial: 'gtm_on_rails/layouts/google_tag_manager_tag_in_head')
6
+ tags.join.html_safe
7
+ end
8
+
9
+ def render_gtm_on_rails_tag_in_body
10
+ tags = []
11
+ tags << render(partial: 'gtm_on_rails/layouts/data_layer_tag')
12
+ tags << render(partial: 'gtm_on_rails/layouts/google_tag_manager_tag_in_body')
13
+ tags.join.html_safe
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module GtmOnRails
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ require "gtm_on_rails/engine"
2
+
3
+ module GtmOnRails
4
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gtm_on_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ykogure
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.22
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.22
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
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
+ description: An plugin that integrate Google Tag Manager easy with Rails
42
+ email:
43
+ - renkin1008@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/views/gtm_on_rails/layouts/_data_layer_tag.html.erb
52
+ - app/views/gtm_on_rails/layouts/_google_tag_manager_tag_in_body.html.erb
53
+ - app/views/gtm_on_rails/layouts/_google_tag_manager_tag_in_head.html.erb
54
+ - lib/generators/gtm_on_rails/install_generator.rb
55
+ - lib/generators/templates/gtm_on_rails.rb
56
+ - lib/gtm_on_rails.rb
57
+ - lib/gtm_on_rails/config.rb
58
+ - lib/gtm_on_rails/controllers/initialize_data_layer.rb
59
+ - lib/gtm_on_rails/engine.rb
60
+ - lib/gtm_on_rails/models/data_layer.rb
61
+ - lib/gtm_on_rails/models/data_layer_event.rb
62
+ - lib/gtm_on_rails/models/data_layer_object.rb
63
+ - lib/gtm_on_rails/tag_helper.rb
64
+ - lib/gtm_on_rails/version.rb
65
+ homepage: https://github.com/ZIGExN/gtm_on_rails
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.6.11
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: An plugin that integrate Google Tag Manager easy with Rails
89
+ test_files: []