From 2a990e3e1ca2651100cf46c57ac936042487bdb1 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Wed, 12 Oct 2016 11:50:56 +0200 Subject: [PATCH] Switch to analytics tracking alongside ja_resource to places that lost tracking --- lib/code_corps/analytics/in_memory.ex | 9 ++- lib/code_corps/analytics/segment.ex | 59 +++++++++++++++++-- .../lib/code_corps/analytics/segment_test.exs | 17 ++++++ web/controllers/comment_controller.ex | 14 ++++- web/controllers/user_category_controller.ex | 6 ++ web/controllers/user_controller.ex | 7 ++- web/controllers/user_role_controller.ex | 6 ++ web/controllers/user_skill_controller.ex | 6 ++ 8 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 test/lib/code_corps/analytics/segment_test.exs diff --git a/lib/code_corps/analytics/in_memory.ex b/lib/code_corps/analytics/in_memory.ex index f4ad6880a..c824ee9c5 100644 --- a/lib/code_corps/analytics/in_memory.ex +++ b/lib/code_corps/analytics/in_memory.ex @@ -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 diff --git a/lib/code_corps/analytics/segment.ex b/lib/code_corps/analytics/segment.ex index 2748c6671..046565efe 100644 --- a/lib/code_corps/analytics/segment.ex +++ b/lib/code_corps/analytics/segment.ex @@ -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)) @@ -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") @@ -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 diff --git a/test/lib/code_corps/analytics/segment_test.exs b/test/lib/code_corps/analytics/segment_test.exs new file mode 100644 index 000000000..133e0b915 --- /dev/null +++ b/test/lib/code_corps/analytics/segment_test.exs @@ -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 diff --git a/web/controllers/comment_controller.ex b/web/controllers/comment_controller.ex index 0ecfe85b9..931873e3a 100644 --- a/web/controllers/comment_controller.ex +++ b/web/controllers/comment_controller.ex @@ -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 diff --git a/web/controllers/user_category_controller.ex b/web/controllers/user_category_controller.ex index 80fe347fb..8267c7eaf 100644 --- a/web/controllers/user_category_controller.ex +++ b/web/controllers/user_category_controller.ex @@ -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 diff --git a/web/controllers/user_controller.ex b/web/controllers/user_controller.ex index fa65947c3..d3ce68092 100644 --- a/web/controllers/user_controller.ex +++ b/web/controllers/user_controller.ex @@ -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 diff --git a/web/controllers/user_role_controller.ex b/web/controllers/user_role_controller.ex index 901988b6e..d9ac02ecc 100644 --- a/web/controllers/user_role_controller.ex +++ b/web/controllers/user_role_controller.ex @@ -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 diff --git a/web/controllers/user_skill_controller.ex b/web/controllers/user_skill_controller.ex index 5c51625c6..662aae612 100644 --- a/web/controllers/user_skill_controller.ex +++ b/web/controllers/user_skill_controller.ex @@ -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