gtm_on_rails 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5a8eac117ffac718f394160b8e35932a4f0ce2c
4
- data.tar.gz: 517d2fa7b9b26cb0d863f7888358525a3f38e8eb
3
+ metadata.gz: 1c9e870f052ec71da90cdec566206bf0a0abbdb3
4
+ data.tar.gz: d9f6a5094d2f6ab658b0f6b4b65c792d2c6ecee1
5
5
  SHA512:
6
- metadata.gz: 5c52b280fe734410874001cc4d026053cb886830e5c9c2332950a8dcae7f6d019a9bc463307c9dc45746b37531268e43232d7c27221d1163a9093060b7014263
7
- data.tar.gz: b875836ba165cab590f5154b14f58a7de0a6dc05032c6190daa4d58fb25db044ae94911e2aa97db5f7c4ad4a482f8aece9333a4bf6e1df183a571eb4bf24c2ea
6
+ metadata.gz: 4bada8d3c2ab77c132d1aa4e4441b633c07e94ea6fbde9ee3c302c089b061da502f9630d5c368c154fa5a17898062a43faef76c4e95a3972826e7e6cc3ecf087
7
+ data.tar.gz: 3ee546cdb7d1ae43bb00a3d0f0c8e2c7143f372fc931cde46fe0ac7d647cde5e72db2994584735a94d56bceaa26cab99dd8dd3f5a841c8e42b018c205c855b22
data/README.ja.md CHANGED
@@ -52,6 +52,21 @@ data_layer.push({
52
52
 
53
53
  送信したdataLayerの内容は、GoogleTagManager側で変数に設定する等して使用して下さい。
54
54
 
55
+ ```ruby
56
+ data_layer.push(
57
+ {
58
+ name: 'name'
59
+ },
60
+ before_page_view: true
61
+ )
62
+ ```
63
+
64
+ 上記のように`before_page_view`属性を設定することで、GoogleTagManagerの「Page View」イベントより前にdataLayerをpushできます。
65
+
66
+ 「Page View」イベント自体にdataLayerの変数を使用したい場合等に使用します。
67
+
68
+ ※GoogleAnalyticsのページビュー送信より前に「ヒット」系の送信が行われてしまうと、正常にGoogleAnalyticsのページビューが計測できない可能性があるので、使用する際は注意して下さい。
69
+
55
70
  #### `GtmOnRails::DataLayer::Object`
56
71
  ```ruby
57
72
  object = GtmOnRails::DataLayer::Object.new({name: 'name'})
data/README.md CHANGED
@@ -52,6 +52,21 @@ data_layer.push({
52
52
  ```
53
53
  Configure the variable and so on Google Tag Manager when use sended values.
54
54
 
55
+ ```ruby
56
+ data_layer.push(
57
+ {
58
+ name: 'name'
59
+ },
60
+ before_page_view: true
61
+ )
62
+ ```
63
+
64
+ You also can set dataLayer values before the "Page View" event of Google Tag Manager, when you use `before_page_view` option as the above.
65
+
66
+ For examle, you use this option when want to use dataLayer's variables in the "Page View" event.
67
+
68
+ † Be careful of using this option, because you may not be measure PageView normalcy, when you send Hit type event to GoogleAnalytics before send PageView.
69
+
55
70
  #### `GtmOnRails::DataLayer::Object`
56
71
  ```ruby
57
72
  object = GtmOnRails::DataLayer::Object.new({name: 'name'})
@@ -0,0 +1,14 @@
1
+ <% begin %>
2
+ <!-- GTM on Rails before Page View -->
3
+ <%= data_layer.print_on_html(before_page_view: true) %>
4
+ <!-- End GTM on Rails before Page View -->
5
+ <%-
6
+ rescue => e
7
+ if GtmOnRails.config.rescue_when_error_occurred
8
+ logger.warn(e.message)
9
+ logger.warn(e.backtrace.join("\n"))
10
+ else
11
+ raise e
12
+ end
13
+ end
14
+ -%>
@@ -3,30 +3,40 @@ module GtmOnRails
3
3
  class DataLayer
4
4
  include ActionView::Helpers::TagHelper
5
5
 
6
- attr_accessor :objects
6
+ attr_accessor :objects, :objects_before_page_view
7
7
 
8
8
  def initialize(*args)
9
9
  options = args.extract_options!
10
10
  @objects = [args].flatten # @objects are instances of GTM::DataLayerObject
11
+
12
+ @objects_before_page_view = []
11
13
  end
12
14
 
13
- def push(args)
15
+ def push(args, **options)
14
16
  args = [args].flatten
15
17
  args.each do |object|
16
18
  case object
17
19
  when Hash
18
- @objects << GtmOnRails::DataLayer::Object.new(object)
20
+ obj = GtmOnRails::DataLayer::Object.new(object)
19
21
  when GtmOnRails::DataLayer::Object
20
- @objects << object
22
+ obj = object
23
+ end
24
+
25
+ if options[:before_page_view]
26
+ @objects_before_page_view << obj
27
+ else
28
+ @objects << obj
21
29
  end
22
30
  end
23
31
  end
24
32
 
25
- def to_js
33
+ def to_js(before_page_view: false)
26
34
  js_codes = []
27
35
 
28
36
  js_codes << "var dataLayer = dataLayer || [];"
29
- @objects.each do |data_layer_object|
37
+
38
+ objs = before_page_view ? @objects_before_page_view : @objects
39
+ objs.each do |data_layer_object|
30
40
  # dataLayer size limit exception
31
41
  size = data_layer_object.to_json.bytesize
32
42
  if size > GtmOnRails.config.data_layer_limit_byte_size
@@ -40,8 +50,8 @@ module GtmOnRails
40
50
  return js_codes.join.html_safe
41
51
  end
42
52
 
43
- def print_on_html
44
- content_tag(:script, self.to_js)
53
+ def print_on_html(before_page_view: false)
54
+ content_tag(:script, self.to_js(before_page_view: before_page_view))
45
55
  end
46
56
 
47
57
  def method_missing(method, *args, &block)
@@ -2,6 +2,7 @@ module GtmOnRails
2
2
  module TagHelper
3
3
  def render_gtm_on_rails_tag_in_head
4
4
  tags = []
5
+ tags << render(partial: 'gtm_on_rails/layouts/data_layer_before_page_view_tag')
5
6
  tags << render(partial: 'gtm_on_rails/layouts/google_tag_manager_tag_in_head')
6
7
  tags.join.html_safe
7
8
  end
@@ -1,3 +1,3 @@
1
1
  module GtmOnRails
2
- VERSION = '0.1.10'
2
+ VERSION = '0.1.11'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gtm_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - ykogure
@@ -49,6 +49,7 @@ files:
49
49
  - README.ja.md
50
50
  - README.md
51
51
  - Rakefile
52
+ - app/views/gtm_on_rails/layouts/_data_layer_before_page_view_tag.html.erb
52
53
  - app/views/gtm_on_rails/layouts/_data_layer_tag.html.erb
53
54
  - app/views/gtm_on_rails/layouts/_google_tag_manager_tag_in_body.html.erb
54
55
  - app/views/gtm_on_rails/layouts/_google_tag_manager_tag_in_head.html.erb