【问题标题】:How to track Facebook Share button in Google Analytics (analytics.js)如何在 Google Analytics (analytics.js) 中跟踪 Facebook 分享按钮
【发布时间】:2015-09-09 13:45:21
【问题描述】:

我正在尝试使用谷歌分析跟踪我网页中的所有社交互动,以便我可以在 Acquisition --> Social --> plugins 下查看它们。

目前我可以跟踪 facebook 的点赞和发送按钮,但不跟踪分享按钮。我找不到共享按钮的任何特定代码。网络上的大多数代码都被提到用于共享/发送,但共享没有被跟踪。我提供了下面的代码

        <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>

    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r; i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new    
    Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g; 
    m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-xxxxxxxx', 'auto');
    ga('send', 'pageview');

</script>
</head>
<body>
<div id="fb-root"></div>
      <script>
      $(document).ready(function(){
    alert('doc loaded');
});

      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'xxxxxxxxxxxxxxxxxx',
          status     : true,
          xfbml      : true
        });
                //Logged In Users
        FB.getLoginStatus(function(response) {
            if (response.status !== "unknown") { 
                ga('set', 'dimension1', 'Logged In');
            }
        });

        //Facebook Likes 
        FB.Event.subscribe('edge.create', function(href, widget) {
            var currentPage = $(document).attr('title');
            ga('send', {
                'hitType': 'social',
                'socialNetwork': 'Facebook',
                'socialAction': 'Like',
                'socialTarget': href,
                'page': currentPage,
                'hitCallback': function() {
                 alert('like done sending data');
  }

            });
        });

        //Facebook Unlikes
        FB.Event.subscribe('edge.remove', function(href, widget) {
            var currentPage = $(document).attr('title');
            ga('send', {
                'hitType': 'social',
                'socialNetwork': 'Facebook',
                'socialAction': 'Unlike',
                'socialTarget': href,
                'page': currentPage,
                'hitCallback': function() {
    alert('unlike done sending data');
  }

            });
        });

        //Facebook Send/Share
        FB.Event.subscribe('message.send', function(href, widget) {


            var currentPage = $(document).attr('title');
            ga('send', {
                'hitType': 'social',
                'socialNetwork': 'Facebook',
                'socialAction': 'Send',
                'socialTarget': href,
                'page': currentPage,
                'hitCallback': function() {
    alert('send done sending data');
  }

            });
        });

        //Facebook Comments
        FB.Event.subscribe('comment.create', function(href, widget) {
            var currentPage = $(document).attr('title');
            ga('send', {
                'hitType': 'social',
                'socialNetwork': 'Facebook',
                'socialAction': 'Comment',
                'socialTarget': href,
                'page': currentPage,
                'hitCallback': function() {
    alert('comment done sending data');
  }

            });
        });
      };
// Load Facebook SDK
    (function(d, s, id) {
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) return;
     js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.4&  appId=xxxxxxxxxxxx";
      fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
      </script>


      <p><a href="www.example.in" >Some Text </a></p>

      <script src="//connect.facebook.net/en_US/all.js#xfbml=1"></script>
      <fb:like></fb:like>

     <div class="fb-share-button" data-href="https://example.com"  data-layout="button_count"></div>

     <div class="fb-send" data-href="http://example.com"></div>



      </body>
      </html> 

我从这个链接https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.4 看到了 FB.event.subscribe 方法参数,但是没有具体的 share 参数。 我究竟做错了什么?还是无法在 facebook 中跟踪分享按钮。

【问题讨论】:

  • 不幸的是,我认为这不可能与 FB 事件有关。所有可能的事件都在您提供的文档中,因此如果不存在,您将无法执行。

标签: javascript facebook google-analytics facebook-social-plugins analytics.js


【解决方案1】:

据我所知,无法订阅共享按钮事件,但使用 Share Dialog 实现自定义按钮会在完成时提供回调。您可以在此回调中将事件推送到 Google Analytics。

这是一个例子:

FB.ui({
    method: 'share',
    href: 'https://developers.facebook.com/docs/',
}, function(response){
     //Google Analytics send here
});

您还可以通过检查 response.error_message 来验证响应中的共享是否有效,尽管来自文档的注释:

请注意,response.error_message 仅在使用您的应用的人使用 Facebook 登录对您的应用进行身份验证时才会出现。

【讨论】:

    【解决方案2】:

    根据文档 (https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.7),分享按钮仍然没有可用的事件...

    【讨论】:

    • 您的回答不够完整。您应该提供一个链接,甚至更好地引用文档中的内容。
    • 根据旧的“message.send”事件的错误历史,facebook 决定不再发送用于分享操作的事件。 developers.facebook.com/bugs/1734301140155193
    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    相关资源
    最近更新 更多