admin管理员组文章数量:1394774
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
1 Answer
Reset to default 6You'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.
本文标签: javascriptSet up Facebook pixel to fire on click eventStack Overflow
版权声明:本文标题:javascript - Set up Facebook pixel to fire on click event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744102381a2590924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论