Facebook Tracking of In-Page Events with Conversion Pixel Code - facebook

Full disclosure - I'm not a programmer, but I'm the only one in my organisation who might be able to get this working. Can anyone help with the following please?
I'm trying to use Facebook's conversion pixel code to track certain button clicks on our site. Facebook's developer docs give the following instructions for tracking in-page events:
After the base code snippet is installed, you can track in-page actions, such as clicks on a button, by making a _fbq.push('track') call for the conversion pixel through registering different event handlers on an HTML DOM element. For example:
function trackConversionEvent(val, cny) {
var cd = {};
cd.value = val;
cd.currency = cny;
_fbq.push(['track', '<pixel_id>', cd]);
}
<button onClick="trackConversionEvent('10.00','USD');" /
The problem I'm facing is it's not clear to me what Facebook means by "the base code snippet". My initial assumption was that it's the conversion pixel code they give you to install in the head section of the page, i.e.
<!-- Facebook Conversion Code -->
<script>(function() {
var _fbq = window._fbq || (window._fbq = []);
if (!_fbq.loaded) {
var fbds = document.createElement('script');
fbds.async = true;
fbds.src = '//connect.facebook.net/en_US/fbds.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(fbds, s);
_fbq.loaded = true;
}
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', '<pixel_id>', {'value':'0.00','currency':'USD'}]);
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=<pixel_id>&cd[value]=0.00&cd[currency]=USD&noscript=1" /></noscript>
However, if I install the above on our page it loads/fires the conversion pixel code each time the page loads rather than on the button click (presumably because it's typically used on a thank you/confirmation page). Can anyone shed any light on this for me? I believe I understand where to position the onClick code to associate it with a button click but I'm struggling to understand where I need to position the trackConversionEvent code and what the base code snippet is. Through testing, I know that removing the following lines from the larger code snippet stops the pixel from loading:
window._fbq = window._fbq || [];
window._fbq.push(['track', '<pixel_id>', {'value':'0.00','currency':'USD'}]);
but I'm not sure if that's actually what I need to do. Do I for example, need to replace those two lines with the trackConversionEvent code so that the pixel doesn't fire when the page loads but the onClick code actually functions instead when the button is clicked?
Many thanks in advance for any pointers or suggestions.

This should work, I.ve just tested on a blog based on WP.
<!-- Facebook Conversion Code -->
<script>(function() {
var _fbq = window._fbq || (window._fbq = []);
if (!_fbq.loaded) {
var fbds = document.createElement('script');
fbds.async = true;
fbds.src = '//connect.facebook.net/en_US/fbds.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(fbds, s);
_fbq.loaded = true;
}
})();
window._fbq = window._fbq || [];
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=<pixel_id>&cd[value]=0.00&cd[currency]=USD&noscript=1" /></noscript>
<!-- End Of Facebook Conversion Code -->
This is the event handler that you could bind to any button, link or any DOM element::
function trackConversionEvent(val, cny) {
var cd = {};
cd.value = val;
cd.currency = cny;
_fbq.push(['track', '<pixel_id>', cd]);
}
<button onClick="trackConversionEvent('10.00','USD');"

Related

DOMContentLoaded is not firing

I am trying to create a chrome extension but having problems with DOMContentLoaded as it is not firing.
Note: my code was taken from a different website.
Basically, I have create an HTML file with a button:
<head>
<title>GTmetrix Analyzer</title>
<script src="popup.js"></script>
</head>
<body>
<h1>GTmetrix Analyzer</h1>
<button id="checkPage">Check this page
now!</button>
</body>
And this is the JS file (popup.js):
document.addEventListener
('DOMContentLoaded',
function() {
console.log("f")
var checkPageButton =
document.getElementById('checkPage');
checkPageButton.addEventListener('click',
function() {
chrome.tabs.getSelected(null,
function(tab) {
d = document;
var f = d.createElement('form');
f.action = 'http://gtmetrix.com/analyze.html?bm';
f.method = 'post';
var i = d.createElement('input');
i.type = 'hidden';
i.name = 'url';
i.value = tab.url;
f.appendChild(i);
d.body.appendChild(f);
f.submit();
});
}, false);
}, false);
I added the console.log event in order to check if the event is executed, so this is how I verified that it isn't working.
I also added run_at": "document_start
but then I got
Uncaught TypeError: Cannot read property 'addEventListener' of null
For the "click" event, so I guess that the event was triggered before the button was created.
Help, please!

Google GPT - googletag.cmd queue re-order

Could somebody help me with the Google GPT googletag.cmd queue re-order issue?
(1) in the home HTML page, I have this:
googletag.cmd.push(function() { defineSlot1(); });
googletag.cmd.push(function() { defineSlot2(); });
googletag.cmd.push(function() { defineSlot3(); });
googletag.cmd.push(function() { defineSlot4(); });
(2) in attached js, I dynamically added:
googletag.cmd.push(function() { defineSlot5(); });
(3) So right now in the googletag.cmd queue, the sequence like this:
[slot1, slot2, slot3, slot4, slot5];
My question is, how to move the slot5 to the second place after slot1, like this:
[slot1, slot5, slot2, slot3, slot4];
In https://developers.google.com/doubleclick-gpt/reference,
googletag.commandArray only has push(f) method, and the googletag.cmd is not a standard array.
How to do this? Thanks.
Google GPT library is loaded in async mode and it initially defines googletag.cmd as a regular Array.
<script type='text/javascript'>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' == document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') +
'//www.googletagservices.com/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>
When the main library gets downloaded (it occurs in parallel with page rendering) it converts the initially defined array into a custom object (https://developers.google.com/doubleclick-gpt/reference#googletag.CommandArray) so you indeed won’t be able to modify it.
Back to your question:
If you use
googletag.pubads().enableSingleRequest() (https://developers.google.com/doubleclick-gpt/reference#googletag.PubAdsService_enableSingleRequest) all of your adslots will be queried and displayed on the first googletag.display(…) call.
If you don’t use enableSingleRequest an individual query will be sent each time you call googletag.display(…) for a particular ad slot regardless of its definition order
Considering p.1 and p.2 I don’t get why would you care the order of slots definition.
Anyway, if you really need it you might define your own queue variable
window.custom_cmd = [];
and use it instead of googletag.cmd throughout your page so it would look as follows:
custom_cmd.push(function() {
// regular defineSlot(…) calls
});
and then somewhere after your attached js file is loaded just
// reorder it or whatever else
…
// copy your queue into the regular queue
for (var i = 0; i < custom_cmd.length; i++) {
googletag.cmd.push(custom_cmd[i]);
}
this code would trigger the ad calls

drupal 7 adding facebook tracking code to specific page

I need to add Facebook tracking code to a specific page in Drupal 7. I was wondering if there was a way to add the code to the html.tpl.php file where there is some code that calls for it to be added to a specific page.
I tried adding the code to a block which would live on the page, but the code is being stripped out (disable rich text, add code and save)
This is the code I am trying to add
<!-- Facebook Conversion Code for Ecuador landing page leads -->
<script>(function() {
var _fbq = window._fbq || (window._fbq = []);
if (!_fbq.loaded) {
var fbds = document.createElement('script');
fbds.async = true;
fbds.src = '//connect.facebook.net/en_US/fbds.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(fbds, s);
_fbq.loaded = true;
}
})();
window._fbq = window._fbq || [];
window._fbq.push(['track', '6026961635755', {'value':'0.00','currency':'USD'}]);
</script>
<noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=6026961635755&cd[value]=0.00&cd[currency]=USD&noscript=1" /></noscript>
After I save it this is what I am getting
<!-- Facebook Conversion Code for Ecuador landing page leads --><noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=6026961635755&amp;cd[value]=0.00&amp;cd[currency]=USD&amp;noscript=1" /></noscript>
Facebook Tracking Pixel module provides an easy to use interface that enables you to include the conversion code to the page(s) you select.
It applies the 'new' Facebook pixel code, the one that Facebook recommends using.
Drupal project page:
https://www.drupal.org/project/facebook_tracking_pixel
When you create a static block for text format (drop-down bellow body field) select "plain text" so drupal shouldn't filter out some tags. Under visibility settings you can specify on what pages this block should be shown. For more powerful mechanism for specifying on what page to display the block look for "context" module.

Connecting a simple gain node to Web Audio API file buffers

I can't seem to understand why this isn't working. I have no errors.I have done this using oscillators and it works fine. FYI this is a continuation of this thread:
Using Web Audio API to assign two sounds to two Divs and play each independently via a click event with javascript
Code:
<div id="divElement"></div>
<style>
#divElement{
background-color: orange;
width:70px;
height:100px;
left:143px;
}
</style>
<script>
var context = new webkitAudioContext(),
savedBuffer;
var playAudioFile = function () {
var source = context.createBufferSource();
var gainNode = context.createGainNode();
source.buffer = savedBuffer;
// START OF PROBLEM AREA: Gain not working but file still plays with no errors.
source.connect(gainNode);
gainNode.connect(context.destination);
gainNode.gain = 0;
// END OF PROBLEM AREA
source.noteOn(0); // Play sound immediately
};
var request = new XMLHttpRequest();
request.open('get', 'audio/A.mp3', true);
request.responseType = 'arraybuffer';
request.onload = function () {
context.decodeAudioData(request.response,
function(incomingBuffer) {
savedBuffer = incomingBuffer;
var divElement = document.getElementById("divElement");
divElement.addEventListener("click", playAudioFile , false);
}
);
};
request.send();
</script>
Try gainNode.gain.value = 0 instead. gainNode.gain is an AudioGain object, which has the attribute value.
http://www.w3.org/TR/webaudio/#AudioGainNode

FB.Canvas.getPageInfo not working

I am working on a FB app and used FB.Canvas.getPageInfo to get the scroll of the canvas to set popups in the screen center because the application is not scrolling but its the fb canvas which is scrolling and to find the center of the screen on long pages i need to get the number of pixels the canvas is scrolled and then with some plus minus i get the screen center of current page position. This was working fine till yesterday but is not since morning, I have tried alot but no success yet.
the code is called in setTimeout function, until the value is received I forcefully set my popup's dispaly to none and show a loader image.
remember it was working fine til last night
here is the code
$('.popup-call-local').live('click', function(event){
//FB.Canvas.getPageInfo(function(info){alert(info.scrollTop);});
var view_name = this.id;
var check_view_name = view_name.replace(/_/g, '-');
if($('#'+check_view_name).length > 0){
$('#popupDiv').css('height',$('.wrap').css('height'));
$('#popupDiv').css('display','block');
$('#popupDiv').css('z-index',1000);
var x = Number((window.screen.width - 400) / 2);
var y = Number(event.pageY)+ 10;
$('#popupDiv').html('<img src="<?php echo base_url(); ?>images/loaderblack.gif" style="position:absolute; z-index:50000; left:'+x+'px; top:'+y+'px;" />');
$('#'+check_view_name).css('z-index',2000);
var viewportHeight = window.screen.height;
var windowScrolled = -1;
$foo = jQuery('#'+check_view_name),
elWidth = $foo.width(),
elHeight = $foo.height(),
elOffset = $foo.offset();
setTimeout(function(){FB.Canvas.getPageInfo(function(info){windowScrolled=info.scrollTop; while(windowScrolled == -1){
$('#'+check_view_name).css('display','none');}
var v = (((viewportHeight - elHeight) / 2) + (windowScrolled) - 100); if(v<=0){v=Number(v)*Number(-1);}$('#popupDiv').html('');$('#'+check_view_name).css('display','block');$('#spinner').css('display','none');$('#'+check_view_name).css('top', Number(v) + 'px');});}, 3000);
}else{
return false;
}
});
EDIT
well
just found that its a facebook bug reported here
http://developers.facebook.com/bugs/185097921606322?browse=search_4f60961cc946d7143000700