3
3
namespace NewfoldLabs \WP \Module \Onboarding \Services ;
4
4
5
5
use NewfoldLabs \WP \Module \Onboarding \Data \Events ;
6
+ use NewfoldLabs \WP \Module \Onboarding \Data \Options ;
6
7
7
8
/**
8
9
* Class for handling analytics events.
@@ -24,6 +25,9 @@ public static function send( $event ) {
24
25
);
25
26
}
26
27
28
+ // Add timestamp and ttl to specific events
29
+ $ event = self ::add_timestamp_and_ttl ( $ event );
30
+
27
31
$ event_data_request = new \WP_REST_Request (
28
32
\WP_REST_Server::CREATABLE ,
29
33
NFD_MODULE_DATA_EVENTS_API
@@ -86,4 +90,46 @@ public static function validate( $event ) {
86
90
87
91
return $ event ;
88
92
}
93
+
94
+ /**
95
+ * Adds timestamp and ttl properties to specific events (onboarding_started and onboarding_complete).
96
+ *
97
+ * @param array $event The event to enhance.
98
+ * @return array The enhanced event.
99
+ */
100
+ private static function add_timestamp_and_ttl ( $ event ) {
101
+ $ current_time = time ();
102
+
103
+ switch ( $ event ['action ' ] ) {
104
+ case 'onboarding_started ' :
105
+ // Add timestamp to onboarding_started event
106
+ $ event ['data ' ]['timestamp ' ] = $ current_time ;
107
+ break ;
108
+
109
+ case 'onboarding_complete ' :
110
+ // Add timestamp and ttl to onboarding_complete event
111
+ $ event ['data ' ]['timestamp ' ] = $ current_time ;
112
+
113
+ // Use the same completion time that was stored in handle_completed()
114
+ $ completion_time = get_option ( Options::get_option_name ( 'completed_time ' ) );
115
+ $ start_time = get_option ( Options::get_option_name ( 'start_time ' ) );
116
+
117
+ if ( $ start_time ) {
118
+ if ( $ completion_time ) {
119
+ // Use stored completion time
120
+ $ ttl_seconds = $ completion_time - $ start_time ;
121
+ } else {
122
+ // Fallback to current time if completion_time not found
123
+ $ ttl_seconds = $ current_time - $ start_time ;
124
+ }
125
+
126
+ if ( $ ttl_seconds >= 0 ) {
127
+ $ event ['data ' ]['ttl ' ] = $ ttl_seconds ;
128
+ }
129
+ }
130
+ break ;
131
+ }
132
+
133
+ return $ event ;
134
+ }
89
135
}
0 commit comments