How to implement Facebook conversion tracking onclick or when there is no separate thank you page

Trying to implement Facebook conversion tracking on the Facebook tab. You can view the page here http://www.facebook.com/StChristophersFellowship/app_366591783429546 . Problem - a separate page does not start after the form is submitted. Can I make part of a javascript run, but ONLY when I click the submit button, I think that it will also be entered into the head of the HTML document. I found this answer to run Javascript by reference or clicked - will this method work if I call the tracking / conversion code from a separate JS document?

Any help would be greatly appreciated - Thank you!

"I have to agree with the comments above that you cannot call the file, but you can load the JS file like this, I'm not sure if it answers your question, but it can help ... oh and I used the link instead of the button in my example ...

<a href='linkhref.html' id='mylink'>click me</a>

<script type="text/javascript">

var myLink = document.getElementById('mylink');

myLink.onclick = function(){

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "Public/Scripts/filename.js."; 
document.getElementsByTagName("head")[0].appendChild(script);
return false;

}


</script>"
+5
source share
2 answers

I tried to use the Nicolás solution, but the conversion was never tested.

However, I found an article describing a method that worked instantly using the URL from <noscript><img src="..." /></noscript>.

Put an element on your page, for example:

<div id="fb_pixel"></div>

Then declare these CSS rules for your conversions, changing background-imagefor the URL <noscript>found in your own tracking code.

#fb_pixel { display:none; }
.fb_conversion
{
    background-image: url('https://www.facebook.com/offsite_event.php?id=33333333333&value=0&currency=USD');
}

, (, AJAX), fb_conversion <div> JavaScript. , jQuery:

$("#fb_pixel").addClass("fb_conversion");

, .

+17

, , AJAX- . :

$(function () {
    $('#btnSend').click(facebookTrackingSendClick);
});

function facebookTrackingSendClick(e) {
    e.preventDefault();
    var clickedElement = this;
    var fb_param = {};
    fb_param.pixel_id = '123';
    fb_param.value = '0.00';
    fb_param.currency = 'BRL';
    (function () {
        var fpw = document.createElement('script');
        fpw.async = true;
        fpw.src = '//connect.facebook.net/en_US/fp.js';
        fpw.onload = function () {
            // Callback here.
        };
        var ref = document.getElementsByTagName('script')[0];
        ref.parentNode.insertBefore(fpw, ref);
    })();
}

+2

All Articles