How to Use the Slimstat Tracker in External PHP Scripts
The Slimstat plugin for WordPress offers effective tracking tools by default. However, you might sometimes need to track page views from non-WordPress sources, such as during redirects or within custom PHP scripts. Here’s a simplified guide on how to implement Slimstat tracking outside of WordPress:
1. Verify Slimstat Activation
Before you use the tracker, make sure that the Slimstat plugin is active. You can check this by confirming both the presence of the wp_slimstat
class and the availability of the slimtrack()
method:
if (class_exists('wp_slimstat') && method_exists('wp_slimstat', 'slimtrack')) {
// Slimstat is active and ready to track
}
2. Initiate Tracking
Once you’ve confirmed that Slimstat is active, initiate tracking by calling the slimtrack()
method. This will capture key data like the URL, user agent, and IP address:
wp_slimstat::slimtrack();
3. Customize Tracking Data (Optional)
If you want to enhance or modify the data being tracked, Slimstat allows you to use filters. For instance, you can add custom data to the tracking details using the slimstat_filter_pageview_stat
filter:
add_filter('slimstat_filter_pageview_stat', 'my_custom_tracking_data');
function my_custom_tracking_data($data) {
$data['custom_param'] = 'Additional tracking information';
return $data;
}
Using these steps, you can integrate Slimstat tracking into any external PHP script or scenario where WordPress isn’t directly involved, ensuring detailed analytics across your applications.