PHP+MySQL表单结合gtag_report_conversion实现Adwords追踪问题
Implementing gtag_report_conversion for Form Submissions
First, let’s ensure you have the foundational Google Ads gtag setup in your page’s <head> section—this must load before any conversion tracking code:
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_ADWORDS_CONVERSION_ID"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'YOUR_ADWORDS_CONVERSION_ID'); </script>
Next, you’ll need the gtag_report_conversion function (provided by Google Ads when you create a conversion action). Here’s a standard, reliable version:
function gtag_report_conversion(url) { var callback = function () { if (typeof(url) !== 'undefined') { window.location = url; } }; gtag('event', 'conversion', { 'send_to': 'YOUR_ADWORDS_CONVERSION_ID/YOUR_CONVERSION_LABEL', 'event_callback': callback }); return false; }
Now, attach this tracking to your form submission with one of these two methods:
Option 1: Direct onsubmit handler (quick implementation)
Modify your form tag to include the onsubmit attribute that triggers the conversion before submission:
<form id="form1" method="post" enctype="multipart/form-data" role="form" autocomplete="off" onsubmit="return gtag_report_conversion(this.action);"> <ul class="form-box"> <li> <input name="adi" type="text" id="adi" class="form-text" required="required" placeholder="Adınız" /> </li> <li> <input name="mesaj" type="text" id="mesaj" class="form-text" required="required" placeholder="Soyadınız" /> </li> <!-- Add remaining form fields and submit button here --> </ul> </form>
Option 2: JavaScript event listener (cleaner separation of concerns)
If you prefer to keep HTML and JS separate, add this script right before your closing </body> tag:
document.getElementById('form1').addEventListener('submit', function(e) { e.preventDefault(); // Pause immediate form submission const form = this; gtag('event', 'conversion', { 'send_to': 'YOUR_ADWORDS_CONVERSION_ID/YOUR_CONVERSION_LABEL', 'event_callback': function() { form.submit(); // Submit form only after conversion is tracked } }); });
Key Tips:
- Replace
YOUR_ADWORDS_CONVERSION_IDandYOUR_CONVERSION_LABELwith the exact values from your Google Ads conversion setup (you’ll find these in the official tracking snippet Google provides). - For AJAX-powered forms, trigger the conversion event immediately after your successful AJAX response (no need to handle page navigation).
- Test with Google Ads’ Tag Assistant to confirm the conversion fires correctly when the form is submitted.
内容的提问来源于stack exchange,提问作者user3404273
相关产品推荐
相关产品推荐

