Sha256: fa6a356243e6fa9b6de060693b506fe20f4f1f3efa78cf69e62faf9cfcc4c7c7
Contents?: true
Size: 954 Bytes
Versions: 3
Compression:
Stored size: 954 Bytes
Contents
module SimpleAnalyticsRails::Middleware # This receives the request payload and injects the JavaScript snippet to the HTML. # It's injected just before the </head> element. class JavascriptInjection def initialize(app) @app = app end def call(env) status, headers, response = @app.call(env) if headers && headers["Content-Type"]&.include?("text/html") response = inject_javascript_to_response(response) end [status, headers, response] end private def inject_javascript_to_response(response) if SimpleAnalyticsRails.configuration.enabled? && response.respond_to?("[]") response[0].gsub!("</head>", "#{javascript_script.head_html}</head>") response[0].gsub!("</body>", "#{javascript_script.body_html}</body>") end response end def javascript_script @javascript_script ||= SimpleAnalyticsRails::JavascriptScript.new end end end
Version data entries
3 entries across 3 versions & 1 rubygems