如何在WordPress站点追踪用户进入/退出点及表单提交行为
Hey there! Let's break down how to track visitor activity on your WordPress site—including entry/exit points and form submissions. I'll cover a few approaches depending on your technical comfort level:
1. Built-in Tools & Basic Code (No Plugins)
If you want to avoid plugins, you can leverage server logs and simple WordPress hooks:
- Entry Points: Check your web server's access logs (Apache's
access.logor Nginx'saccess.log) to view theRefererheader, which tells you where visitors originated from. For a WordPress-specific log, add a small snippet to store the entry URL inlocalStorage(via JavaScript) or log it directly to a custom file. - Exit Points: Server logs show the last URL a visitor accessed before leaving. Alternatively, use a
beforeunloadJavaScript event to send the exit URL to a custom log via AJAX. - Form Submissions: For default comment forms, use the
comment_postaction to log submissions. For custom forms, hook intoadmin_post/admin_post_noprivto capture form data when it's submitted.
2. Plugin-Based Solutions (Easiest for Most Users)
Plugins are the quickest way to get up and running with minimal code:
- Google Analytics for WordPress by MonsterInsights: Seamlessly integrates Google Analytics. Enable form tracking to monitor submissions from tools like Contact Form 7 or Gravity Forms, and view entry/exit pages directly in the analytics dashboard.
- WP Activity Log: Tracks nearly all site activity, including visitor entry/exit points and form submissions. Use the filtering options to narrow down to the exact data you care about.
- Formidable Forms: If you use this form builder, it has built-in submission tracking—you can see the entry page for each submission and set up alerts for new entries.
3. Custom Code Implementation (For Developers)
If you need full control over your tracking data, here's how to build your own system:
Tracking Entry/Exit Points
First, enqueue the tracking script in your theme's functions.php:
function enqueue_visitor_tracking_script() { wp_enqueue_script( 'visitor-tracking', get_template_directory_uri() . '/js/visitor-tracking.js', array('jquery'), '1.0', true ); wp_localize_script( 'visitor-tracking', 'trackingData', array( 'currentUrl' => get_permalink(), 'ajaxUrl' => admin_url('admin-ajax.php') ) ); } add_action('wp_enqueue_scripts', 'enqueue_visitor_tracking_script');
Create visitor-tracking.js in your theme's js folder:
// Store entry point on the visitor's first page load if (!localStorage.getItem('entryPoint')) { localStorage.setItem('entryPoint', trackingData.currentUrl); } // Track exit point when the user navigates away from the page window.addEventListener('beforeunload', function() { jQuery.post(trackingData.ajaxUrl, { action: 'log_exit_point', entryPoint: localStorage.getItem('entryPoint'), exitPoint: trackingData.currentUrl }); localStorage.removeItem('entryPoint'); });
Add the AJAX handler to functions.php to log the data:
function log_exit_point() { $entry_point = sanitize_text_field($_POST['entryPoint']); $exit_point = sanitize_text_field($_POST['exitPoint']); $log_file = WP_CONTENT_DIR . '/visitor-activity.log'; $log_entry = date('Y-m-d H:i:s') . " | Entry: $entry_point | Exit: $exit_point\n"; file_put_contents($log_file, $log_entry, FILE_APPEND); wp_die(); } add_action('wp_ajax_log_exit_point', 'log_exit_point'); add_action('wp_ajax_nopriv_log_exit_point', 'log_exit_point');
Tracking Form Submissions (Example for Contact Form 7)
Add this to functions.php to log submissions from Contact Form 7:
function track_cf7_submissions($contact_form) { $submission = WPCF7_Submission::get_instance(); if ($submission) { $posted_data = $submission->get_posted_data(); $entry_point = isset($_SERVER['HTTP_REFERER']) ? sanitize_text_field($_SERVER['HTTP_REFERER']) : 'Direct Visit'; $log_file = WP_CONTENT_DIR . '/form-submissions.log'; $log_entry = date('Y-m-d H:i:s') . " | Entry Point: $entry_point | Submission Data: " . print_r($posted_data, true) . "\n"; file_put_contents($log_file, $log_entry, FILE_APPEND); } } add_action('wpcf7_mail_sent', 'track_cf7_submissions');
Choose the approach that fits your needs—plugins are perfect for quick setup, while custom code gives you full control over what data you collect.
内容的提问来源于stack exchange,提问作者Prashant Saxena

