Google Ads enables you to track important user actions on your website using a conversion tracking code, also known as an event snippet. This code helps measure campaign success and optimize advertising strategies. In this article, we will explore the correct methods of implementation using onclick and setTimeout to track clicks and user interactions.
Basic Structure of the Google Ads Conversion Code
Conversion tracking works through two key components:
- Global Site Tag (gtag.js) – must be placed on all pages of the website.
- Event Snippet – fires only when a conversion occurs (e.g., form submission or button click).
The global site tag should be added inside the <head>
section:
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-XXXXXXXXXX');
</script>
Triggering the Event Snippet on Button Click (onclick)
If you want to track a conversion when a user clicks a specific button (e.g., "Submit Form" or "Book Now"), you can use the onclick
method.
Example implementation:
<button onclick="recordConversion()">Submit Form</button>
<script>
function recordConversion() {
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXXXXX/abcdEFGHIJKL',
'value': 100.0,
'currency': 'USD'
});
}
</script>
Advantages of this approach:
- Fires immediately upon click.
- Allows tracking of specific interactions (e.g., clicks on CTA buttons).
Delaying the Event Snippet Execution (setTimeout)
If you want to ensure that a conversion is recorded before the user leaves the page, you can introduce a small delay using setTimeout
. This approach is useful for form submissions or actions that redirect users to another page.
Example implementation with setTimeout
:
<button onclick="delayedConversion()">Complete Purchase</button>
<script>
function delayedConversion() {
setTimeout(function() {
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXXXXX/abcdEFGHIJKL',
'value': 200.0,
'currency': 'USD'
});
}, 2000); // Delay of 2 seconds before sending the conversion event
}
</script>
Advantages of this approach:
- Reduces the risk of losing conversion data if the user leaves too quickly.
- Increases the reliability of tracking for actions that involve page redirection.
Implementing the Code via Google Tag Manager
If you prefer not to modify your website's code directly, you can manage conversion tracking using Google Tag Manager (GTM):
- Create a new tag → Google Ads Conversion Tracking.
- Enter your Conversion ID and Event Snippet Code.
- Set the trigger to button click or thank-you page view.
- Publish changes and test in GTM Preview Mode.
How to Verify That the Conversion Tracking Works Correctly?
After implementation, it is essential to check that Google Ads correctly records conversions:
- Use Google Tag Assistant to inspect tag loading.
- Monitor conversions in Google Ads Conversion Tracking.
- Test the setup using Google Tag Manager Preview Mode.
Properly setting up the Google Ads event snippet is crucial for effective conversion tracking. Using the onclick method allows you to track specific user actions, while setTimeout ensures reliable data submission. If you want greater control over conversion tracking, Google Tag Manager is the recommended approach.