roll-amp 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +67 -2
- data/lib/roll/amp.rb +11 -1
- data/lib/roll/amp/helpers/tags.rb +21 -0
- data/lib/roll/amp/html/analytics_tag.rb +27 -0
- data/lib/roll/amp/html/google_analytics_tag.rb +18 -0
- data/lib/roll/amp/script/amp_js_tag.rb +10 -9
- data/lib/roll/amp/script/analytics_script_tag.rb +18 -0
- data/lib/roll/amp/script/include_script_tag.rb +33 -0
- data/lib/roll/amp/script/json_script_tag.rb +19 -0
- data/lib/roll/amp/script/script_tag.rb +27 -0
- data/lib/roll/amp/style/style_tag.rb +3 -2
- data/lib/roll/amp/version.rb +1 -1
- data/roll-amp.gemspec +1 -1
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15a60f9621476bd9d68c5f0f7dd32323a0886a66
|
4
|
+
data.tar.gz: fbb79a383432b067e56ea670013cc3be3524f013
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e08f8e9f5b6d6188bd2509d227854773442a0c35637ecd0c83dbacc05f351dfe9713d344ab5f34e284bb805181bc8bda5a743d04cecdd1ba2b9a5f68f339c347
|
7
|
+
data.tar.gz: 63c8d8484a2bc4120e3ba60b2e4ab2dec61c1061e14755f32b80a7c88a88360ec3afc7cc898648ca4391b17449c6de1049fb4f75050ec514d45ce729ee91b015
|
data/README.md
CHANGED
@@ -12,9 +12,9 @@ Implemented:
|
|
12
12
|
* Tag to include AMP boilerplate CSS.
|
13
13
|
* Tag to include AMP base JS.
|
14
14
|
* Tag to include custom CSS.
|
15
|
+
* Google Analytics integration.
|
15
16
|
|
16
17
|
Todo:
|
17
|
-
* Google Analytics integration.
|
18
18
|
* AMP validation via Ruby tests.
|
19
19
|
* ...
|
20
20
|
|
@@ -50,19 +50,84 @@ Or install it yourself as:
|
|
50
50
|
$ gem install roll-amp
|
51
51
|
|
52
52
|
## Usage
|
53
|
+
* [Documentation on RubyDoc](http://www.rubydoc.info/gems/roll-amp/)
|
53
54
|
* [How to setup AMP layout for Rails app pages](https://github.com/roll-rails/roll-amp/wiki/How-to-setup-AMP-layout-for-Rails-app-pages)
|
54
55
|
|
55
|
-
|
56
|
+
### Include main JS
|
57
|
+
```
|
58
|
+
<%= amp_js %>
|
59
|
+
```
|
60
|
+
Includes `https://cdn.ampproject.org/v0.js` script at a place where the `amp_js`
|
61
|
+
tag was specified.
|
62
|
+
This tag is required for AMP.
|
63
|
+
|
64
|
+
### Include boilerplate CSS
|
65
|
+
```
|
66
|
+
<%= amp_boilerplate %>
|
67
|
+
```
|
68
|
+
Includes basic CSS suggested at https://www.ampproject.org/docs/get_started/create/basic_markup
|
69
|
+
|
70
|
+
### Include custom CSS
|
71
|
+
1. Create CSS (SASS, SCSS) file under `app/stylesheets` of your Rails app.
|
72
|
+
For example `app/stylesheets/amp/application.scss`.
|
73
|
+
2. Add the following line to your AMP layout:
|
74
|
+
```
|
75
|
+
<%= amp_custom_style('amp/application') %>
|
76
|
+
```
|
77
|
+
When `amp/application` is available in the assets pipeline,
|
78
|
+
its content will be loaded from there. Otherwise such file will be searched in
|
79
|
+
the `public` directory.
|
80
|
+
Usually it’s sufficient to create CSS file and Rails will handle the rest.
|
81
|
+
*Please note, AMP sets 50K limit on CSS size. If you include larger stylesheet
|
82
|
+
your pages won’t be valid.*
|
83
|
+
|
84
|
+
### Google Analytics
|
85
|
+
1. Insert the following tag *before* `amp_js`:
|
86
|
+
```
|
87
|
+
<%= amp_analytics_js %>
|
88
|
+
```
|
89
|
+
2. Add analytics configuration to the *body*:
|
90
|
+
```
|
91
|
+
<%=
|
92
|
+
amp_google_analytics(
|
93
|
+
'UA-00000-1',
|
94
|
+
{
|
95
|
+
trackPageview: {
|
96
|
+
on: 'visible',
|
97
|
+
request: 'pageview'
|
98
|
+
}
|
99
|
+
}
|
100
|
+
)
|
101
|
+
%>
|
102
|
+
```
|
103
|
+
First parameter is Google Analytics account ID.
|
104
|
+
Second parameter defines triggers. Please see [Adding Analytics to your AMP pages](https://developers.google.com/analytics/devguides/collection/amp-analytics/)
|
105
|
+
to learn about triggers. The example above shows the most common configuration.
|
106
|
+
|
107
|
+
|
108
|
+
### Layout example
|
56
109
|
```html
|
57
110
|
<!doctype html>
|
58
111
|
<html ⚡>
|
59
112
|
<head>
|
113
|
+
<%= amp_analytics_js %>
|
60
114
|
<%= amp_js %>
|
61
115
|
<%= csrf_meta_tags %>
|
62
116
|
<%= amp_boilerplate %>
|
63
117
|
<%= amp_custom_style('amp/application') %>
|
64
118
|
</head>
|
65
119
|
<body>
|
120
|
+
<%=
|
121
|
+
amp_google_analytics(
|
122
|
+
'UA-00000-1',
|
123
|
+
{
|
124
|
+
trackPageview: {
|
125
|
+
on: 'visible',
|
126
|
+
request: 'pageview'
|
127
|
+
}
|
128
|
+
}
|
129
|
+
)
|
130
|
+
%>
|
66
131
|
<%= yield %>
|
67
132
|
</body>
|
68
133
|
</html>
|
data/lib/roll/amp.rb
CHANGED
@@ -20,7 +20,17 @@ module Roll
|
|
20
20
|
|
21
21
|
# JS in AMP.
|
22
22
|
module Script
|
23
|
-
use :
|
23
|
+
use :AnalyticsScriptTag, 'script/analytics_script_tag'
|
24
|
+
use :AmpJsTag, 'script/amp_js_tag'
|
25
|
+
use :IncludeScriptTag, 'script/include_script_tag'
|
26
|
+
use :ScriptTag, 'script/script_tag'
|
27
|
+
use :JsonScriptTag, 'script/json_script_tag'
|
28
|
+
end
|
29
|
+
|
30
|
+
# HTML in AMP.
|
31
|
+
module Html
|
32
|
+
use :AnalyticsTag, 'html/analytics_tag'
|
33
|
+
use :GoogleAnalyticsTag, 'html/google_analytics_tag'
|
24
34
|
end
|
25
35
|
|
26
36
|
# Modules with utility functions to use in Rails helpers.
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rails'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
module Roll
|
4
5
|
module Amp
|
@@ -29,6 +30,26 @@ module Roll
|
|
29
30
|
def amp_js
|
30
31
|
Script::AmpJsTag.new.to_html
|
31
32
|
end
|
33
|
+
|
34
|
+
# Renders script tag with link to AMP analytics JS.
|
35
|
+
# @return [String] HTML containing script tag.
|
36
|
+
def amp_analytics_js
|
37
|
+
Script::AnalyticsScriptTag.new.to_html
|
38
|
+
end
|
39
|
+
|
40
|
+
# Renders tags to connect Google Analytics.
|
41
|
+
# @return [String] HTML containing AMP analytics tag with script tag,
|
42
|
+
# which configures Google Analytics.
|
43
|
+
def amp_google_analytics(account, triggers = {})
|
44
|
+
Html::GoogleAnalyticsTag.new(
|
45
|
+
Script::JsonScriptTag.new({
|
46
|
+
vars: {
|
47
|
+
account: account
|
48
|
+
},
|
49
|
+
triggers: triggers
|
50
|
+
}.to_json).to_html
|
51
|
+
).to_html
|
52
|
+
end
|
32
53
|
end
|
33
54
|
end
|
34
55
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
|
3
|
+
module Roll
|
4
|
+
module Amp
|
5
|
+
module Html
|
6
|
+
# The amp-analytics tag.
|
7
|
+
class AnalyticsTag
|
8
|
+
include ActionView::Helpers::TagHelper
|
9
|
+
include ActionView::Helpers::OutputSafetyHelper
|
10
|
+
|
11
|
+
# Initializes new instance of the analytics tag.
|
12
|
+
# @param type [String] the type of AMP analytics.
|
13
|
+
# @param content [String] the content of the tag.
|
14
|
+
def initialize(type, content)
|
15
|
+
@type = type
|
16
|
+
@content = content
|
17
|
+
end
|
18
|
+
|
19
|
+
# Prints this tag as HTML.
|
20
|
+
# @return [String] HTML-safe string containing the tag's HTML view.
|
21
|
+
def to_html
|
22
|
+
content_tag('amp-analytics', raw(@content), type: @type)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Roll
|
2
|
+
module Amp
|
3
|
+
module Html
|
4
|
+
# The amp-analytics tag for Google Analytics.
|
5
|
+
class GoogleAnalyticsTag < AnalyticsTag
|
6
|
+
# Initializes new instance of the analytics tag.
|
7
|
+
# @param content [String] the content of the tag.
|
8
|
+
def initialize(content)
|
9
|
+
super(GoogleAnalyticsTag.type, content)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.type
|
13
|
+
'googleanalytics'.freeze
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,16 +1,17 @@
|
|
1
|
-
require 'action_view'
|
2
|
-
|
3
1
|
module Roll
|
4
2
|
module Amp
|
5
3
|
module Script
|
6
|
-
# The script tag with AMP
|
7
|
-
class AmpJsTag
|
8
|
-
|
4
|
+
# The script tag with AMP main JS file.
|
5
|
+
class AmpJsTag < IncludeScriptTag
|
6
|
+
# Initializes new instance of the AMP script tag.
|
7
|
+
def initialize
|
8
|
+
super(AmpJsTag.src)
|
9
|
+
end
|
9
10
|
|
10
|
-
#
|
11
|
-
#
|
12
|
-
def
|
13
|
-
|
11
|
+
# @return [String] Link to main AMP script which
|
12
|
+
# must be always included on AMP pages.
|
13
|
+
def self.src
|
14
|
+
'https://cdn.ampproject.org/v0.js'.freeze
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Roll
|
2
|
+
module Amp
|
3
|
+
module Script
|
4
|
+
# The script tag that enables analytics on AMP pages.
|
5
|
+
class AnalyticsScriptTag < IncludeScriptTag
|
6
|
+
# Initializes analytics script tag instance.
|
7
|
+
def initialize
|
8
|
+
super(AnalyticsScriptTag.src, 'amp-analytics')
|
9
|
+
end
|
10
|
+
|
11
|
+
# @return [String] Link to the script.
|
12
|
+
def self.src
|
13
|
+
'https://cdn.ampproject.org/v0/amp-analytics-0.1.js'.freeze
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
|
3
|
+
module Roll
|
4
|
+
module Amp
|
5
|
+
module Script
|
6
|
+
# The script tag to include AMP scripts. Note, it's always async.
|
7
|
+
class IncludeScriptTag
|
8
|
+
include ActionView::Helpers::OutputSafetyHelper
|
9
|
+
|
10
|
+
# Initializes new instance of the include script tag.
|
11
|
+
# @param src [String] the URL to the script.
|
12
|
+
# @param custom_element [String] the custom-element attribute value.
|
13
|
+
def initialize(src, custom_element = '')
|
14
|
+
@src = src
|
15
|
+
@custom_element = custom_element
|
16
|
+
end
|
17
|
+
|
18
|
+
# Prints this tag as HTML.
|
19
|
+
# @return [String] HTML-safe string containing the tag's HTML view.
|
20
|
+
def to_html
|
21
|
+
if @custom_element.empty?
|
22
|
+
raw("<script async src=\"#{@src}\"></script>")
|
23
|
+
else
|
24
|
+
raw(
|
25
|
+
"<script async src=\"#{@src}\" " \
|
26
|
+
"custom-element=\"#{@custom_element}\"></script>"
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Roll
|
2
|
+
module Amp
|
3
|
+
module Script
|
4
|
+
# The script tag that contains JSON.
|
5
|
+
class JsonScriptTag < ScriptTag
|
6
|
+
# Initializes script tag instance.
|
7
|
+
# @param content [String] the content of the tag.
|
8
|
+
def initialize(content)
|
9
|
+
super(JsonScriptTag.type, content)
|
10
|
+
end
|
11
|
+
|
12
|
+
# @return [String] JSON MIME type.
|
13
|
+
def self.type
|
14
|
+
'application/json'.freeze
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
|
3
|
+
module Roll
|
4
|
+
module Amp
|
5
|
+
module Script
|
6
|
+
# The script tag for embedded scripts.
|
7
|
+
class ScriptTag
|
8
|
+
include ActionView::Helpers::TagHelper
|
9
|
+
include ActionView::Helpers::OutputSafetyHelper
|
10
|
+
|
11
|
+
# Initializes new instance of the script tag.
|
12
|
+
# @param type [String] the MIME type of the script.
|
13
|
+
# @param content [String] the script's content.
|
14
|
+
def initialize(type, content)
|
15
|
+
@type = type
|
16
|
+
@content = content
|
17
|
+
end
|
18
|
+
|
19
|
+
# Prints this tag as HTML.
|
20
|
+
# @return [String] HTML-safe string containing the tag's HTML view.
|
21
|
+
def to_html
|
22
|
+
content_tag('script', raw(@content), type: @type)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -6,11 +6,12 @@ module Roll
|
|
6
6
|
# The style tag.
|
7
7
|
class StyleTag
|
8
8
|
include ActionView::Helpers::TagHelper
|
9
|
+
include ActionView::Helpers::OutputSafetyHelper
|
9
10
|
|
10
11
|
# Initializes new instance of the style tag.
|
11
12
|
# @param amp_attr [String] the AMP-specific attribute of the tag
|
12
13
|
# to indicate its purpose.
|
13
|
-
# @param [String] the content of the tag.
|
14
|
+
# @param content [String] the content of the tag.
|
14
15
|
def initialize(amp_attr, content)
|
15
16
|
@amp_attr = amp_attr
|
16
17
|
@content = content
|
@@ -19,7 +20,7 @@ module Roll
|
|
19
20
|
# Prints this tag as HTML.
|
20
21
|
# @return [String] HTML-safe string containing the tag's HTML view.
|
21
22
|
def to_html
|
22
|
-
content_tag('style', @content, @amp_attr => '')
|
23
|
+
content_tag('style', raw(@content), @amp_attr => '')
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
data/lib/roll/amp/version.rb
CHANGED
data/roll-amp.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = 'AMP in Rails apps'
|
13
13
|
spec.description = 'Utilities to simplify Accelerated Mobile Pages (AMP)'\
|
14
|
-
'implementation in Rails apps'
|
14
|
+
' implementation in Rails apps'
|
15
15
|
spec.homepage = 'https://github.com/roll-rails/roll-amp'
|
16
16
|
spec.license = 'MIT'
|
17
17
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roll-amp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Viacheslav Shynkarenko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionview
|
@@ -134,7 +134,7 @@ dependencies:
|
|
134
134
|
- - "~>"
|
135
135
|
- !ruby/object:Gem::Version
|
136
136
|
version: 0.9.1
|
137
|
-
description: Utilities to simplify Accelerated Mobile Pages (AMP)implementation in
|
137
|
+
description: Utilities to simplify Accelerated Mobile Pages (AMP) implementation in
|
138
138
|
Rails apps
|
139
139
|
email:
|
140
140
|
- shinkarenko.vi@gmail.com
|
@@ -158,7 +158,13 @@ files:
|
|
158
158
|
- lib/roll/amp/autoload.rb
|
159
159
|
- lib/roll/amp/helpers/config.reek
|
160
160
|
- lib/roll/amp/helpers/tags.rb
|
161
|
+
- lib/roll/amp/html/analytics_tag.rb
|
162
|
+
- lib/roll/amp/html/google_analytics_tag.rb
|
161
163
|
- lib/roll/amp/script/amp_js_tag.rb
|
164
|
+
- lib/roll/amp/script/analytics_script_tag.rb
|
165
|
+
- lib/roll/amp/script/include_script_tag.rb
|
166
|
+
- lib/roll/amp/script/json_script_tag.rb
|
167
|
+
- lib/roll/amp/script/script_tag.rb
|
162
168
|
- lib/roll/amp/style/boilerplate_style_tag.rb
|
163
169
|
- lib/roll/amp/style/boilerplate_style_tags_set.rb
|
164
170
|
- lib/roll/amp/style/compiled_stylesheet_file.rb
|