Error in Google AdWords Conversion Services - Asynchronous Conversion Code

I have never used Google Adwords on the site, so if I am mistaken about "lingo", do not hesitate to correct me.

I’m working on a site that has a landing page for one of its campaigns on Google AdWord. On this page there is a form that, when processing, takes you to another page to say "Thank you for your request ...". I deleted this and rewrote it in PHP and Javascript to prevent the page from refreshing or redirecting.

The problem is that on the thank you page, the Google code is a little different and runs when the page loads. My question is, how can I re-execute the conversion code with different variables without reloading the page? Is there a google script for this?

Deletes a script tag and then re-places it again to re-script?

This is the code I have (before submitting the form):

<!-- Google Code for Company Remarketing List Remarketing List --> <script type="text/javascript"> /* <![CDATA[ */ var google_conversion_id = 000000; var google_conversion_language = "en"; var google_conversion_format = "3"; var google_conversion_color = "ffffff"; var google_conversion_label = "abcdefg"; 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/000000/?label=abcdefg&amp;guid=ON&amp;script=0"/> </div> </noscript> 

What you need to change after submitting the form:

 var google_conversion_id = 111111; var google_conversion_label = "gfedcba"; "http://www.googleadservices.com/pagead/conversion/gfedcba/?label=111111&amp;guid=ON&amp;script=0 

Changing variables is easy! The hard part is restarting the script with the new variables.

Any help is much appreciated!

UPDATE

The answer to the post here probably solves this question, however I would like to know how I can represent other variables with the variables mentioned in this answer. They are pretty clear, but I can't be sure that they are right!

Also, does anyone know where on Google I can really see instructions for this?

+2
source share
1 answer

The reason you cannot just re-execute the script, as you may have noticed, is that it uses document.write , which should not be called after the document is already loaded.

One possible solution is to simply repay the GIF request yourself, as you mentioned. If you really want to re-execute the script, there is the option of redirecting document.write .

Here's a general idea of ​​how this can be done - this snippet will be posted somewhere where you upload new content to your page. It is assumed that you are using jQuery and have already loaded the contents of the new page into $newContent and marked all the script tags that must be executed when you reload with class="ajax-exec" . It executes the inline script directly and uses the jQuery $.ajax with dataType: script . Then it waits until all external scripts are executed and add the redirected output to the hidden div .

This works for us, but comes with no warranty (:

 // Execute js from the new content (eg AdWords conversions tags). // We redirect document.write to a string buffer as we're already // done loading the page at this point. var buf = ''; var writeMethSave = document.write; $('div#lazyload-buf').remove(); var done = {}; document.write = function (string) { buf += string; }; $newContent.filter('script.ajax-exec').each(function () { var url = $(this).attr('src'); if (url) { // External script. done[url] = false; $.ajax({ url: url, dataType: "script", success: function () { done[url] = true; } }); } else { // Inline script. $.globalEval($(this).html()); } }); function checkForDone () { for (var url in done) { if (!done[url]) { setTimeout(checkForDone, 25); return; } } // All done, restore method and write output to div document.write = writeMethSave; var $container = $(document.createElement("div")); $container.attr('id', 'lazyload-buf'); $container.hide(); $(document.body).append($container); $container.html(buf); }; setTimeout(checkForDone, 25); 
+1
source

All Articles