gtm_rails 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -2
- data/lib/gtm_rails/config.rb +1 -1
- data/lib/gtm_rails/helper.rb +39 -4
- data/lib/gtm_rails/hooks.rb +1 -1
- data/lib/gtm_rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9b47055f9b0503a5da6958685c6143684e8b848
|
4
|
+
data.tar.gz: a7aedddc9f401bfbcb1830a7a4da44dcacd54d95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7901270779c9cb549424e6c402014676fabec9a9664eadd47a8ade816d5ebb27810396bc3fb91848e81c82f630055e8646b9e5202011cb94219b071558bb3e53
|
7
|
+
data.tar.gz: c3f811f8936e4afb3afc9f6e7ca2d2a4d38dac74d0eb0ee1779312f833398db74f50b8dd56d5970895ffe9198b83afd01dc6ae0c59d96148e88616f704cf9b42
|
data/README.md
CHANGED
@@ -37,7 +37,7 @@ production:
|
|
37
37
|
baz: GTM-YourGtmId6
|
38
38
|
```
|
39
39
|
|
40
|
-
The above is a sample. `foo`, `bar`, `baz` are arbitrary label names.
|
40
|
+
The above is a sample. `foo`, `bar`, `baz` are arbitrary label names, and replacing GTM-XXXX with your container ID.
|
41
41
|
|
42
42
|
In view, use as follows. The argument is a label name specified in `config/google_tag_manager.yml`.
|
43
43
|
|
@@ -45,7 +45,21 @@ In view, use as follows. The argument is a label name specified in `config/googl
|
|
45
45
|
<%= google_tag_manager(:foo) %>
|
46
46
|
```
|
47
47
|
|
48
|
-
|
48
|
+
Or do next 2 steps. (recommended, click here to [read more](https://developers.google.com/tag-manager/quickstart))
|
49
|
+
|
50
|
+
First, following method as close to the opening `<head>` tag as possible on every page of your website.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
<%= google_tag_manager_script_tag(:foo) %>
|
54
|
+
```
|
55
|
+
|
56
|
+
Second, following method immediately after the opening `<body>` tag on every page of your website.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
<%= google_tag_manager_noscript_tag(:foo) %>
|
60
|
+
```
|
61
|
+
|
62
|
+
Google Tag Manager container snippet will be expanded based on `Rails.env` and label name. If there is no match, a blank string is returned.
|
49
63
|
|
50
64
|
## Contributing
|
51
65
|
|
data/lib/gtm_rails/config.rb
CHANGED
data/lib/gtm_rails/helper.rb
CHANGED
@@ -1,21 +1,56 @@
|
|
1
1
|
module GtmRails
|
2
2
|
module Helper
|
3
3
|
def google_tag_manager(label)
|
4
|
-
|
4
|
+
container_id = GtmRails::Config.container_ids[label]
|
5
5
|
|
6
|
-
return '' if
|
6
|
+
return '' if container_id.blank?
|
7
7
|
|
8
8
|
<<-HTML.html_safe
|
9
9
|
<!-- Google Tag Manager -->
|
10
|
-
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=#{
|
10
|
+
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=#{container_id}"
|
11
11
|
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
|
12
12
|
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
13
13
|
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
14
14
|
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
15
15
|
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
16
|
-
})(window,document,'script','dataLayer','#{
|
16
|
+
})(window,document,'script','dataLayer','#{container_id}');</script>
|
17
17
|
<!-- End Google Tag Manager -->
|
18
18
|
HTML
|
19
19
|
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Use this method as close to the opening <head> tag as possible on every page of your website.
|
23
|
+
#
|
24
|
+
def google_tag_manager_script_tag(label)
|
25
|
+
container_id = GtmRails::Config.container_ids[label]
|
26
|
+
|
27
|
+
return '' if container_id.blank?
|
28
|
+
|
29
|
+
<<-HTML.html_safe
|
30
|
+
<!-- Google Tag Manager -->
|
31
|
+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
32
|
+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
33
|
+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
34
|
+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
35
|
+
})(window,document,'script','dataLayer','#{container_id}');</script>
|
36
|
+
<!-- End Google Tag Manager -->
|
37
|
+
HTML
|
38
|
+
end
|
39
|
+
|
40
|
+
#
|
41
|
+
# Use this method immediately after the opening <body> tag on every page of your website.
|
42
|
+
#
|
43
|
+
def google_tag_manager_noscript_tag(label)
|
44
|
+
container_id = GtmRails::Config.container_ids[label]
|
45
|
+
|
46
|
+
return '' if container_id.blank?
|
47
|
+
|
48
|
+
<<-HTML.html_safe
|
49
|
+
<!-- Google Tag Manager (noscript) -->
|
50
|
+
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=#{container_id}"
|
51
|
+
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
|
52
|
+
<!-- End Google Tag Manager (noscript) -->
|
53
|
+
HTML
|
54
|
+
end
|
20
55
|
end
|
21
56
|
end
|
data/lib/gtm_rails/hooks.rb
CHANGED
@@ -5,7 +5,7 @@ module GtmRails
|
|
5
5
|
|
6
6
|
loader = ::GtmRails::YamlLoader.new
|
7
7
|
|
8
|
-
GtmRails::Config.
|
8
|
+
GtmRails::Config.container_ids = (loader.load[Rails.env] || {}).with_indifferent_access
|
9
9
|
|
10
10
|
ActionView::Base.send :include, GtmRails::Helper
|
11
11
|
end
|
data/lib/gtm_rails/version.rb
CHANGED