Embeds javascript into the head to track page view with app insights

This commit is contained in:
Tatsuya Sato 2018-12-25 21:19:35 +09:00
Родитель 8d3cf6696f
Коммит 6ea7e4a76a
2 изменённых файлов: 45 добавлений и 0 удалений

Просмотреть файл

@ -5,5 +5,6 @@ require_relative 'application_insights/version'
module ApplicationInsights
module Rack
autoload :TrackRequest, "application_insights/rack/track_request"
autoload :TrackPageView, "application_insights/rack/track_page_view"
end
end

Просмотреть файл

@ -0,0 +1,44 @@
require 'rack'
module ApplicationInsights
module Rack
class TrackPageView
def initialize(app, instrumentation_key)
@app = app
@instrumentation_key = instrumentation_key
end
def call(env)
status, headers, response = @app.call(env)
content_type = headers['Content-Type']
return [status, headers, response] unless content_type && content_type.include?('text/html')
unless response.is_a?(::Rack::Response)
response = ::Rack::Response.new(response, status, headers)
end
new_response = ::Rack::Response.new([], response.status, response.headers)
response.body.each do |b|
new_response.write(b.sub(/<\/head>/, "\n#{embedded_script_code}\n</head>"))
end
[new_response.status, new_response.headers, new_response]
end
private
def embedded_script_code
<<~"SCRIPT"
<script type="text/javascript">
var appInsights=window.appInsights||function(a){
function b(a){c[a]=function(){var b=arguments;c.queue.push(function(){c[a].apply(c,b)})}}var c={config:a},d=document,e=window;setTimeout(function(){var b=d.createElement("script");b.src=a.url||"https://az416426.vo.msecnd.net/scripts/a/ai.0.js",d.getElementsByTagName("script")[0].parentNode.appendChild(b)});try{c.cookie=d.cookie}catch(a){}c.queue=[];for(var f=["Event","Exception","Metric","PageView","Trace","Dependency"];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext"),b("clearAuthenticatedUserContext"),b("startTrackEvent"),b("stopTrackEvent"),b("startTrackPage"),b("stopTrackPage"),b("flush"),!a.disableExceptionTracking){f="onerror",b("_"+f);var g=e[f];e[f]=function(a,b,d,e,h){var i=g&&g(a,b,d,e,h);return!0!==i&&c["_"+f](a,b,d,e,h),i}}return c
}({
instrumentationKey:"#{@instrumentation_key}"
});
window.appInsights=appInsights,appInsights.queue&&0===appInsights.queue.length&&appInsights.trackPageView();
</script>
SCRIPT
end
end
end
end