wordpresspluginshtml-tabledatatableexport-to-csv

How to Track Export Actions Per User with WPDataTables Plugin in WordPress?


I'm currently evaluating the WPDataTables plugin for a WordPress project. Specifically, I need to track how many times each user exports data from a table. This functionality is critical for our project as we need to limit how much data our subscribers can export to prevent database scraping.

Does the WPDataTables plugin offer a built-in feature to count export actions per user? If not, is there a recommended approach or workaround to implement this functionality?

Thank you for your help!:)

——

I reviewed the WPDataTables documentation and explored the plugin settings but couldn't find an option to count export actions per user. I was expecting to find a built-in feature or a setting to track user-specific export activity.


Solution

  • We can use workaround approach to achieve the required functionality. In this approach we will use WordPress's user meta to track the export actions per user.

    Whenever the export button is clicked, we will increment a counter stored in the user's meta data in order to track user.

    Step 1: Here we need to add custom JavaScript to theme JS file to detect clicks on the export button and send an AJAX request to the server. In this '#table_1_wrapper' is the table id.

    jQuery(document).ready(function($) {
        var ajaxurl = 'https://gomarketish.com/wp-admin/admin-ajax.php'; // Replace with your actual AJAX URL
    
        $('#table_1_wrapper').on('click', '.buttons-csv, .buttons-excel, .buttons-pdf', function() {
            $.ajax({
                url: ajaxurl,
                type: 'POST',
                data: {
                    action: 'track_export_action'
                },
                success: function(response) {
                    console.log('Export action has been tracked.');
    
                    // Check if response is a string and equals to 'You have reached your max. export limit.'
                    if (typeof response === 'string' && response.trim() === 'You have reached your max. export limit.') {
                        // Hide buttons with class 'DTTT_button_export'
                        $('.DTTT_button_export').hide();
                    }
                }
            });
        });
    });
    

    Step 2: Here we are adding a custom function in your theme's functions.php file to handle the AJAX request and update the user meta.

    function track_user_export_action() {
        if (is_user_logged_in()) {
            $user_id = get_current_user_id();
            $export_count = get_user_meta( $user_id, 'export_count', true );
    
            if (!$export_count) {
                $export_count = 0;
            }
    
            $export_count++;
            update_user_meta( $user_id, 'export_count', $export_count );
    
            wp_send_json_success( 'Export action tracked' );
        } else {
            wp_send_json_error( 'User not logged in' );
        }
    
        wp_die();
    }
    
    add_action( 'wp_ajax_track_export_action', 'track_user_export_action' );
    

    Step 3: Here we need to add a check to see if the user has exceeded their export limit before allowing the export action. This also needs to add to your theme's functions.php file.

    function set_limit_export_actions() {
        if (is_user_logged_in()) {
            $user_id      = get_current_user_id();
            $export_count = get_user_meta($user_id, 'export_count', true);
            $max_exports  = 10; // Set your limit here
    
            if ($export_count >= $max_exports) {
                wp_die( 'You have reached your max. export limit.' );
            }
        }
    }
    
    add_action('wp_ajax_track_export_action', 'set_limit_export_actions', 1);
    

    Please let me know if this helps.