An Easy Guide to Tracking User Actions in WordPress
In WordPress, often times, we could need to follow the action of our users, be they subscribers, customers or contributors. Let’s say, for instance, we want to limit the number of times a user can export data from our site. Well, you’re in luck – WordPress allows us to do just that using a few innate features and some simple code. This method involves tracking the user’s actions using WordPress’s user meta data.
- Monitor Clicks on Export Button with JavaScript
The first step is to use custom JavaScript that will be added to your WordPress theme JS file. This helps in detecting clicks made on the export button.
In the JavaScript code below, you are using jQuery (a JavaScript library). You’re setting up a function that, every time the export button is clicked, increments a counter stored in the user’s meta data. This effectively tracks the user’s export actions.
Here’s the code you’ll need:
jQuery(document).ready(function($) {
var ajaxurl = ‘https://yoursite.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.');
if (typeof response === 'string' && response.trim() === 'You have reached your max. export limit.') {
$('.DTTT_button_export').hide();
}
}
});
});
});
Substitute ‘https://yoursite.com/wp-admin/admin-ajax.php’ with the actual AJAX URL of your site.
- Create a Custom Function in functions.php
The second step is to add a custom function to your theme’s functions.php file. This function will handle the AJAX request made by the JavaScript code and update the user meta.
Below is the function that should be added:
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’ );
This code checks if the user is logged-in, gets the user’s ID, retrieves the current export count, increments it by 1, and then updates the user’s metadata.
- Setting a Limit for Export Actions
The final step involves adding a checker to see if a user has exceeded their export limit before allowing the export action to happen. Yet again, we’ll be editing the theme’s functions.php file.
Place this function into your 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);
In this function, we’re setting a limit for the user for how many times they are allowed to perform the export action. If they exceed this limit, the action will be blocked with an error message.
And there you have it – a step-by-step guide on how to track user export actions in WordPress. By following these steps, you can easily restrict how many times a user exports data from your website based on your preferences.