Skip to content

Commit

Permalink
Save tracked events more often (#2862)
Browse files Browse the repository at this point in the history
* Setting to save every event as it's tracked.
  • Loading branch information
brianhogg authored Feb 5, 2025
1 parent c298c0c commit db13442
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .changelogs/save-trackd-events-more-often.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
significance: patch
type: added
entry: Setting to increase the frequency user tracked events are saved.
2 changes: 1 addition & 1 deletion assets/js/app/llms-tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ LLMS.Tracking = function( settings ) {
store.set( 'events', all );

// If couldn't store the latest event because of size limits.
if ( all.length > store.get( 'events', [] ).length ) {
if ( settings.saving_frequency === 'always' || all.length > store.get( 'events', [] ).length ) {

// Copy the cookie in a temporary variable.
var _temp = store.getAll();
Expand Down
16 changes: 12 additions & 4 deletions includes/admin/settings/class.llms.settings.general.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public function __construct() {
add_filter( 'lifterlms_settings_tabs_array', array( $this, 'add_settings_page' ), 20 );
add_action( 'lifterlms_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'lifterlms_settings_save_' . $this->id, array( $this, 'save' ) );

}

/**
Expand Down Expand Up @@ -115,13 +114,24 @@ function ( $role ) {
'type' => 'checkbox',
);

$settings[] = array(
'title' => __( 'Frequency of Saving Tracked Events', 'lifterlms' ),
'default' => 'minimum',
'desc' => __( 'Specifies how often tracked events are sent to the server. "Minimum" sends only when local storage is almost full (fewer network calls). "Always" sends each event immediately (higher accuracy, more server load).', 'lifterlms' ),
'id' => 'lifterlms_tracked_event_saving_frequency',
'type' => 'select',
'options' => array(
'minimum' => __( 'Minimum', 'lifterlms' ),
'always' => __( 'Always', 'lifterlms' ),
),
);

$settings[] = array(
'id' => 'general_settings',
'type' => 'sectionend',
);

return apply_filters( 'lifterlms_general_settings', $settings );

}

/**
Expand All @@ -133,9 +143,7 @@ public function save() {

$settings = $this->get_settings();
LLMS_Admin_Settings::save_fields( $settings );

}

}

return new LLMS_Settings_General();
16 changes: 3 additions & 13 deletions includes/class-llms-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private function __construct() {

add_action( 'init', array( $this, 'register_events' ), 9 );
add_action( 'init', array( $this, 'store_cookie' ) );

}

/**
Expand Down Expand Up @@ -74,11 +73,11 @@ public function get_client_settings() {
return apply_filters(
'llms_events_get_client_settings',
array(
'nonce' => wp_create_nonce( 'llms-tracking' ),
'events' => $events,
'nonce' => wp_create_nonce( 'llms-tracking' ),
'events' => $events,
'saving_frequency' => get_option( 'lifterlms_tracked_event_saving_frequency', 'minimum' ),
)
);

}

/**
Expand All @@ -104,7 +103,6 @@ public function get_registered_events() {
protected function is_event_valid( $event ) {

return array_key_exists( $event, $this->get_registered_events() );

}

/**
Expand Down Expand Up @@ -149,7 +147,6 @@ public function prepare_event( $raw_event = array() ) {
}

return $prepared;

}

/**
Expand Down Expand Up @@ -221,7 +218,6 @@ public function record( $args = array() ) {
}

return $llms_event;

}

/**
Expand Down Expand Up @@ -260,7 +256,6 @@ public function record_many( $events = array() ) {
$wpdb->query( 'COMMIT' );

return $recorded;

}

/**
Expand Down Expand Up @@ -297,7 +292,6 @@ public function register_events() {
* @param array $events Array of events. Array key is the event name and array value is used to determine if the key is a client-side event.
*/
$this->registered_events = apply_filters( 'llms_get_registered_events', $events );

}

/**
Expand Down Expand Up @@ -333,7 +327,6 @@ protected function sanitize_raw_event( $raw ) {
}

return $clean;

}

/**
Expand Down Expand Up @@ -391,7 +384,6 @@ protected function should_track_client_events() {
* @param string[] $post_types Array of post types that should be tracked.
*/
return apply_filters( 'llms_tracking_should_track_client_events', $ret, $post_types );

}

/**
Expand Down Expand Up @@ -419,7 +411,6 @@ public function store_cookie() {

// Cookie reset.
llms_setcookie( 'llms-tracking', '', time() - 60, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, llms_is_site_https() && is_ssl() );

}

/**
Expand Down Expand Up @@ -453,5 +444,4 @@ public function store_tracking_events( $tracking ) {

return true;
}

}

0 comments on commit db13442

Please sign in to comment.