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?

2 Answers 2

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");
});
Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

Thanks for this; interesting about the conversion pixel. I didn't include the <noscript> code that directly calls the pixel as I didn't really want to bother with disabled javascript browsers. But how do I add your code into my myOfLineFunction function? I'm a javascript newbie. And, how does your answer relate to this question/answer: stackoverflow.com/questions/2082129/… Am I better off with that?
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.
OK, thanks. I'll go with the other answer. But now: how do I integrate the funciton from the href link into my myOfLineFunction?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.