@@ -15,13 +15,7 @@ defmodule CodeCorps.Analytics.Segment do
1515 ```
1616 """
1717
18- alias CodeCorps.Comment
19- alias CodeCorps.OrganizationMembership
20- alias CodeCorps.Task
21- alias CodeCorps.User
22- alias CodeCorps.UserCategory
23- alias CodeCorps.UserRole
24- alias CodeCorps.UserSkill
18+ alias CodeCorps . { Comment , OrganizationMembership , StripeInvoice , Task , User , UserCategory , UserRole , UserSkill }
2519 alias Ecto.Changeset
2620
2721 @ api Application . get_env ( :code_corps , :analytics )
@@ -37,6 +31,7 @@ defmodule CodeCorps.Analytics.Segment do
3731 end
3832 def get_event_name ( :created , % OrganizationMembership { } ) , do: "Requested Organization Membership"
3933 def get_event_name ( :edited , % OrganizationMembership { } ) , do: "Approved Organization Membership"
34+ def get_event_name ( :payment_succeeded , % StripeInvoice { } ) , do: "Processed Subscription Payment"
4035 def get_event_name ( :created , % UserCategory { } ) , do: "Added User Category"
4136 def get_event_name ( :created , % UserSkill { } ) , do: "Added User Skill"
4237 def get_event_name ( :created , % UserRole { } ) , do: "Added User Role"
@@ -61,13 +56,16 @@ defmodule CodeCorps.Analytics.Segment do
6156 def track ( { :ok , record } , action , % Plug.Conn { } = conn ) when action in @ actions_without_properties do
6257 action_name = get_event_name ( action , record )
6358 do_track ( conn , action_name )
64-
6559 { :ok , record }
6660 end
6761 def track ( { :ok , record } , action , % Plug.Conn { } = conn ) do
6862 action_name = get_event_name ( action , record )
6963 do_track ( conn , action_name , properties ( record ) )
70-
64+ { :ok , record }
65+ end
66+ def track ( { :ok , % { user_id: user_id } = record } , action , nil ) do
67+ action_name = get_event_name ( action , record )
68+ do_track ( user_id , action_name , properties ( record ) )
7169 { :ok , record }
7270 end
7371 def track ( { :error , % Changeset { } = changeset } , _action , _conn ) , do: { :error , changeset }
@@ -98,14 +96,13 @@ defmodule CodeCorps.Analytics.Segment do
9896 |> Enum . join ( " " )
9997 end
10098
101- defp do_track ( conn , event_name , properties ) do
99+ defp do_track ( conn_or_user , event_name , properties \\ % { } )
100+ defp do_track ( % Plug.Conn { } = conn , event_name , properties ) do
102101 @ api . track ( conn . assigns [ :current_user ] . id , event_name , properties )
103102 conn
104103 end
105-
106- defp do_track ( conn , event_name ) do
107- @ api . track ( conn . assigns [ :current_user ] . id , event_name , % { } )
108- conn
104+ defp do_track ( user_id , event_name , properties ) do
105+ @ api . track ( user_id , event_name , properties )
109106 end
110107
111108 defp properties ( comment = % Comment { } ) do
@@ -125,6 +122,17 @@ defmodule CodeCorps.Analytics.Segment do
125122 organization_id: organization_membership . organization . id
126123 }
127124 end
125+ defp properties ( invoice = % StripeInvoice { } ) do
126+ revenue = invoice . total / 100 # TODO: this only works for some currencies
127+ currency = String . capitalize ( invoice . currency ) # ISO 4127 format
128+
129+ % {
130+ currency: currency ,
131+ invoice_id: invoice . id ,
132+ revenue: revenue ,
133+ user_id: invoice . user_id
134+ }
135+ end
128136 defp properties ( task = % Task { } ) do
129137 % {
130138 task: task . title ,
@@ -154,10 +162,12 @@ defmodule CodeCorps.Analytics.Segment do
154162 skill_id: user_skill . skill . id
155163 }
156164 end
165+
157166 defp properties ( _struct ) do
158167 % { }
159168 end
160169
170+
161171 defp traits ( user ) do
162172 % {
163173 admin: user . admin ,
0 commit comments