@@ -2,12 +2,10 @@ import * as amplitude from '@amplitude/analytics-browser'
22import data from 'common/data/base/_data'
33import isFreeEmailDomain from 'common/utils/isFreeEmailDomain'
44
5- const enableDynatrace = ! ! window . enableDynatrace && typeof dtrum !== 'undefined'
65import { loadReoScript } from 'reodotdev'
7-
8- import freeEmailDomains from 'free-email-domains'
96import { groupBy } from 'lodash'
107import getUserDisplayName from 'common/utils/getUserDisplayName'
8+
119global . API = {
1210 ajaxHandler ( store , res ) {
1311 switch ( res . status ) {
@@ -62,42 +60,7 @@ global.API = {
6260 } ,
6361 alias ( id , user = { } ) {
6462 if ( Project . excludeAnalytics ?. includes ( id ) ) return
65- if ( Project . mixpanel ) {
66- mixpanel . alias ( id )
67- }
68-
69- if ( enableDynatrace && user ?. id ) {
70- dtrum . identifyUser ( `${ user . id } ` )
71- }
7263 Utils . setupCrisp ( )
73- if ( Project . heap ) {
74- heap . identify ( id )
75- const user = AccountStore . model
76- const orgs =
77- ( user &&
78- user . organisations &&
79- _ . map (
80- user . organisations ,
81- ( o ) => `${ o . name } #${ o . id } (${ o . role } )[${ o . num_seats } ]` ,
82- ) . join ( ',' ) ) ||
83- ''
84- const plans = AccountStore . getPlans ( )
85- heap . addUserProperties ( {
86- // use human-readable names
87- '$first_name' : user . first_name ,
88-
89- '$last_name' : user . last_name ,
90- 'USER_ID' : id ,
91- email : id ,
92- 'isCompanyEmail' :
93- ! user . email . includes ( '@gmail' ) &&
94- ! user . email . includes ( '@yahoo' ) &&
95- ! user . email . includes ( '@hotmail' ) &&
96- ! user . email . includes ( '@icloud' ) ,
97- orgs,
98- 'plan' : plans && plans . join ( ',' ) ,
99- } )
100- }
10164 if ( Project . reo ) {
10265 const reoPromise = loadReoScript ( { clientID : Project . reo } )
10366 reoPromise . then ( ( Reo ) => {
@@ -128,8 +91,9 @@ global.API = {
12891 }
12992 if ( Project . amplitude ) {
13093 amplitude . setUserId ( id )
131- const identify = new amplitude . Identify ( ) . set ( 'email' , id )
132- amplitude . identify ( identify )
94+ API . trackTraits ( {
95+ email : id ,
96+ } )
13397 if ( typeof window . engagement !== 'undefined' ) {
13498 window . engagement . boot ( {
13599 integrations : [
@@ -218,40 +182,6 @@ global.API = {
218182 identify ( id , user = { } ) {
219183 if ( Project . excludeAnalytics ?. includes ( id ) ) return
220184 try {
221- const orgs =
222- ( user &&
223- user . organisations &&
224- _ . map (
225- user . organisations ,
226- ( o ) => `${ o . name } #${ o . id } (${ o . role } )[${ o . num_seats } ]` ,
227- ) . join ( ',' ) ) ||
228- ''
229- if ( Project . mixpanel ) {
230- mixpanel . identify ( id )
231- const plans = AccountStore . getPlans ( )
232-
233- mixpanel . people . set ( {
234- '$email' : id ,
235- // use human-readable names
236- '$first_name' : user . first_name ,
237-
238- '$last_name' : user . last_name ,
239- // only reserved properties need the $
240- 'USER_ID' : id ,
241- 'isCompanyEmail' :
242- ! user . email . includes ( '@gmail' ) &&
243- ! user . email . includes ( '@yahoo' ) &&
244- ! user . email . includes ( '@hotmail' ) &&
245- ! user . email . includes ( '@icloud' ) ,
246- orgs,
247- 'plan' : plans && plans . join ( ',' ) ,
248- } )
249- }
250-
251- if ( enableDynatrace && user ?. id ) {
252- dtrum . identifyUser ( `${ user . id } ` )
253- }
254-
255185 const planNames = {
256186 enterprise : 'Enterprise' ,
257187 free : 'Free' ,
@@ -297,41 +227,16 @@ global.API = {
297227 )
298228 const selectedRole = selectedOrg ?. role //ADMIN | USER
299229 const selectedOrgName = selectedOrg ?. name
300- if ( Project . heap ) {
301- const plans = AccountStore . getPlans ( )
302- heap . identify ( id )
303- heap . addUserProperties ( {
304- // use human-readable names
305- '$first_name' : user . first_name ,
306- '$last_name' : user . last_name ,
307- 'USER_ID' : id ,
308- email : id ,
309- 'isCompanyEmail' :
310- ! user . email . includes ( '@gmail' ) &&
311- ! user . email . includes ( '@yahoo' ) &&
312- ! user . email . includes ( '@hotmail' ) &&
313- ! user . email . includes ( '@icloud' ) ,
314- orgs,
315- 'plan' : plans && plans . join ( ',' ) ,
316- } )
317- }
318230
319- if ( Project . amplitude ) {
320- amplitude . setUserId ( id )
321- const identify = new amplitude . Identify ( )
322- . set ( 'email' , id )
323- . set ( 'name' , { 'first' : user . first_name , 'last' : user . last_name } )
324- . set ( 'organisation' , selectedOrgName )
325- . set ( 'role' , selectedRole )
326- . set ( 'plan' , selectedPlanName )
327- . set (
328- 'tasks' ,
329- ( user . onboarding ?. tasks || [ ] ) ?. map ( ( v ) => v . name ) ,
330- )
331- . set ( 'integrations' , user . onboarding ?. tools ?. integrations || [ ] )
332-
333- amplitude . identify ( identify )
334- }
231+ API . trackTraits ( {
232+ email : id ,
233+ integrations : user . onboarding ?. tools ?. integrations || [ ] ,
234+ name : { 'first' : user . first_name , 'last' : user . last_name } ,
235+ organisation : selectedOrgName ,
236+ plan : selectedPlanName ,
237+ role : selectedRole ,
238+ tasks : ( user . onboarding ?. tasks || [ ] ) ?. map ( ( v ) => v . name ) ,
239+ } )
335240 API . flagsmithIdentify ( )
336241 } catch ( e ) {
337242 console . error ( 'Error identifying' , e )
@@ -352,20 +257,7 @@ global.API = {
352257 tag,
353258 } )
354259 } ,
355- register ( email , firstName , lastName ) {
356- if ( Project . excludeAnalytics ?. includes ( email ) ) return
357- if ( Project . mixpanel ) {
358- mixpanel . register ( {
359- 'Email' : email ,
360- 'First Name' : firstName ,
361- 'Last Name' : lastName ,
362- } )
363- }
364- } ,
365260 reset ( ) {
366- if ( Project . mixpanel ) {
367- mixpanel . reset ( )
368- }
369261 return flagsmith . logout ( )
370262 } ,
371263 setCookie ( key , v ) {
@@ -432,11 +324,6 @@ global.API = {
432324 } )
433325 }
434326
435- if ( Project . heap ) {
436- heap . track ( data . event , {
437- category : data . category ,
438- } )
439- }
440327 if ( Project . amplitude ) {
441328 const eventData = {
442329 category : data . category ,
@@ -445,18 +332,6 @@ global.API = {
445332
446333 amplitude . track ( data . event , eventData )
447334 }
448- if ( Project . mixpanel ) {
449- if ( ! data ) {
450- console . error ( 'Passed null event data' )
451- }
452- console . info ( 'track' , data )
453- if ( ! data || ! data . category || ! data . event ) {
454- console . error ( 'Invalid event provided' , data )
455- }
456- mixpanel . track ( data . event , {
457- category : data . category ,
458- } )
459- }
460335 } ,
461336 trackPage ( title ) {
462337 if ( Project . ga ) {
@@ -467,19 +342,14 @@ global.API = {
467342 title,
468343 } )
469344 }
470- if ( Project . heap ) {
471- heap . track ( `Page View - ${ title } ` , {
472- location : document . location . href ,
473- page : document . location . pathname ,
474- title,
475- } )
476- }
477- if ( Project . mixpanel ) {
478- mixpanel . track ( `Page View - ${ title } ` , {
479- location : document . location . href ,
480- page : document . location . pathname ,
481- title,
482- } )
345+ } ,
346+ trackTraits ( traits ) {
347+ if ( Project . amplitude && traits ) {
348+ const identifyObj = new amplitude . Identify ( )
349+ for ( const [ key , value ] of Object . entries ( traits ) ) {
350+ identifyObj . set ( key , value )
351+ }
352+ amplitude . identify ( identifyObj )
483353 }
484354 } ,
485355}
0 commit comments