PHP表单提交后带谷歌跟踪代码的感谢页定时自动跳转问题
Solution for Tracking Code + Delayed Redirect on Thank You Page
Hey there! Let's fix this issue so your Google tracking code fires properly while still getting that delayed redirect to the login page.
First, let's break down why your original approaches didn't work:
- The PHP
header()redirect happens server-side, so the browser never actually loads the thank you page at all—meaning your tracking code never gets a chance to run. That's a non-starter. - Your jQuery attempt was closer, but you need to make sure you're safely passing the PHP variable to JavaScript and that the tracking code is fully loaded before the redirect happens.
Here's a complete, working solution that checks all your boxes:
Step 1: Build a Full Thank You Page
Create a proper HTML page for your thank you screen—this ensures the browser loads the page, runs your tracking code, and then redirects after the delay.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Thanks for Submitting!</title> <!-- Google Analytics Tracking Code (replace with your ID) --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-YOUR-TRACKING-ID"></script> <script> // Initialize GA window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-YOUR-TRACKING-ID'); // Optional: Track a specific form submission event (super useful for analytics) gtag('event', 'form_submission_success', { 'event_category': 'Form Actions', 'event_action': 'Completed Form', 'event_label': 'Your Form Name' }); </script> </head> <body> <div style="text-align: center; margin-top: 50px;"> <h1>Thank You!</h1> <p>Your form has been submitted successfully.</p> <p>You'll be redirected to the login page in 10 seconds...</p> </div> <!-- Delayed Redirect Script --> <script> // Wait for the page to fully load before setting the timeout document.addEventListener('DOMContentLoaded', function() { // Safely pass the PHP URL to JavaScript (avoids XSS and quote issues) const redirectUrl = <?php echo json_encode($form->data['curl_data']->url); ?>; // Set 10-second delay (10000 milliseconds) setTimeout(function() { window.location.href = redirectUrl; }, 10000); }); </script> </body> </html>
Key Details to Note:
- Safe Variable Passing: Using
json_encode($form->data['curl_data']->url)ensures the URL is properly escaped for JavaScript—no broken quotes or unexpected characters that could break your redirect. - Tracking Code Placement: Putting the GA code in the
<head>withasynclets it load without blocking the page, and adding the customform_submission_successevent gives you better insight into user behavior than just a page view. - Native JS Instead of jQuery:
DOMContentLoadedworks in all modern browsers without needing jQuery, but if you still want to use jQuery, you can replace that line with$(document).ready(function() { ... }). - Testing Tip: Shorten the timeout to
2000(2 seconds) while testing to quickly verify the tracking code fires and the redirect works, then switch back to10000for production.
This setup guarantees your Google tracking code runs fully before the redirect happens, and you keep the critical $form->data variable intact.
内容的提问来源于stack exchange,提问作者boloneysandwich
相关产品推荐
相关产品推荐

