@@ -53,7 +53,7 @@ const Flagsmith = class {
5353 getFlags = ( resolve ?:( v ?:any ) => any , reject ?:( v ?:any ) => any ) => {
5454 const { onChange, onError, identity, api } = this ;
5555 let resolved = false ;
56- const handleResponse = ( { flags : features , traits } , segments ) => {
56+ const handleResponse = ( { flags : features , traits } ) => {
5757 if ( identity ) {
5858 this . withTraits = false ;
5959 }
@@ -75,18 +75,38 @@ const Flagsmith = class {
7575 this . oldFlags = {
7676 ...this . flags
7777 } ;
78- if ( segments ) {
79- let userSegments = { } ;
80- segments . map ( ( s ) => {
81- userSegments [ s . name ] = s ;
82- } ) ;
83- this . segments = userSegments ;
84- }
8578 const flagsEqual = deepEqual ( this . flags , flags ) ;
8679 const traitsEqual = deepEqual ( this . traits , userTraits ) ;
8780 this . flags = flags ;
8881 this . traits = userTraits ;
8982 this . updateStorage ( ) ;
83+ if ( this . dtrum ) {
84+ let traits : {
85+ "javaLongOrObject" : Record < string , number > ,
86+ "date" : Record < string , Date > ,
87+ "shortString" : Record < string , string > ,
88+ "javaDouble" : Record < string , number > ,
89+ } = {
90+ javaDouble : { } ,
91+ date : { } ,
92+ shortString : { } ,
93+ javaLongOrObject : { } ,
94+ }
95+ Object . keys ( this . flags ) . map ( ( key ) => {
96+ setDynatraceValue ( traits , "flagsmith_value_" + key , this . getValue ( key ) )
97+ setDynatraceValue ( traits , "flagsmith_enabled_" + key , this . hasFeature ( key ) )
98+ } )
99+ Object . keys ( this . traits ) . map ( ( key ) => {
100+ setDynatraceValue ( traits , "flagsmith_trait_" + key , this . getTrait ( key ) )
101+ } )
102+ this . log ( "Sending javaLongOrObject traits to dynatrace" , traits . javaLongOrObject )
103+ this . log ( "Sending date traits to dynatrace" , traits . date )
104+ this . log ( "Sending shortString traits to dynatrace" , traits . shortString )
105+ this . log ( "Sending javaDouble to dynatrace" , traits . javaDouble )
106+ this . dtrum . sendSessionProperties (
107+ traits . javaLongOrObject , traits . date , traits . shortString , traits . javaDouble
108+ )
109+ }
90110 if ( this . trigger ) {
91111 this . trigger ( )
92112 }
@@ -114,7 +134,7 @@ const Flagsmith = class {
114134 . then ( ( res ) => {
115135 // @ts -ignore
116136 this . withTraits = false
117- handleResponse ( res [ 0 ] , res [ 1 ] )
137+ handleResponse ( res [ 0 ] )
118138 if ( resolve && ! resolved ) {
119139 resolved = true ;
120140 resolve ( ) ;
@@ -128,7 +148,7 @@ const Flagsmith = class {
128148 ] )
129149 . then ( ( res ) => {
130150 // @ts -ignore
131- handleResponse ( { flags : res [ 0 ] } , null )
151+ handleResponse ( { flags : res [ 0 ] } )
132152 if ( resolve && ! resolved ) {
133153 resolved = true ;
134154 resolve ( ) ;
@@ -176,10 +196,10 @@ const Flagsmith = class {
176196 onError = null
177197 trigger = null
178198 identity = null
179- segments = null
180199 ticks = null
181200 timer = null
182201 traits = null
202+ dtrum = null
183203 withTraits = null
184204
185205 init ( {
@@ -192,6 +212,7 @@ const Flagsmith = class {
192212 defaultFlags,
193213 preventFetch,
194214 enableLogs,
215+ dtrum,
195216 enableAnalytics,
196217 AsyncStorage : _AsyncStorage ,
197218 identity,
@@ -248,6 +269,10 @@ const Flagsmith = class {
248269 throw ( 'Please specify a environment id' ) ;
249270 }
250271
272+ if ( dtrum ) {
273+ this . dtrum = dtrum ;
274+ }
275+
251276 if ( angularHttpClient ) {
252277 _fetch = ( url : string , params : { headers : Record < string , string > , method : "GET" | "POST" | "PUT" , body : string } ) => {
253278 const { headers, method, body} = params
@@ -424,7 +449,6 @@ const Flagsmith = class {
424449 environmentID : this . environmentID ,
425450 flags : this . flags ,
426451 identity : this . identity ,
427- segments : this . segments ,
428452 traits : this . traits ,
429453 evaluationEvent : this . evaluationEvent ,
430454 }
@@ -437,7 +461,6 @@ const Flagsmith = class {
437461 this . environmentID = state . environmentID || this . environmentID ;
438462 this . flags = state . flags || this . flags ;
439463 this . identity = state . identity || this . identity ;
440- this . segments = state . segments || this . segments ;
441464 this . traits = state . traits || this . traits ;
442465 this . evaluationEvent = state . evaluationEvent || this . evaluationEvent ;
443466 }
@@ -467,7 +490,6 @@ const Flagsmith = class {
467490
468491 logout ( ) {
469492 this . identity = null ;
470- this . segments = null ;
471493 this . traits = null ;
472494 if ( this . initialised ) {
473495 return this . getFlags ( ) ;
@@ -614,3 +636,16 @@ type Config= {fetch?:any, AsyncStorage?:any};
614636export default function ( { fetch, AsyncStorage } :Config ) :IFlagsmith {
615637 return new Flagsmith ( { fetch, AsyncStorage } ) as IFlagsmith ;
616638} ;
639+
640+ // transforms any trait to match sendSessionProperties
641+ // https://www.dynatrace.com/support/doc/javascriptapi/interfaces/dtrum_types.DtrumApi.html#addActionProperties
642+ const setDynatraceValue = function ( obj , trait , value ) {
643+ let key = 'shortString'
644+ let convertToString = true
645+ if ( typeof value === 'number' ) {
646+ key = 'javaDouble'
647+ convertToString = false
648+ }
649+ obj [ key ] = obj [ key ] || { }
650+ obj [ key ] [ trait ] = convertToString ? value + "" :value
651+ }
0 commit comments