71

Google Adwords offers no code to add to your page to count a conversion if somebody clicks on a link. But as it's Javascript, I am sure there is a way to do this.

Here's the code (unaltered) Google gives you to include in the page, that should count as a conversion (most of the time a thank you page):

<!-- Google Code for Klick Conversion Page -->
<script type="text/javascript">
<!--
var google_conversion_id = 1062751462;
var google_conversion_language = "de";
var google_conversion_format = "1";
var google_conversion_color = "ffffff";
var google_conversion_label = "dKXuCODvugEQ5pnh-gM";
var google_conversion_value = 0;
//-->
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/1062751462/?label=dKXuCODvugEQ5pnh-gM&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

With other conversion tracking scripts some function has to be executed to count the conversion. Here, just adding the JS-File to your page can be enough to trigger the conversion-tracking, as conversion.js calls a function on load (download it and look at it after running it through a code beatuifier, it's really quite nice work!).

Any idea how to tackle this?

janpio
  • 10,645
  • 16
  • 64
  • 107
  • 1
    Maybe this can be useful: http://stackoverflow.com/questions/27149647/how-to-implement-a-google-adwords-conversion-pixel-using-ajax-on-a-submit-butt/27149886?noredirect=1#comment42795611_27149886 – Imnotapotato Nov 26 '14 at 15:00

5 Answers5

119

Don't know if you've already found it... I mention it anyway for future surfers...

I was looking for the same, and found this piece of code :

<script type="text/javascript"> 
    function trackConv(google_conversion_id, google_conversion_label) {
        var image = new Image(1, 1); 
        image.src = "//www.googleadservices.com/pagead/conversion/" + google_conversion_id + "/?label=" + google_conversion_label + "&script=0";  
    }
 </script>

Then for links which you want to track just do this :

<a onclick="trackConv(1234567890, 'LQV8CNq6RxCKlPbvAw');" href="http://www.example.com">Link</a> 
Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
Eli
  • 1,222
  • 1
  • 10
  • 3
  • No, didn't find anything. Looks nice! – janpio Feb 23 '11 at 18:43
  • 11
    You should remove the `&script=0` part of your arguments. Otherwise you might be sending the wrong signal to Google, saying that the user doesn't have javascript enabled, which is not accurate. In addition unless your link is a `target=_blank` or opens a new window, this is very unlikely to work at all. Your window might be redirected before the image request is made. The right way to do it, is to cancel the click event, and trigger the redirect with location.href, and a timeout, similarly to the way of tracking events explained at http://stackoverflow.com/a/19461995/1647538 – hexalys Dec 07 '13 at 01:48
  • 14
    Do you still need the conversion.js when using this method? – Misbit Nov 25 '14 at 10:33
  • where does the rest of the script go? – Imnotapotato Nov 26 '14 at 12:15
  • An interesting suggestions is to use `onmousedown` and `onkeydown` instead. http://stackoverflow.com/a/7932329/1333493 – Nemo Sep 24 '15 at 08:36
  • How do you test that it works after implementing it? Will it be immediately processed in Analytics? – WJA Jan 13 '16 at 01:47
  • @RickSanchez the rest of the script is not necessary. – Daniel Node.js Nov 27 '17 at 23:37
  • @vsdev no you dont need. – Goran Jakovljevic Jul 08 '19 at 08:14
20

It appears that Google now offers an onclick option that you can copy and paste from the Conversions page in AdWords. From the AdWords Conversions page:

Add the tag to a button on your website, such as a "Buy now" button.

Here's a snippet from the page of documentation entitled Track clicks on your website as conversions. Replace XXXXX with conversion ID and label:

<!-- Google Code for Conversion Page
In your html page, add the snippet and call
goog_report_conversion when someone clicks on the
chosen link or button. -->
<script type="text/javascript">
  /* <![CDATA[ */
  goog_snippet_vars = function() {
    var w = window;
    w.google_conversion_id = XXXXXXX;
    w.google_conversion_label = "XXXXXXX";
    w.google_remarketing_only = false;
  }
  // DO NOT CHANGE THE CODE BELOW.
  goog_report_conversion = function(url) {
    goog_snippet_vars();
    window.google_conversion_format = "3";
    var opt = new Object();
    opt.onload_callback = function() {
    if (typeof(url) != 'undefined') {
      window.location = url;
    }
  }
  var conv_handler = window['google_trackConversion'];
  if (typeof(conv_handler) == 'function') {
    conv_handler(opt);
  }
}
/* ]]> */
</script>
<script type="text/javascript"
  src="//www.googleadservices.com/pagead/conversion_async.js">
</script>

And somewhere else in your code

button.addEventListener('click', function() {
  console.log('Button clicked!');
  goog_report_conversion();
});
Flimm
  • 136,138
  • 45
  • 251
  • 267
Jonathan Guerrera
  • 971
  • 1
  • 9
  • 15
4

Google Conversion Tracking concept using Ajax on a submit button :

 $.ajax({
        type: "POST",
        url: "enquiry-submit.php",
        data: data,
        success: function (result) {
          $("#msg").fadeIn(400).html(result);

          /* Conversion Tracking Start */
          var google_conversion_id = YOUR_CONVERSION_ID_HERE;
          var google_conversion_language = "en";
          var google_conversion_format = "3";
          var google_conversion_color = "ffffff";
          var google_conversion_label = "YOUR_CONVERSION_LABEL_HERE";
          var google_remarketing_only = false;

          $.getScript('//www.googleadservices.com/pagead/conversion.js');

          var image = new Image(1, 1); 
          image.src = "//www.googleadservices.com/pagead/conversion/YOUR_CONVERSION_ID_HERE/?label=YOUR_CONVERSION_LABEL_HERE&guid=ON&script=0";  
          /* Conversion Tracking End */
        }
      });

It is 100% working on my Google Ads Campaign.

Note: You must Test this by clicking on your ad. The effect of conversion will be visible after 12 minute on your AdWords Console

Irshad Khan
  • 5,670
  • 2
  • 44
  • 39
4

I've a similar problem.

The Problem: My client have a contact page that have a form. After the user fill all the form fields, there is a validation(to check if the user filled correctly all the fields). After the validation, the user is redirected to the webmail server page. There isn't an "Success" or "Thank You" page. So i needed to put the Adwords tag, after the form validation.

The Solution:

The validation was done this way:

var missinginfo = "";
var f = document.forms["CONTACT"];
if (f.name.value == ""){
missinginfo += "\n     -  name";}
.
.
.
if (missinginfo != "") 
{
missinginfo ="_____________________________\n" +
"Empty Field" + "incorrectly filled" +
missinginfo + "\n_____________________________"
alert(missinginfo);
return false;
}
//End of Validation

So i added this snippet code:

else if(missinginfo == ""){   //Check if the form was filled correctly
adw_conv();  //Function Name
return false;
}
function adw_conv(){
var img = new Image()  //Creates an image using JS to make the request
img.src = "http://www.googleadservices.com/pagead/conversion/123456789/?label=-8bcaCNHv6AIQl_v8_QM&amp;guid=ON&amp;script=0"; 
img.onload = function(){    
var form = document.getElementsByName('CONTACT')[0];
form.submit();
}}

This way, after the form validation and before the website redirect the user to the webmail page, is triggered the Adwords Conversion!

  • This code is confusing. What is `return false` doing, when it isn't in a function? – Flimm Aug 26 '16 at 09:51
  • Also, this is dangerous. If the 'image' fails to load, your form will never be submitted. So your form depends on an external service over which you have zero control. Do you really want users to cancel because Google is having problems? – Mats de Swart Aug 14 '17 at 19:14
2

Add the code below to the section of the page you want to track conversions on.

<script>
function adwTrack() {
   var img = new Image(1,1);
   img.src =     "https://www.googleadservices.com/pagead/conversion/XXXXXXXXXX/?value=1.00&amp;currency_code=EUR&amp;label=XXXXXXXXXX&amp;guid=ON&amp;script=0";

}

Just replace the XXX… with your actual conversion id and label.

Then call the adwTrack() function we created above in your link’s onclick event:

<a href="#" onclick="adwTrack();">Track This</a>

You can also do this using GTM: https://www.redflymarketing.com/blog/track-conversions-without-a-thank-you-page/

Dave Davis
  • 844
  • 1
  • 9
  • 19