Search code examples
javascriptjqueryajaxknockout.jsgoogle-ads-api

How to add Google AdWords Conversion Tracking code in knockoutjs button click event?


I have got following code from Google AdWords.

<script type="text/javascript">
    /* <![CDATA[ */
        var google_conversion_id = 973348620;
        var google_conversion_language = "en";
        var google_conversion_format = "3";
        var google_conversion_color = "ffffff";
        var google_conversion_label = "uffQCOz32QrMe70gM";
        var google_conversion_value = [BookingValue];;
        var google_remarketing_only = false;
    /* ]]> */
</script>

<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
    <div style="display:inline;">
        <img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/973348620/?value=[BookingValue];&amp;label=uffQCOz32QrMe70gM&amp;guid=ON&amp;script=0"/>
    </div>
</noscript>

I have designed my code using knockoutjs as single page application.

I need to add this tracking code on success event of ajax call. So I need to implement all above code inside javascript code.


Solution

  • I'd recommend using the proper asynchronous version of the Google tag instead of doing any craziness of adding the synchronous version of the tag via script tags to the DOM.

    I answered this in another thread talking about angular.js which is very similar to knockout.js so the same answer should apply: How do I adapt a Google AdWords tracking pixel for use in an AngularJS app?

    Hope that helps!