使用Analytic Universal跟踪Facebook按钮点击次数

时间:2015-05-27 00:51:05

标签: javascript facebook google-analytics

我需要一些帮助才能让我的代码正常工作。

我正在尝试跟踪自定义FB共享按钮上的点击次数。

我需要实现此代码:

onClick="ga('send', 'event', 'category', 'action', 'ShareFBButton');">

这是我的代码:

<script type="text/javascript">
function fbs_click(width, height) {
    var leftPosition, topPosition;
      //Allow for borders.
    leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
    //Allow for title and status bars.
  topPosition = (window.screen.height / 2) - ((height / 2) + 10);
var windowFeatures = "status=no,height=" + height + ",width=" + width + ",
  resizable=yes,left=" 
+ leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + 
 ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no";
u=location.href;
t=document.title;
window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer', windowFeatures);
return false;}
 </script> 

 <a href="http://www.facebook.com/share.php?u=<?php the_permalink()?>" 
  onClick="return fbs_click(570, 300)" target="_blank" title="ShareOnFacebook">
<img src="images/shareonfacebook.png" width="614" height="54"   
 alt="ShareOnFacebook"></a>`

1 个答案:

答案 0 :(得分:0)

只需将其添加到回调正文中:

<script> 
function fbs_click(width, height) {
  ga('send', 'event', 'category', 'action', 'ShareFBButton');
  /*....
  ....
  .... rest of script
  ....*/
  t=document.title;
  window.open('http://www.facebook.com/sharer.php?        u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer', windowFeatures);
 return false;}
</script>