Skip to content

Commit f958891

Browse files
authored
Merge pull request #824 from newfold-labs/add/onboarding-time-tracking
track start and end time for onboarding
2 parents 0021014 + d325c75 commit f958891

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

includes/Services/EventService.php

Lines changed: 46 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,46 @@ 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+
// 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+
}
89135
}

includes/Services/StatusService.php

Lines changed: 13 additions & 1 deletion
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
*/
@@ -181,7 +193,7 @@ public static function track(): void {
181193
}
182194

183195
// Ignore if the request is not for the onboarding page.
184-
if ( isset( $_GET['page'] ) && WP_Admin::$slug === \sanitize_text_field( $_GET['page'] ) ) {
196+
if ( isset( $_GET['page'] ) && \sanitize_text_field( $_GET['page'] ) === WP_Admin::$slug ) {
185197
return;
186198
}
187199

0 commit comments

Comments
 (0)