如何跟踪Facebook分享按钮在谷歌分析(Analytics .js)

How to track Facebook Share button in Google Analytics (analytics.js)

本文关键字:谷歌 Analytics js 分享 何跟踪 跟踪 Facebook 按钮      更新时间:2023-09-26

我正在尝试使用谷歌分析跟踪我的网页上的所有社交互动,以便我可以在收购->社交->插件下查看它们。

目前我能够跟踪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

据我所知,没有办法订阅共享按钮事件,但是用share Dialog实现自定义按钮提供了完成时的回调。你可以在这个回调中将事件推送到Google Analytics。

下面是一个例子:

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

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

注意响应。error_message只会出现,当有人使用你的应用程序与Facebook登录身份验证你的应用程序。

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