Skip to content

Commit adbb0e9

Browse files
committed
Added tracking for GithubAppInstallation created
1 parent 09350f3 commit adbb0e9

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

lib/code_corps/analytics/segment_traits_builder.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ defmodule CodeCorps.Analytics.SegmentTraitsBuilder do
2525
project_id: donation_goal.project_id
2626
}
2727
end
28+
defp traits(%CodeCorps.GithubAppInstallation{} = installation) do
29+
%{
30+
access_token_expires_at: installation.access_token_expires_at,
31+
github_account_login: installation.github_account_login,
32+
github_account_type: installation.github_account_type,
33+
github_id: installation.github_id,
34+
origin: installation.origin,
35+
state: installation.state,
36+
project_id: installation.project_id,
37+
user_id: installation.user_id
38+
}
39+
end
2840
defp traits(%CodeCorps.ProjectSkill{} = record) do
2941
record = record |> Repo.preload([:project, :skill])
3042
%{

lib/code_corps_web/controllers/github_app_installation_controller.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule CodeCorpsWeb.GithubAppInstallationController do
44

55
import CodeCorps.Helpers.Query, only: [id_filter: 2]
66

7-
alias CodeCorps.{GithubAppInstallation, User}
7+
alias CodeCorps.{Analytics.SegmentTracker, GithubAppInstallation, User}
88

99
action_fallback CodeCorpsWeb.FallbackController
1010
plug CodeCorpsWeb.Plug.DataToAttributes
@@ -29,7 +29,14 @@ defmodule CodeCorpsWeb.GithubAppInstallationController do
2929
with %User{} = current_user <- conn |> Guardian.Plug.current_resource,
3030
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %GithubAppInstallation{}, params),
3131
{:ok, %GithubAppInstallation{} = installation} <- %GithubAppInstallation{} |> GithubAppInstallation.create_changeset(params) |> Repo.insert do
32+
33+
current_user |> track_created(installation)
3234
conn |> put_status(:created) |> render("show.json-api", data: installation)
3335
end
3436
end
37+
38+
@spec track_created(User.t, GithubAppInstallation.t) :: any
39+
defp track_created(%User{id: user_id}, %GithubAppInstallation{} = installation) do
40+
user_id |> SegmentTracker.track("Created GithubAppInstallation", installation)
41+
end
3542
end

test/lib/code_corps/analytics/segment_traits_builder_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ defmodule CodeCorps.Analytics.SegmentTraitsBuilderTest do
1616
assert :comment |> insert |> SegmentTraitsBuilder.build
1717
assert :donation_goal |> insert |> SegmentTraitsBuilder.build
1818

19+
assert :github_app_installation |> insert |> SegmentTraitsBuilder.build
20+
1921
assert :project_skill |> insert |> SegmentTraitsBuilder.build
2022
assert :project_user |> insert |> SegmentTraitsBuilder.build
2123

test/lib/code_corps_web/controllers/github_app_installation_controller_test.exs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule CodeCorpsWeb.GithubAppInstallationControllerTest do
33

44
use CodeCorpsWeb.ApiCase, resource_name: :github_app_installation
55

6+
alias CodeCorps.{Analytics.SegmentTraitsBuilder, GithubAppInstallation, Repo}
7+
68
describe "index" do
79
test "lists all resources", %{conn: conn} do
810
[record_1, record_2] = insert_pair(:github_app_installation)
@@ -49,6 +51,19 @@ defmodule CodeCorpsWeb.GithubAppInstallationControllerTest do
4951
assert conn |> request_create(attrs) |> json_response(201)
5052
end
5153

54+
@tag :authenticated
55+
test "tracks creation", %{conn: conn, current_user: current_user} do
56+
project = insert(:project)
57+
insert(:project_user, project: project, user: current_user, role: "owner")
58+
attrs = %{project: project, user: current_user}
59+
60+
conn |> request_create(attrs)
61+
62+
user_id = current_user.id
63+
traits = GithubAppInstallation |> Repo.one |> SegmentTraitsBuilder.build
64+
assert_receive({:track, ^user_id, "Created GithubAppInstallation", ^traits})
65+
end
66+
5267
@tag :authenticated
5368
test "does not create resource and renders 422 when data is invalid", %{conn: conn, current_user: user} do
5469
project = insert(:project)

0 commit comments

Comments
 (0)