@@ -59,7 +59,7 @@ def get_event_batching_query_params(account_id: str) -> Dict[str, Any]:
5959
6060# Function to build generic properties for tracking events
6161def get_events_base_properties (
62- event_name : str , visitor_user_agent : str = "" , ip_address : str = ""
62+ event_name : str , visitor_user_agent : str = "" , ip_address : str = "" , is_usage_stats_event : bool = False , usage_stats_account_id : int = None
6363) -> Dict [str , Any ]:
6464
6565 # Get the instance of SettingsManager
@@ -69,10 +69,9 @@ def get_events_base_properties(
6969 sdk_key = settings .get_sdk_key ()
7070 account_id = settings .get_account_id ()
7171
72- return {
72+ properties = {
7373 "en" : event_name ,
7474 "a" : account_id ,
75- "env" : sdk_key ,
7675 "eTime" : get_current_unix_timestamp_in_millis (),
7776 "random" : get_random_number (),
7877 "p" : "FS" ,
@@ -83,6 +82,14 @@ def get_events_base_properties(
8382 + UrlEnum .EVENTS .value ,
8483 }
8584
85+ if not is_usage_stats_event :
86+ # set env key for standard sdk events
87+ properties ["env" ] = sdk_key
88+ else :
89+ # set account id for internal usage stats event
90+ properties ["a" ] = str (usage_stats_account_id )
91+ return properties
92+
8693
8794# Function to build payload for tracking events
8895def _get_event_base_payload (
@@ -91,10 +98,27 @@ def _get_event_base_payload(
9198 event_name : str ,
9299 visitor_user_agent : str = "" ,
93100 ip_address : str = "" ,
101+ is_usage_stats_event : bool = False ,
102+ usage_stats_account_id : int = None ,
94103) -> Dict [str , Any ]:
95104
96- uuid_value = get_uuid (user_id , SettingsManager .get_instance ().get_account_id ())
105+ account_id = SettingsManager .get_instance ().get_account_id ()
106+ if is_usage_stats_event :
107+ # set account id for internal usage stats event
108+ account_id = usage_stats_account_id
109+
110+ uuid_value = get_uuid (str (user_id ), str (account_id ))
97111 sdk_key = SettingsManager .get_instance ().get_sdk_key ()
112+
113+ props = {
114+ "vwo_sdkName" : Constants .SDK_NAME ,
115+ "vwo_sdkVersion" : Constants .SDK_VERSION ,
116+ }
117+
118+ if not is_usage_stats_event :
119+ # set env key for standard sdk events
120+ props ["vwo_envKey" ] = sdk_key
121+
98122 properties = {
99123 "d" : {
100124 "msgId" : f"{ uuid_value } -{ get_current_unix_timestamp_in_millis ()} " ,
@@ -103,18 +127,21 @@ def _get_event_base_payload(
103127 "visitor_ua" : visitor_user_agent ,
104128 "visitor_ip" : ip_address ,
105129 "event" : {
106- "props" : {
107- "vwo_sdkName" : Constants .SDK_NAME ,
108- "vwo_sdkVersion" : Constants .SDK_VERSION ,
109- "vwo_envKey" : sdk_key ,
110- },
130+ "props" : props ,
111131 "name" : event_name ,
112132 "time" : get_current_unix_timestamp_in_millis (),
113133 },
114- "visitor" : {"props" : {Constants .VWO_FS_ENVIRONMENT : sdk_key }},
115134 }
116135 }
117136
137+ if not is_usage_stats_event :
138+ # set visitor props for standard sdk events
139+ properties ["d" ]["visitor" ] = {
140+ "props" : {
141+ Constants .VWO_FS_ENVIRONMENT : sdk_key ,
142+ },
143+ }
144+
118145 return properties
119146
120147
@@ -136,10 +163,6 @@ def get_track_user_payload_data(
136163 properties ["d" ]["event" ]["props" ]["variation" ] = str (variation_id )
137164 properties ["d" ]["event" ]["props" ]["isFirst" ] = 1
138165
139- usage_stats_data = UsageStatsUtil ().get_usage_stats ()
140- if len (usage_stats_data ) > 0 :
141- properties ["d" ]["event" ]["props" ]["vwoMeta" ] = usage_stats_data
142-
143166 LogManager .get_instance ().debug (
144167 debug_messages .get ("IMPRESSION_FOR_TRACK_USER" ).format (
145168 accountId = settings .get_account_id (), userId = user_id , campaignId = campaign_id
@@ -352,7 +375,7 @@ def get_messaging_event_payload(
352375 properties ["d" ]["event" ]["props" ]["vwo_envKey" ] = settings .get_sdk_key ()
353376 properties ["d" ]["event" ]["props" ][
354377 "product"
355- ] = "fme" # Assuming 'product' is a required field
378+ ] = Constants . PRODUCT_NAME
356379
357380 # Set the message data
358381 data = {
@@ -392,7 +415,7 @@ def get_sdk_init_event_payload(
392415
393416 # Set the required fields as specified
394417 properties ["d" ]["event" ]["props" ][Constants .VWO_FS_ENVIRONMENT ] = settings .get_sdk_key ()
395- properties ["d" ]["event" ]["props" ]["product" ] = "fme"
418+ properties ["d" ]["event" ]["props" ]["product" ] = Constants . PRODUCT_NAME
396419
397420 data = {
398421 "isSDKInitialized" : True ,
@@ -404,12 +427,37 @@ def get_sdk_init_event_payload(
404427 return properties
405428
406429
430+ def get_sdk_usage_stats_event_payload (
431+ event_name : str , usage_stats_account_id : int
432+ ) -> Dict [str , Any ]:
433+ """
434+ Constructs the payload for sdk usage stats event.
435+
436+ Args:
437+ event_name: The name of the event.
438+ usage_stats_account_id: Account ID for usage stats event.
439+
440+ Returns:
441+ The constructed payload with required fields.
442+ """
443+ # Get user ID and properties
444+ settings = SettingsManager .get_instance ()
445+ user_id = f"{ settings .get_account_id ()} _{ settings .get_sdk_key ()} "
446+ properties = _get_event_base_payload (None , user_id , event_name , None , None , True , usage_stats_account_id )
447+
448+ # Set the required fields as specified
449+ properties ["d" ]["event" ]["props" ]["product" ] = Constants .PRODUCT_NAME
450+ properties ["d" ]["event" ]["props" ]["vwoMeta" ] = UsageStatsUtil ().get_usage_stats ()
451+
452+ return properties
453+
454+
407455def send_event (
408456 properties : Dict [str , Any ], payload : Dict [str , Any ], event_name : str
409457) -> Dict [str , Any ]:
410458 try :
411459 #if event_name is not VWO_LOG_EVENT, then set base url to constants.hostname
412- if event_name == EventEnum .VWO_LOG_EVENT .value :
460+ if event_name == EventEnum .VWO_LOG_EVENT .value or event_name == EventEnum . VWO_USAGE_STATS . value :
413461 base_url = Constants .HOST_NAME
414462 protocol = Constants .HTTPS_PROTOCOL
415463 port = 443
0 commit comments