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
8 changes: 6 additions & 2 deletions lib/code_corps/analytics/segment_tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ defmodule CodeCorps.Analytics.SegmentTracker do
@doc """
Calls `track` in the configured API module.
"""
@spec track(String.t, atom, struct) :: any
def track(user_id, action, data) do
@spec track(integer, atom | String.t, struct) :: any
def track(user_id, action, data) when is_atom(action) do
event = SegmentEventNameBuilder.build(action, data)
traits = SegmentTraitsBuilder.build(data)
@api.track(user_id, event, traits)
end
def track(user_id, event, data) when is_binary(event) do
traits = SegmentTraitsBuilder.build(data)
@api.track(user_id, event, traits)
end
end
7 changes: 6 additions & 1 deletion lib/code_corps/analytics/segment_traits_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ defmodule CodeCorps.Analytics.SegmentTraitsBuilder do
created_at: user.inserted_at,
email: user.email,
first_name: user.first_name,
github_id: user.github_id,
github_username: user.github_username,
last_name: user.last_name,
sign_up_context: user.sign_up_context,
state: user.state,
twitter: user.twitter,
username: user.username
type: user.type,
username: user.username,
website: user.website
}
end

Expand Down
4 changes: 2 additions & 2 deletions lib/code_corps/model/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ defmodule CodeCorps.User do
field :password, :string, virtual: true
field :password_confirmation, :string, virtual: true
field :sign_up_context, :string, default: "default"
field :state, :string, default: "signed_up"
field :state_transition, :string, virtual: true
field :twitter, :string
field :type, :string, default: "user"
field :username, :string
field :website, :string
field :state, :string, default: "signed_up"
field :state_transition, :string, virtual: true

has_one :slugged_route, SluggedRoute

Expand Down
10 changes: 8 additions & 2 deletions lib/code_corps_web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
defmodule CodeCorpsWeb.UserController do
use CodeCorpsWeb, :controller

alias CodeCorps.{Helpers.Query, Services.UserService, User}
alias CodeCorps.GitHub
alias CodeCorps.{
Analytics,
GitHub,
Helpers.Query,
Services.UserService,
User
}

action_fallback CodeCorpsWeb.FallbackController
plug CodeCorpsWeb.Plug.DataToAttributes
Expand Down Expand Up @@ -48,6 +53,7 @@ defmodule CodeCorpsWeb.UserController do
current_user = Guardian.Plug.current_resource(conn)
with {:ok, user} <- GitHub.User.connect(current_user, code, state)
do
Analytics.SegmentTracker.track(user.id, "Connected to GitHub", user)
conn |> render("show.json-api", data: user)
end
end
Expand Down
13 changes: 13 additions & 0 deletions test/lib/code_corps_web/controllers/user_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ defmodule CodeCorpsWeb.UserControllerTest do

describe "github_oauth" do
@attrs %{"code" => "foo", "state" => "bar"}

@tag :authenticated
test "return the user when current user connects successfully", %{conn: conn, current_user: current_user} do
path = user_path(conn, :github_oauth)
Expand All @@ -257,6 +258,18 @@ defmodule CodeCorpsWeb.UserControllerTest do
assert json["data"]["attributes"]["github-id"]
end

@tag :authenticated
test "tracks event on segment when current user connects successfully", %{conn: conn, current_user: %{id: id}} do
path = user_path(conn, :github_oauth)

assert conn |> post(path, @attrs) |> json_response(200)
expected_data =
User
|> Repo.get(id)
|> CodeCorps.Analytics.SegmentTraitsBuilder.build
assert_received {:track, ^id, "Connected to GitHub", ^expected_data}
end

test "requires authentication", %{conn: conn} do
path = user_path(conn, :github_oauth)
assert conn |> post(path, @attrs) |> json_response(401)
Expand Down