1

I need to fire the Google Conversion scripts below when the myOfLineFunction runs. How can I integrate the Google variables and Google's conversion.js into myOfLineFunction to do this?

The function that runs when an offline message is sent:

<script>
myOfLineFunction('api.chat.onOfflineMessageToOperator', function(event) {
("sent_an_offline_message");
});
</script>

How do I run the Google variables below when the above myOfLineFunction runs?

<script>
var google_conversion_id = xxxxxxxxxx;
var google_conversion_language = "en";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_label = "abcdefghijklmnop";
var google_conversion_value = 0;
var google_remarketing_only = false;
</script>

And include this script, too?

<script type="text/javascript"
src="//www.googleadservices.com/pagead/conversion.js"></script>

Edit 6/10/14

This answer seems to be the way to go: (from How to track a Google Adwords conversion onclick? )

using:

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

 </script>

With tracking for links like this:

 <a onclick="trackConv(1234567890,"LQV8CNq6RxCKlPbvAw");"
href="http://somelink.com">Link</a> 

So how do I trigger the tracking within my myOfLineFunction?

Community
  • 1
  • 1
markratledge
  • 17,322
  • 12
  • 60
  • 106

2 Answers2

1

I'll make the disclaimer that the solution I included here is dirty, but it's the only one I could come up with if you're limited to running a script on the current page.

Your problem is that the inline Google conversion pixel uses document.write, so it must be included in the page when the browser renders it. We can work around this by creating a child window and running the script inside that. There's no way to hide the window (the dirty part), however we can make is as small as possible and blur it, and also close it once the Google pixel has had time to load:

function fireGcPixel() {
    var script = '<scr' + 'ipt>';
    script += 'var google_conversion_id = "xxxxxxxxxx";';
    script += 'var google_conversion_language = "en";';
    script += 'var google_conversion_format = "2";';
    script += 'var google_conversion_color = "ffffff";';
    script += 'var google_conversion_label = "abcdefghijklmnop";';
    script += 'var google_conversion_value = 0;';
    script += 'var google_remarketing_only = false;';
    script += '</scr' + 'ipt>';
    script += '<scr' + 'ipt type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"></scr' + 'ipt>';

    var win, doc;

    win = window.open('', 'dialog', 'toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,left=10000, top=10000,width=1,height=1, visible=none');
    win.onload = function(){alert("loaded");};
    doc = win.document;
    doc.write(script);
    doc.close();
    win.blur();
};

Now, if you control the domain, there are some cleaner solutions:

  1. You can place the pixel in a separate HTML file on your domain and load it via an 1x1 iframe when myOfLineFunction is called.
  2. When myOfLineFunction is called, you can redirect the user to a new page which includes the pixel.

Hope this helps.

nauten
  • 487
  • 1
  • 5
  • 11
  • Thanks for this; interesting about the conversion pixel. I didn't include the – markratledge Jun 10 '14 at 13:06
  • If you wanted to use this solution, you paste the entire function body underneath the line ("sent_an_offline_message") in your function. However, I would personally go with the noscript solution you provided. The only thing you lose there is the ability to dynamically set conversion value, and a few other variables. Definitely the easiest. – nauten Jun 10 '14 at 14:40
  • OK, thanks. I'll go with the other answer. But now: how do I integrate the funciton from the href link into my myOfLineFunction? – markratledge Jun 10 '14 at 17:13
  • See my other answer above. You should just be able to include that block of JS in your page, and swap in your conversion ID and label. – nauten Jun 11 '14 at 02:42
1

Try this:

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

myOfLineFunction('api.chat.onOfflineMessageToOperator', function(event) {
    trackConv(1234567890,"LQV8CNq6RxCKlPbvAw");
});
nauten
  • 487
  • 1
  • 5
  • 11