From 76cacc6eae39fd29c157fa152012dae7bbfbddcc Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Wed, 18 Oct 2017 16:08:58 +0200 Subject: [PATCH] Added tracking of "Connected to GitHub" event to segment --- lib/code_corps/analytics/segment_tracker.ex | 8 ++++++-- lib/code_corps/analytics/segment_traits_builder.ex | 7 ++++++- lib/code_corps/model/user.ex | 4 ++-- lib/code_corps_web/controllers/user_controller.ex | 10 ++++++++-- .../controllers/user_controller_test.exs | 13 +++++++++++++ 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/code_corps/analytics/segment_tracker.ex b/lib/code_corps/analytics/segment_tracker.ex index fa8fba9d7..6f41c9470 100644 --- a/lib/code_corps/analytics/segment_tracker.ex +++ b/lib/code_corps/analytics/segment_tracker.ex @@ -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 diff --git a/lib/code_corps/analytics/segment_traits_builder.ex b/lib/code_corps/analytics/segment_traits_builder.ex index 3e1a7f915..11e03a8a4 100644 --- a/lib/code_corps/analytics/segment_traits_builder.ex +++ b/lib/code_corps/analytics/segment_traits_builder.ex @@ -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 diff --git a/lib/code_corps/model/user.ex b/lib/code_corps/model/user.ex index 47422af00..4845dca4d 100644 --- a/lib/code_corps/model/user.ex +++ b/lib/code_corps/model/user.ex @@ -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 diff --git a/lib/code_corps_web/controllers/user_controller.ex b/lib/code_corps_web/controllers/user_controller.ex index 11d55194c..205d6f2ba 100644 --- a/lib/code_corps_web/controllers/user_controller.ex +++ b/lib/code_corps_web/controllers/user_controller.ex @@ -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 @@ -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 diff --git a/test/lib/code_corps_web/controllers/user_controller_test.exs b/test/lib/code_corps_web/controllers/user_controller_test.exs index 52690c5ad..cc55547df 100644 --- a/test/lib/code_corps_web/controllers/user_controller_test.exs +++ b/test/lib/code_corps_web/controllers/user_controller_test.exs @@ -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) @@ -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)