Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/commands/solution/complete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def call
award_trophies!
record_activity!
log_metric!
track_exercise_completed!
end

private
Expand Down Expand Up @@ -52,6 +53,24 @@ def log_metric!
Metric::Queue.(:complete_solution, solution.completed_at, solution:, track:, user:)
end

# This is only reached when this call completed the solution, so the
# event isn't sent again for a solution that was already completed. The
# job works out the user's activity, which refreshes their person
# properties and adds exercises_completed (including this one) to the
# event.
def track_exercise_completed!
Analytics::TrackEvent.defer(
user,
"exercise_completed",
properties: {
track: track.slug,
exercise: exercise.slug
},
set_person_properties: true,
snapshot_activity: true
)
end

memoize
def user = solution.user

Expand Down
57 changes: 57 additions & 0 deletions test/commands/solution/complete_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,63 @@ class Solution::CompleteTest < ActiveSupport::TestCase
end
end

test "sends exercise_completed" do
exercise = create :practice_exercise, slug: "bob"

user = create :user
user_track = create :user_track, user:, track: exercise.track
solution = create(:practice_solution, user:, exercise:)
create(:iteration, solution:)

Analytics::TrackEvent.expects(:defer).with(
user,
"exercise_completed",
properties: { track: exercise.track.slug, exercise: "bob" },
set_person_properties: true,
snapshot_activity: true
)

Solution::Complete.(solution, user_track)
end

test "doesn't send exercise_completed when solution has already been completed" do
exercise = create :practice_exercise

user = create :user
user_track = create :user_track, user:, track: exercise.track
solution = create(:practice_solution, :completed, user:, exercise:)
create(:iteration, solution:)

Analytics::TrackEvent.expects(:defer).never

Solution::Complete.(solution, user_track)
end

test "exercise_completed counts this completion and flips completed_non_hello_world" do
user = create :user
track = create :track
user_track = create(:user_track, user:, track:)
create(:hello_world_solution, :completed, user:, track:)
exercise = create(:practice_exercise, track:, slug: "bob")
solution = create(:practice_solution, user:, track:, exercise:)
create(:iteration, solution:)

client = mock
Analytics.stubs(:client).returns(client)
client.expects(:capture).with do |args|
properties = args[:properties]
args[:event] == "exercise_completed" &&
properties[:exercise] == "bob" &&
properties[:exercises_completed] == 2 &&
properties[:"$set"][:exercises_completed] == 2 &&
properties[:"$set"][:completed_non_hello_world] == true
end

Solution::Complete.(solution, user_track)

perform_enqueued_jobs
end

test "awards anybody there badge when hello-world exercise is completed in five tracks" do
user = create :user
user_track = create(:user_track, user:)
Expand Down
Loading