javascript - Set up Facebook pixel to fire on click event - Stack Overflow

I want to set up a pixel to fire when a button is clicked. Facebook offers some info here on doing this

I want to set up a pixel to fire when a button is clicked. Facebook offers some info here on doing this very thing, but I am a javascript novice and can't figure it out. How does one use the info on that page to alter the code below to fire on a button click? Here is the base code snippet:

<script>(function() {
var _fbq = window._fbq || (window._fbq = []);
if (!_fbq.loaded) {
var fbds = document.createElement('script');
fbds.async = true;
fbds.src = '//connect.facebook/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', 'MyPixelID', {'value':'0.01','currency':'USD'}]);
</script>

I want to set up a pixel to fire when a button is clicked. Facebook offers some info here on doing this very thing, but I am a javascript novice and can't figure it out. How does one use the info on that page to alter the code below to fire on a button click? Here is the base code snippet:

<script>(function() {
var _fbq = window._fbq || (window._fbq = []);
if (!_fbq.loaded) {
var fbds = document.createElement('script');
fbds.async = true;
fbds.src = '//connect.facebook/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', 'MyPixelID', {'value':'0.01','currency':'USD'}]);
</script>
Share Improve this question asked Nov 5, 2014 at 3:12 powerdownpowerdown 472 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You're just missing the function to call which "fires" the pixel and the button...

Javascript:

function myButtonClicked(val, curr) {
  //Do your thing
  window._fbq.push(['track', 'MyPixelID', {'value':val,'currency':curr}]);
}

And the button:

<button onClick="myButtonClicked(val, curr);" />

Or a link styled to look like a button:

<a href="javascript:myButtonClicked(val, curr);">Text</a>

And, continuing on per the ments:

<a href="http://example." onclick="myButtonClicked(val, curr);">Text</a>

Note: In the line above, the onclick is processed first, then since propagation is not cancelled by any means, the href navigation executes.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744102381a4558590.html

相关推荐

  • javascript - Set up Facebook pixel to fire on click event - Stack Overflow

    I want to set up a pixel to fire when a button is clicked. Facebook offers some info here on doing this

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信