I had a Google Adwords Conversion tracking code that I needed to implement basically on an onclick
event. The form I am tracking, submits information using AJAX and then renders a ‘Thank you‘ message to the page by replacing the form’s div with the ‘Thank you‘ HTML.
I googled for this requirement and found a solution on Google Adwords Conversion with AJAX which I am sharing here.
Google Adwords Conversion with AJAX
Actually Google itself provides an asynchronous version of the AdWords Remarketing Tag because it is useful for websites where the tag needs to be fired other than during a page load means like AJAX.
There are 2 steps to implement asynchronous version of the AdWords in the website:
1. Loads the conversion_async.js script from the ‘googleadservices.com‘ server. This can be loaded by adding the following snippet inside of the head section of your site:
1 | <script src="http://www.googleadservices.com/pagead/conversion_async.js" type="text/javascript" charset="utf-8"></script> |
What this step does: By loading conversion_async.js, google_trackConversion function will be added to the window object.
2. Call the google_trackConversion function. We can call this function whether on page load or at appropriate time by programmatically.
When calling the google_trackConversion function the parameters that were globally-scoped for the standard AdWords Remarketing Tag are now encapsulated within an object passed as an argument.
Firing the asynchronous AdWords on page load:
1 2 3 4 5 6 7 8 9 10 11 12 | <script type="text/javascript"> window.google_trackConversion({ google_conversion_id: 123456789, google_conversion_language : "en", google_conversion_format : "3", google_custom_params: { parameter1: 'abc123', parameter2: 29.99 }, google_remarketing_only: true }); </script> |
We can directly call google_trackConversion function on page load with in a ‘script‘ tag like above given example.
Firing the asynchronous AdWords Remarketing Tag programmatically
We can also fired asynchronous AdWords by calling google_trackConversion function from our existing JavaScript code whenever is appropriate, for example as part of a ‘XMLHttpRequest‘ or AJAX or on any form or form controls events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | $('#sendBtn').click(function(){ //get info var fullname = $("#fullname").val(); var email = $("#email").val(); var text = $("#text").val(); //send info to php $.ajax({ beforeSend: function() { if ( IsEmail(email) == false) { $('#aboutUnsuccess').show("slow"); $('#contactform').hide("slow"); } }, url: 'http://www.example.com/contact.php', type: "POST", data: ({ "fullname": fullname, "email": email, "text": text }), success: function (results){ if ( IsEmail(email) == true) { window.google_trackConversion({ google_conversion_id: 123456789, google_conversion_language : "en", google_conversion_format : "3", google_custom_params: { parameter1: 'abc123', parameter2: 29.99 }, google_remarketing_only: true }); $('#aboutSuccess').show("slow"); } } }); }); |
On XMLHttpRequest :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | var xmlRequest = new XMLHttpRequest(); xmlRequest.onreadystatechange = function() { if (xmlhttp.readyState === 4){ window.google_trackConversion({ google_conversion_id: 123456789, google_conversion_language : "en", google_conversion_format : "3", google_custom_params: { parameter1: 'abc123', parameter2: 29.99 }, google_remarketing_only: true }); } }; xmlRequest.open("post", "addToBasket", true); xmlRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlRequest.send("product=abc123&price=29.99"); |
This is all light above is an example to understand how can we integrate Google Adwords Conversion with AJAX.
Awesome man – Really saved my day 🙂
Worked like a charm