Skip to content

Commit

Permalink
Switch to analytics tracking alongside ja_resource to places that los…
Browse files Browse the repository at this point in the history
…t tracking
  • Loading branch information
begedin committed Oct 12, 2016
1 parent caa3651 commit 2a990e3
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 11 deletions.
9 changes: 7 additions & 2 deletions lib/code_corps/analytics/in_memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ defmodule CodeCorps.Analytics.InMemory do
def identify(_user) do
end

def track(conn, _event, _struct), do: conn
def track(conn, _event), do: conn
def track(%Plug.Conn{} = conn, _event, _struct), do: conn
def track(%Plug.Conn{} = conn, _event), do: conn

# tracking with ja_resource
def track({:ok, model}, _action, _conn), do: {:ok, model}
def track({:error, %Ecto.Changeset{} = changeset}, _action, _conn), do: {:error, changeset}
def track({:error, errors}, :deleted, _conn), do: {:error, errors}
end
59 changes: 54 additions & 5 deletions lib/code_corps/analytics/segment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule CodeCorps.Analytics.Segment do
alias CodeCorps.UserCategory
alias CodeCorps.UserRole
alias CodeCorps.UserSkill
alias Ecto.Changeset

def identify(user = %User{}) do
Segment.Analytics.identify(user.id, traits(user))
Expand Down Expand Up @@ -47,9 +48,6 @@ defmodule CodeCorps.Analytics.Segment do
def track(conn, :removed, user_skill = %UserSkill{}) do
conn |> do_track("Removed User Skill", properties(user_skill))
end
def track(conn, _event, _struct) do
conn # return conn without event to track
end

def track(conn, :updated_profile) do
conn |> do_track("Updated Profile")
Expand All @@ -63,14 +61,65 @@ defmodule CodeCorps.Analytics.Segment do
def track(conn, :signed_up) do
conn |> do_track("Signed Up")
end
def track(conn, _event) do
def track(%Plug.Conn{} = conn, _event) do
conn # return conn without event to track
end

# switch to tracking in the context of ja_resource

@actions_without_properties [:updated_profile, :signed_in, :signed_out, :signed_up]

def track({:ok, record}, action, %Plug.Conn{} = conn) when action in @actions_without_properties do
action_name = get_action_name(action, record)
do_track(conn, action_name)

{:ok, record}
end

def track({:ok, record}, action, %Plug.Conn{} = conn) do
action_name = get_action_name(action, record)
do_track(conn, action_name, properties(record))

{:ok, record}
end

def track({:error, %Changeset{} = changeset}, _action, _conn), do: {:error, changeset}
def track({:error, errors}, :deleted, _conn), do: {:error, errors}

def get_action_name(action, _) when action in @actions_without_properties do
friendly_action_name(action)
end

def get_action_name(action, model) do
[friendly_action_name(action), friendly_model_name(model)] |> Enum.join(" ")
end

defp friendly_action_name(:deleted), do: "Removed"
defp friendly_action_name(action) do
action
|> Atom.to_string
|> String.split("_")
|> Enum.map(&String.capitalize/1)
|> Enum.join(" ")
end

defp friendly_model_name(model) do
model.__struct__
|> Module.split
|> List.last
|> Macro.underscore
|> String.split("_")
|> Enum.map(&String.capitalize/1)
|> Enum.join(" ")
end

# end switch to tracking in the context of ja_resource

defp do_track(conn, event_name, properties) do
Segment.Analytics.track(conn.assigns[:current_user].id, event_name, properties)
conn
conn |> Plug.Conn.assign(:tracked, event_name)
end

defp do_track(conn, event_name) do
Segment.Analytics.track(conn.assigns[:current_user].id, event_name, %{})
conn
Expand Down
17 changes: 17 additions & 0 deletions test/lib/code_corps/analytics/segment_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule CodeCorps.Analytics.SegmentTest do
use ExUnit.Case, async: true

import CodeCorps.Analytics.Segment, only: [get_action_name: 2]
import CodeCorps.Factories

test "get_action_name/2" do
assert get_action_name(:created, build(:comment)) == "Created Comment"
assert get_action_name(:updated, build(:comment)) == "Updated Comment"

assert get_action_name(:deleted, build(:user_skill)) == "Removed User Skill"
assert get_action_name(:deleted, build(:user_role)) == "Removed User Role"
assert get_action_name(:deleted, build(:user_category)) == "Removed User Category"

assert get_action_name(:signed_up, build(:user)) == "Signed Up"
end
end
14 changes: 12 additions & 2 deletions web/controllers/comment_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ defmodule CodeCorps.CommentController do

plug JaResource

def handle_create(_conn, attributes) do
Comment.create_changeset(%Comment{}, attributes)
def handle_create(conn, attributes) do
%Comment{}
|> Comment.create_changeset(attributes)
|> Repo.insert
|> @analytics.track(:created, conn)
end

def handle_update(conn, comment, attributes) do
comment
|> Comment.changeset(attributes)
|> Repo.update
|> @analytics.track(:updated, conn)
end
end
6 changes: 6 additions & 0 deletions web/controllers/user_category_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ defmodule CodeCorps.UserCategoryController do
|> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset)
end
end

def handle_delete(conn, record) do
record
|> Repo.delete
|> @analytics.track(:deleted, conn)
end
end
7 changes: 5 additions & 2 deletions web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ defmodule CodeCorps.UserController do
query |> id_filter(id_list)
end

def handle_create(_conn, attributes) do
User.registration_changeset(%User{}, attributes)
def handle_create(conn, attributes) do
%User{}
|> User.registration_changeset(attributes)
|> Repo.insert
|> @analytics.track(:signed_up, conn)
end

def update(conn, %{"id" => id, "data" => data = %{"type" => "user", "attributes" => _user_params}}) do
Expand Down
6 changes: 6 additions & 0 deletions web/controllers/user_role_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ defmodule CodeCorps.UserRoleController do
|> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset)
end
end

def handle_delete(conn, record) do
record
|> Repo.delete
|> @analytics.track(:deleted, conn)
end
end
6 changes: 6 additions & 0 deletions web/controllers/user_skill_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ defmodule CodeCorps.UserSkillController do
|> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset)
end
end

def handle_delete(conn, record) do
record
|> Repo.delete
|> @analytics.track(:deleted, conn)
end
end

0 comments on commit 2a990e3

Please sign in to comment.