【问题标题】:Call Facebook pixel fbq when button is clicked单击按钮时调用 Facebook 像素 fbq
【发布时间】:2020-08-21 20:44:33
【问题描述】:

当单击某个按钮时,我试图在我的 Facebook 像素中触发 fbq 功能。使用下面的代码,我的控制台告诉我“a 为空”。我该如何解决这个问题?

附:我知道我的链接代码很乱。这对于我设置按钮样式的方式是必要的。

<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window,document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
 fbq('init', '<pixel_id>'); 
fbq('track', 'PageView');
  
</script>
<noscript>
 <img height="1" width="1" 
src="https://www.facebook.com/tr?id=<pixel_id>&ev=PageView
&noscript=1"/>
</noscript>
<!-- End Facebook Pixel Code -->


<!-- This code is in the footer -->

<script>
  
  
  var a = document.getElementById('call-fbq');
  a.addEventListener(
    'click', 
    function() { 
      fbq('track', 'AddToCart');          
    },
    false
  );
  
    
  
</script>
<div class="button-wrapper-2-head"><a href="#row--89322" id="call-fbq">
		<div class="button-wrapper-head">
			<div class="button-head">
		Button
		</div>
		</div>
	</a></div>

【问题讨论】:

    标签: javascript html css facebook facebook-pixel


    【解决方案1】:

    您的 javascript 似乎运行得太早了。为确保您的标记的所有元素都已加载,请尝试在窗口的加载事件中运行您的脚本:

    window.addEventListener('load', function() {
    
          // Your stuff here
          var a = document.getElementById('call-fbq');
          a.addEventListener(
            'click',
            function() {
              fbq('track', 'AddToCart');
            },
            false
          );
    
    });
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2019-06-04
      • 1970-01-01
      相关资源
      最近更新 更多