2626import  java .util .concurrent .atomic .AtomicLong ;
2727
2828import  org .apache .camel .Exchange ;
29+ import  org .apache .camel .Message ;
2930import  org .eclipse .microprofile .config .inject .ConfigProperty ;
3031import  org .eclipse .microprofile .rest .client .inject .RestClient ;
3132import  org .slf4j .Logger ;
@@ -51,7 +52,11 @@ public class AnalyticsService {
5152
5253  private  static  final  Logger  LOGGER  = LoggerFactory .getLogger (AnalyticsService .class );
5354
54-   private  static  final  String  RHDA_TOKEN  = "rhda-token" ;
55+   private  static  final  String  RHDA_TOKEN_HEADER  = "rhda-token" ;
56+   private  static  final  String  RHDA_SOURCE_HEADER  = "rhda-source" ;
57+   private  static  final  String  RHDA_OPERATION_TYPE_HEADER  = "rhda-operation-type" ;
58+   private  static  final  String  USER_AGENT_HEADER  = "User-Agent" ;
59+ 
5560  private  static  final  String  ANONYMOUS_ID  = "telemetry-anonymous-id" ;
5661  private  static  final  String  ANALYSIS_EVENT  = "rhda.exhort.analysis" ;
5762  private  static  final  String  TOKEN_EVENT  = "rhda.exhort.token" ;
@@ -78,35 +83,36 @@ public void identify(Exchange exchange) {
7883      return ;
7984    }
8085
81-     String  userId  = exchange .getIn ().getHeader (RHDA_TOKEN , String .class );
86+     String  userId  = exchange .getIn ().getHeader (RHDA_TOKEN_HEADER , String .class );
87+     Map <String , String > traits  = new  HashMap <>();
88+     traits .put ("serverName" , projectName );
89+     traits .put ("serverVersion" , projectVersion );
90+     traits .put ("serverBuild" , projectBuild );
91+ 
92+     IdentifyEvent .Builder  builder  =
93+         new  IdentifyEvent .Builder ()
94+             .context (
95+                 new  Context (new  Library (projectId , projectVersion ), getSource (exchange .getIn ())))
96+             .traits (traits );
97+ 
8298    if  (userId  == null ) {
8399      String  anonymousId  = UUID .randomUUID ().toString ();
84-       Map <String , String > traits  = new  HashMap <>();
85-       traits .put ("serverName" , projectName );
86-       traits .put ("serverVersion" , projectVersion );
87-       traits .put ("serverBuild" , projectBuild );
88-       IdentifyEvent  event  =
89-           new  IdentifyEvent .Builder ()
90-               .context (new  Context (new  Library (projectId , projectVersion )))
91-               .anonymousId (anonymousId )
92-               .traits (traits )
93-               .build ();
94-       try  {
95-         Response  response  = segmentService .identify (event );
96-         if  (response .getStatus () >= 400 ) {
97-           LOGGER .warn (
98-               String .format (
99-                   "Unable to send event to segment: %d - %s" ,
100-                   response .getStatus (), response .getStatusInfo ()));
101-         }
102-       } catch  (Exception  e ) {
103-         LOGGER .warn ("Unable to send event to segment" , e );
104-       }
100+       builder .anonymousId (anonymousId );
105101      exchange .setProperty (ANONYMOUS_ID , anonymousId );
106102    } else  {
107-       // no need to IDENTIFY as we expect the caller to have done that already 
108-       exchange .setProperty (RHDA_TOKEN , userId );
109-       exchange .getIn ().removeHeader (RHDA_TOKEN );
103+       builder .userId (userId );
104+       exchange .setProperty (RHDA_TOKEN_HEADER , userId );
105+     }
106+     try  {
107+       Response  response  = segmentService .identify (builder .build ());
108+       if  (response .getStatus () >= 400 ) {
109+         LOGGER .warn (
110+             String .format (
111+                 "Unable to send event to segment: %d - %s" ,
112+                 response .getStatus (), response .getStatusInfo ()));
113+       }
114+     } catch  (Exception  e ) {
115+       LOGGER .warn ("Unable to send event to segment" , e );
110116    }
111117  }
112118
@@ -132,6 +138,10 @@ public void trackAnalysis(Exchange exchange) {
132138      snykReport .put ("remediations" , countRemediations (report ));
133139      providers .put (Constants .SNYK_PROVIDER , snykReport );
134140      properties .put ("providers" , providers );
141+       String  operationType  = exchange .getIn ().getHeader (RHDA_OPERATION_TYPE_HEADER , String .class );
142+       if  (operationType  != null ) {
143+         properties .put ("operationType" , operationType );
144+       }
135145    }
136146    try  {
137147      Response  response  = segmentService .track (builder .properties (properties ).build ());
@@ -168,16 +178,31 @@ public void trackToken(Exchange exchange) {
168178    }
169179  }
170180
181+   private  String  getSource (Message  message ) {
182+     String  customSource  = message .getHeader (RHDA_SOURCE_HEADER , String .class );
183+     if  (customSource  != null ) {
184+       return  customSource ;
185+     }
186+     String  userAgent  = message .getHeader (USER_AGENT_HEADER , String .class );
187+     if  (userAgent  != null ) {
188+       return  userAgent ;
189+     }
190+     return  null ;
191+   }
192+ 
171193  private  TrackEvent .Builder  prepareTrackEvent (Exchange  exchange , String  eventName ) {
172-     TrackEvent .Builder  builder  = new  TrackEvent .Builder (eventName );
173-     String  userId  = exchange .getProperty (RHDA_TOKEN , String .class );
194+     TrackEvent .Builder  builder  =
195+         new  TrackEvent .Builder (eventName )
196+             .context (
197+                 new  Context (new  Library (projectId , projectVersion ), getSource (exchange .getIn ())));
198+     String  userId  = exchange .getProperty (RHDA_TOKEN_HEADER , String .class );
174199    if  (userId  != null ) {
175200      builder .userId (userId );
176201    } else  {
177202      String  anonymousId  = exchange .getProperty (ANONYMOUS_ID , String .class );
178203      builder .anonymousId (anonymousId );
179204    }
180-     return  builder . context ( new   Context ( new   Library ( projectId ,  projectVersion ))) ;
205+     return  builder ;
181206  }
182207
183208  private  long  countRemediations (AnalysisReport  report ) {
0 commit comments