Skip to content

Commit 4df45f7

Browse files
committed
track start and end time for onboarding
1 parent 0021014 commit 4df45f7

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

includes/Services/EventService.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace NewfoldLabs\WP\Module\Onboarding\Services;
44

55
use NewfoldLabs\WP\Module\Onboarding\Data\Events;
6+
use NewfoldLabs\WP\Module\Onboarding\Data\Options;
67

78
/**
89
* Class for handling analytics events.
@@ -24,6 +25,9 @@ public static function send( $event ) {
2425
);
2526
}
2627

28+
// Add timestamp and ttl to specific events
29+
$event = self::add_timestamp_and_ttl( $event );
30+
2731
$event_data_request = new \WP_REST_Request(
2832
\WP_REST_Server::CREATABLE,
2933
NFD_MODULE_DATA_EVENTS_API
@@ -86,4 +90,37 @@ public static function validate( $event ) {
8690

8791
return $event;
8892
}
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+
// Calculate ttl from stored start time
114+
$start_time = get_option( Options::get_option_name( 'start_time' ) );
115+
if ( $start_time ) {
116+
$ttl_seconds = $current_time - $start_time;
117+
if ( $ttl_seconds >= 0 ) {
118+
$event['data']['ttl'] = $ttl_seconds;
119+
}
120+
}
121+
break;
122+
}
123+
124+
return $event;
125+
}
89126
}

includes/Services/StatusService.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public static function handle_started(): bool {
2222
$status = get_option( Options::get_option_name( 'status' ) );
2323
if ( 'started' !== $status && 'completed' !== $status ) {
2424
update_option( Options::get_option_name( 'status' ), 'started' );
25+
26+
// Store start time when onboarding begins
27+
update_option( Options::get_option_name( 'start_time' ), time() );
28+
2529
do_action( 'newfold/onboarding/started' );
2630
return true;
2731
}
@@ -36,6 +40,10 @@ public static function handle_started(): bool {
3640
public static function handle_abandoned(): void {
3741
if ( 'started' === get_option( Options::get_option_name( 'status' ) ) ) {
3842
update_option( Options::get_option_name( 'status' ), 'abandoned' );
43+
44+
// Clean up time tracking when onboarding is abandoned
45+
delete_option( Options::get_option_name( 'start_time' ) );
46+
delete_option( Options::get_option_name( 'completed_time' ) );
3947
}
4048
}
4149

@@ -47,6 +55,10 @@ public static function handle_abandoned(): void {
4755
public static function handle_completed(): void {
4856
if ( 'started' === get_option( Options::get_option_name( 'status' ) ) ) {
4957
update_option( Options::get_option_name( 'status' ), 'completed' );
58+
59+
// Store completion time
60+
update_option( Options::get_option_name( 'completed_time' ), time() );
61+
5062
/**
5163
* We're disabling the restart onboarding feature for now.
5264
*/

0 commit comments

Comments
 (0)