Skip to content

Bring back ahoy gem to track visits#803

Merged
brendansudol merged 1 commit intomasterfrom
mb-track-visits
Dec 2, 2016
Merged

Bring back ahoy gem to track visits#803
brendansudol merged 1 commit intomasterfrom
mb-track-visits

Conversation

@monfresh
Copy link
Contributor

@monfresh monfresh commented Dec 1, 2016

Why: To be able to associate visits with users, so we can track
them from unauthenticated to authenticated state, and to make it easier
to count unique visits.

@jessieay
Copy link
Contributor

jessieay commented Dec 1, 2016

Is there a github issue associated with this PR? (I understand what this does but not why we are adding it now)

@monfresh
Copy link
Contributor Author

monfresh commented Dec 2, 2016

We are adding it back because it was a mistake to remove it. I thought that we could get by with just using logging to publish events, but being able to track visits is important, and Ahoy is the easiest way to do that currently. As a follow up, I will see if it makes sense to extract just the visit tracking code from Ahoy and implement it in our app.

**Why**: To be able to associate visits with users, so we can track
them from unauthenticated to authenticated state, and to make it easier
to count unique visits.

def ahoy
@ahoy ||= Rails.env.test? ? FakeAhoyTracker.new : Ahoy::Tracker.new(request: request)
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zachmargolis I know you had made a comment about this technique in a previous PR, where you suggested to use stub_const in tests, but it caused the tests to fail and caused me some head scratching due to an error message that didn't make sense and included the Ahoy gem in the stack trace.

The problem was that I was stubbing FakeAhoyTracker for Ahoy::Tracker, and because I didn't have an initialize method defined in FakeAhoyTracker, it complained about wrong number of arguments, pointing to Ahoy's controller initializer. It said 1 argument was passed, but 0 were expected. That didn't make sense because Ahoy::Tracker clearly accepts arguments. Finally it dawned on me that the tests were using FakeAhoyTracker, and stubbing the const meant that I had to implement all the methods called by the real class in my fake class.

The whole point of the fake class is so that the tests use that class and only that class, and it shouldn't be necessary to implement all the methods from the real class in the fake class. I don't want the tests to touch the real class at all, which is why I prefer this way of doing it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that seems fair enough, would we have to implement all the methods or just a no-op initialize?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we could do a double().as_null_object? But up to you!

@brendansudol brendansudol merged commit a552de7 into master Dec 2, 2016
@brendansudol brendansudol deleted the mb-track-visits branch December 2, 2016 16:01

let(:ahoy) { instance_double(FakeAhoyTracker) }

before { allow(FakeAhoyTracker).to receive(:new).and_return(ahoy) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we stubbed Ahoy::Tracker instead of FakeAhoyTracker here, would that make it possible to remove the env check in Analytics ?

@@ -0,0 +1,5 @@
class FakeAhoyTracker
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not opposed to this strategy, but it probably makes sense to have a test that confirms that FakeAhoyTracker and Ahoy::Tracker have the same interface.

As is, I can imagine a scenario in which the ahoy lib requires new/different args for initialize, for example, but the specs still pass without a change. Am I making sense?

@jessieay
Copy link
Contributor

jessieay commented Dec 2, 2016

woops just reviewed after this was merged accidentally. Feel free to consider or ignore my comments as you see fit.

amoose pushed a commit that referenced this pull request Feb 24, 2017
**Why**: To be able to associate visits with users, so we can track
them from unauthenticated to authenticated state, and to make it easier
to count unique visits.
amoose pushed a commit that referenced this pull request Feb 28, 2017
**Why**: To be able to associate visits with users, so we can track
them from unauthenticated to authenticated state, and to make it easier
to count unique visits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants