Skip to content

Commit

Permalink
use header for test track visitor id (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
betterzega authored and samandmoore committed May 30, 2017
1 parent 1f3c228 commit 7790390
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
31 changes: 30 additions & 1 deletion app/models/test_track/session.rb
Expand Up @@ -12,6 +12,7 @@ def manage
yield
ensure
manage_cookies!
manage_response_headers!
notify_unsynced_assignments! if sync_assignments?
create_alias! if signed_up?
end
Expand Down Expand Up @@ -48,7 +49,11 @@ def sign_up!(identifier_type, identifier_value)
alias signed_up? signed_up

def visitor
@visitor ||= TestTrack::Visitor.new(id: cookies[visitor_cookie_name])
@visitor ||= TestTrack::Visitor.new(id: visitor_id)
end

def visitor_id
cookies[visitor_cookie_name] || request_headers[visitor_request_header_name]
end

def set_cookie(name, value)
Expand Down Expand Up @@ -100,10 +105,26 @@ def request
controller.request
end

def response
controller.response
end

def cookies
controller.send(:cookies)
end

def request_headers
request.headers
end

def response_headers
response.headers
end

def manage_response_headers!
response_headers[visitor_response_header_name] = visitor.id if visitor.id_overridden_by_existing_visitor?
end

def notify_unsynced_assignments!
payload = {
mixpanel_distinct_id: mixpanel_distinct_id,
Expand Down Expand Up @@ -170,6 +191,14 @@ def visitor_cookie_name
ENV['TEST_TRACK_VISITOR_COOKIE_NAME'] || 'tt_visitor_id'
end

def visitor_request_header_name
ENV['TEST_TRACK_VISITOR_REQUEST_HEADER_NAME'] || 'X-TT-Visitor-ID'
end

def visitor_response_header_name
ENV['TEST_TRACK_VISITOR_RESPONSE_HEADER_NAME'] || 'X-Set-TT-Visitor-ID'
end

def fully_qualified_cookie_domain_enabled?
ENV['TEST_TRACK_FULLY_QUALIFIED_COOKIE_DOMAIN_ENABLED'] == '1'
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/test_track/visitor.rb
Expand Up @@ -98,6 +98,10 @@ def loaded?
!offline? && @remote_visitor.present?
end

def id_overridden_by_existing_visitor?
@id_overridden_by_existing_visitor || false
end

private

def assignments
Expand All @@ -113,6 +117,7 @@ def remote_visitor
end

def merge!(other)
@id_overridden_by_existing_visitor = id != other.id
@id = other.id
@assignment_registry = assignment_registry.merge(other.assignment_registry)
@unsynced_assignments = nil
Expand Down
77 changes: 75 additions & 2 deletions spec/models/test_track/session_spec.rb
@@ -1,10 +1,12 @@
require 'rails_helper'

RSpec.describe TestTrack::Session do
let(:controller) { instance_double(ApplicationController, cookies: cookies, request: request) }
let(:controller) { instance_double(ApplicationController, cookies: cookies, request: request, response: response) }
let(:cookies) { { tt_visitor_id: "fake_visitor_id", mp_fakefakefake_mixpanel: mixpanel_cookie }.with_indifferent_access }
let(:headers) { {} }
let(:mixpanel_cookie) { { distinct_id: "fake_distinct_id", OtherProperty: "bar" }.to_json }
let(:request) { double(:request, host: "www.foo.com", ssl?: true) }
let(:request) { double(:request, host: "www.foo.com", ssl?: true, headers: headers) }
let(:response) { double(:response, headers: {}) }
let(:unsynced_assignments_notifier) { instance_double(TestTrack::UnsyncedAssignmentsNotifier, notify: true) }

subject { described_class.new(controller) }
Expand Down Expand Up @@ -59,6 +61,38 @@
expect(cookies['tt_visitor_id'][:value]).to eq "real_visitor_id"
end

context 'test track visitor id is the same as existing visitor id' do
before do
real_visitor = instance_double(TestTrack::Visitor, id: "fake_visitor_id", assignment_registry: {})
identifier = instance_double(TestTrack::Remote::Identifier, visitor: real_visitor)
allow(TestTrack::Remote::Identifier).to receive(:create!).and_return(identifier)
end

it "does not return the visitor id in the reponse header" do
subject.manage do
subject.log_in!("identifier_type", "value")
end

expect(controller.response.headers).not_to have_key('X-Set-TT-Visitor-ID')
end
end

context 'test track visitor id differs from existing visitor id' do
before do
real_visitor = instance_double(TestTrack::Visitor, id: "real_visitor_id", assignment_registry: {})
identifier = instance_double(TestTrack::Remote::Identifier, visitor: real_visitor)
allow(TestTrack::Remote::Identifier).to receive(:create!).and_return(identifier)
end

it "returns the existing visitor id in the reponse header" do
subject.manage do
subject.log_in!("identifier_type", "value")
end

expect(controller.response.headers['X-Set-TT-Visitor-ID']).to eq('real_visitor_id')
end
end

it "changes the distinct_id in the mixpanel cookie" do
subject.manage do
subject.log_in!("identifier_type", "value")
Expand All @@ -81,6 +115,45 @@
end
end

context "current visitor id is passed via the header" do
let(:cookies) { {} }
let(:headers) { { 'X-TT-Visitor-ID' => 'fake_visitor_id' } }

before do
real_visitor = instance_double(TestTrack::Visitor, id: "real_visitor_id", assignment_registry: {})
identifier = instance_double(TestTrack::Remote::Identifier, visitor: real_visitor)
allow(TestTrack::Remote::Identifier).to receive(:create!).and_return(identifier)
end

describe '#sign_up!' do
it "provides the current visitor id when requesting an identifier" do
subject.manage do
subject.sign_up!("identifier_type", "value")

expect(TestTrack::Remote::Identifier).to have_received(:create!).with(
identifier_type: "identifier_type",
visitor_id: "fake_visitor_id",
value: "value"
)
end
end
end

describe '#log_in!' do
it "provides the current visitor id when requesting an identifier" do
subject.manage do
subject.log_in!("identifier_type", "value")

expect(TestTrack::Remote::Identifier).to have_received(:create!).with(
identifier_type: "identifier_type",
visitor_id: "fake_visitor_id",
value: "value"
)
end
end
end
end

context "mixpanel" do
it "resets the mixpanel cookie to the same value if already there" do
subject.manage {}
Expand Down

0 comments on commit 7790390

Please sign in to comment.