Tracking AdWords Conversions within JavaScript
How you can track Adwords Conversions from JavaScript callbacks, AJAX requests etc.
Today I want to talk about grabbing AdWords conversions from things like AJAX requests, JavaScript callbacks, and any other way from inside your website that doesn’t include putting it on a “thank you” page.
When you set up AdWords conversion tracking you will be told to set up something like this on your website;
This is designed to go on the page that lets the user, and you, know that a particular thing has happened – this could be signing up to an email list, buying something, registering on your website, you get the idea.
It has been designed to be used as is, on a page that will only been seen by someone who has completed the action.
Not all websites work like that. Lots of websites keep people on the same page as they perform actions and this means people will hit your page who haven’t necessarily performed an action that you want to track with AdWords.
Enter async AdWords
What Google AdWords doesn’t tell you when it offers up the above code is that if you require this to fire based on a callback of some sort it isn’t just a case of shoe-horning that code into your callback, you need to use an entirely different set of code.
Here is the code you actually need.
This can go anywhere on the page that will be running the JavaScript being used. The main difference here is instead of loading conversion.js
we are loading conversion_async.js
.
You should add this code into the callback or wherever you need to trigger the conversion. It has all the same parameters you had to set before but now they are being included as one thing being added into google_trackConversion
.
You will note I haven’t added the non-JS code that AdWords supply you, like the noscript
tracking image. Since we are calling everything else from within JavaScript there is no point in providing a fallback.
If you need it to work without JavaScript then you would use the more traditional “thank you” page approach in combination with the Async one.