simple_analytics_rails 0.1.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.
@@ -0,0 +1,34 @@
1
+ require_relative "simple_analytics_rails/version"
2
+ require_relative "simple_analytics_rails/configuration"
3
+ require_relative "simple_analytics_rails/javascript_script"
4
+ require_relative "simple_analytics_rails/middleware/javascript_injection"
5
+ require_relative "simple_analytics_rails/railtie" if defined?(Rails)
6
+
7
+ # This will automatically inject the `SimpleAnalyticsRails::Middleware::JavascriptInjection` middleware if you're using Ruby on Rails.
8
+ #
9
+ # Sample Usage for configuration:
10
+ #
11
+ # # config/initializers/simple_analytics.rb
12
+ # SimpleAnalyticsRails.configure do |configuration|
13
+ # configuration.hostname = "example.com"
14
+ # configuration.mode = "hash"
15
+ # configuration.collect_dnt = false
16
+ # configuration.ignore_pages = "/search/*,/account/*,/vouchers"
17
+ # configuration.enabled = Rails.env.production?
18
+ # end
19
+ #
20
+ module SimpleAnalyticsRails
21
+ class << self
22
+ def configure
23
+ yield(configuration)
24
+ end
25
+
26
+ def configuration
27
+ @configuration ||= SimpleAnalyticsRails::Configuration.new
28
+ end
29
+
30
+ def reset_configuration!
31
+ @configuration = nil
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ module SimpleAnalyticsRails
2
+ class Configuration
3
+ def initialize
4
+ @hostname = ""
5
+ @mode = ""
6
+ @ignore_pages = ""
7
+ @collect_dnt = false
8
+ @enabled = true
9
+ end
10
+
11
+ attr_accessor :ignore_pages
12
+ attr_accessor :hostname
13
+ attr_accessor :mode
14
+ attr_writer :collect_dnt
15
+ def collect_dnt?
16
+ @collect_dnt
17
+ end
18
+
19
+ attr_writer :enabled
20
+ def enabled?
21
+ @enabled
22
+ end
23
+
24
+ def to_h
25
+ {
26
+ hostname: @hostname,
27
+ mode: @mode,
28
+ collect_dnt: collect_dnt? ? "true" : nil,
29
+ ignore_pages: @ignore_pages,
30
+ turbolinks_track: turbolinks_track
31
+ }.compact.reject { |_key, value| value.blank? }
32
+ end
33
+
34
+ private
35
+
36
+ def turbolinks_track
37
+ "reload" if defined?(Turbolinks)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,41 @@
1
+ module SimpleAnalyticsRails
2
+ # This outputs the HTML script with the configuration option added to the <script> tag.
3
+ class JavascriptScript
4
+ def head_html
5
+ [
6
+ sa_event_script,
7
+ script_tag
8
+ ].join
9
+ end
10
+
11
+ def body_html
12
+ '<noscript><img src="https://queue.simpleanalyticscdn.com/noscript.gif" alt="" referrerpolicy="no-referrer-when-downgrade" /></noscript>'
13
+ end
14
+
15
+ private
16
+
17
+ def sa_event_script
18
+ [
19
+ "<script>",
20
+ "window.sa_event=window.sa_event||function(){a=[].slice.call(arguments);sa_event.q?sa_event.q.push(a):sa_event.q=[a]};",
21
+ "</script>"
22
+ ].join
23
+ end
24
+
25
+ def script_tag
26
+ [
27
+ "<script",
28
+ configuration_to_html_attributes,
29
+ 'async defer src="https://scripts.simpleanalyticscdn.com/latest.js"></script>'
30
+ ].compact
31
+ .reject(&:blank?)
32
+ .join(" ")
33
+ end
34
+
35
+ def configuration_to_html_attributes
36
+ SimpleAnalyticsRails.configuration.to_h.collect do |key, value|
37
+ "data-#{key.to_s.tr("_", "-")}=\"#{CGI.escapeHTML(value)}\""
38
+ end.join(" ")
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,34 @@
1
+ module SimpleAnalyticsRails::Middleware
2
+ # This receives the request payload and injects the JavaScript snippet to the HTML.
3
+ # It's injected just before the </head> element.
4
+ class JavascriptInjection
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ status, headers, response = @app.call(env)
11
+
12
+ if headers && headers["Content-Type"]&.include?("text/html")
13
+ response = inject_javascript_to_response(response)
14
+ end
15
+
16
+ [status, headers, response]
17
+ end
18
+
19
+ private
20
+
21
+ def inject_javascript_to_response(response)
22
+ if SimpleAnalyticsRails.configuration.enabled? && response.respond_to?("[]")
23
+ response[0].gsub!("</head>", "#{javascript_script.head_html}</head>")
24
+ response[0].gsub!("</body>", "#{javascript_script.body_html}</body>")
25
+ end
26
+
27
+ response
28
+ end
29
+
30
+ def javascript_script
31
+ @javascript_script ||= SimpleAnalyticsRails::JavascriptScript.new
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ # This is the class which injects our Middleware into Rails apps.
2
+ # If `Rails` isn't defined this class isn't included. If you're using other Rack apps you can include the Middleware classes manually.
3
+ class SimpleAnalyticsRails::Railtie < Rails::Railtie
4
+ initializer "simple_analytics_rails.configure_rails_initialization" do |app|
5
+ app.config.middleware.insert_after(ActionDispatch::DebugExceptions, SimpleAnalyticsRails::Middleware::JavascriptInjection)
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleAnalyticsRails
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "lib/simple_analytics_rails/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "simple_analytics_rails"
5
+ spec.version = SimpleAnalyticsRails::VERSION
6
+ spec.authors = ["Mike Rogers"]
7
+ spec.email = ["me@mikerogers.io"]
8
+
9
+ spec.summary = "Add the Simple Analytics JavaScript Script to your Rails app"
10
+ spec.description = spec.summary
11
+ spec.homepage = "https://simpleanalytics.com/"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/simpleanalytics/rubyonrails-plugin"
17
+ spec.metadata["changelog_uri"] = "https://github.com/simpleanalytics/rubyonrails-plugin/blob/main/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "appraisal", "~> 2.4"
29
+ spec.add_development_dependency "rails", ">= 5.2"
30
+ spec.add_development_dependency "rake", "~> 13.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "rspec-rails", "~> 4.0"
33
+ spec.add_development_dependency "standardrb", "~> 1.0"
34
+ spec.add_development_dependency "yard", "~> 0.9"
35
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_analytics_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Rogers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: appraisal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '4.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '4.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: standardrb
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ description: Add the Simple Analytics JavaScript Script to your Rails app
112
+ email:
113
+ - me@mikerogers.io
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".github/workflows/changelog.yml"
119
+ - ".github/workflows/publish.yml"
120
+ - ".github/workflows/standard.yml"
121
+ - ".github/workflows/tests.yml"
122
+ - ".gitignore"
123
+ - ".rspec"
124
+ - ".yardopts"
125
+ - Appraisals
126
+ - CHANGELOG.md
127
+ - CODE_OF_CONDUCT.md
128
+ - Gemfile
129
+ - LICENSE
130
+ - README.md
131
+ - Rakefile
132
+ - bin/console
133
+ - bin/setup
134
+ - gemfiles/.bundle/config
135
+ - gemfiles/rails52.gemfile
136
+ - gemfiles/rails52.gemfile.lock
137
+ - gemfiles/rails60.gemfile
138
+ - gemfiles/rails60.gemfile.lock
139
+ - gemfiles/rails61.gemfile
140
+ - gemfiles/rails61.gemfile.lock
141
+ - gemfiles/rails_main.gemfile
142
+ - gemfiles/rails_main.gemfile.lock
143
+ - lib/simple_analytics_rails.rb
144
+ - lib/simple_analytics_rails/configuration.rb
145
+ - lib/simple_analytics_rails/javascript_script.rb
146
+ - lib/simple_analytics_rails/middleware/javascript_injection.rb
147
+ - lib/simple_analytics_rails/railtie.rb
148
+ - lib/simple_analytics_rails/version.rb
149
+ - simple_analytics_rails.gemspec
150
+ homepage: https://simpleanalytics.com/
151
+ licenses:
152
+ - MIT
153
+ metadata:
154
+ homepage_uri: https://simpleanalytics.com/
155
+ source_code_uri: https://github.com/simpleanalytics/rubyonrails-plugin
156
+ changelog_uri: https://github.com/simpleanalytics/rubyonrails-plugin/blob/main/CHANGELOG.md
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: 2.5.0
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubygems_version: 3.2.15
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Add the Simple Analytics JavaScript Script to your Rails app
176
+ test_files: []