FEATURE: allow view exclusion using custom header

Set Discourse-Track-View to either "0" or "false" to exclude request
This commit is contained in:
Sam 2015-02-26 11:40:57 +11:00
parent 3e2ba5b30b
commit cbe18eb0df
2 changed files with 29 additions and 3 deletions

View file

@ -51,12 +51,18 @@ class Middleware::RequestTracker
helper = Middleware::AnonymousCache::Helper.new(env)
request = Rack::Request.new(env)
env_track_view = env[TRACK_VIEW]
track_view = status == 200
track_view &&= env_track_view != "0".freeze && env_track_view != "false".freeze
track_view &&= env_track_view || (request.get? && !request.xhr? && headers[CONTENT_TYPE] =~ /text\/html/)
{
status: status,
is_crawler: helper.is_crawler?,
has_auth_cookie: helper.has_auth_cookie?,
is_background: request.path =~ /^\/message-bus\// || request.path == /\/topics\/timings/,
track_view: (env[TRACK_VIEW] || (request.get? && !request.xhr? && headers[CONTENT_TYPE] =~ /text\/html/)) && status == 200
track_view: track_view
}
end

View file

@ -13,10 +13,30 @@ describe Middleware::RequestTracker do
end
context "log_request" do
it "can log requests correctly" do
before do
freeze_time Time.now
ApplicationRequest.clear_cache!
end
def log_tracked_view(val)
data = Middleware::RequestTracker.get_data(env(
"HTTP_DISCOURSE_TRACK_VIEW" => val
), ["200",{"Content-Type" => 'text/html'}])
Middleware::RequestTracker.log_request(data)
end
it "can exclude/include based on custom header" do
log_tracked_view("true")
log_tracked_view("1")
log_tracked_view("false")
log_tracked_view("0")
ApplicationRequest.write_cache!
ApplicationRequest.page_view_anon.first.count.should == 2
end
it "can log requests correctly" do
data = Middleware::RequestTracker.get_data(env(
"HTTP_USER_AGENT" => "AdsBot-Google (+http://www.google.com/adsbot.html)"