将Google Analytics点击(事件)跟踪代码添加到Javascript window.open

时间:2013-05-14 01:03:22

标签: javascript google-analytics

我脑子里有以下Javascript:

// Google Analytics code required for EventTracking <script type="text/javascript"> function trackOutboundLink(link, category, action) { try { _gaq.push(['_trackEvent', category , action]); } catch(err){} setTimeout(function() {document.location.href = link.href; }, 100); } 

以下是我的身体:

// required to activate social bookmarks
$(".btn_fb").click(function() {
    window.open("");
    return false;
});    
$(".btn_tw").click(function() {
    window.open("http://twitter.com/home?status=Webpage Title"+document.URL+"", "Twitter", "width=660,height=400,scrollbars=no;resizable=no");
    return false;
});    
$(".btn_li").click(function() {
    window.open("http://www.linkedin.com/shareArticle?mini=true&amp;url="+document.URL+"&amp;title=Webpage Title;summary=Webpage summary", "LinkedIn", "width=660,height=400,scrollbars=no;resizable=no");
    return false;
});         $('.btn_go').click(function() {
    window.open("http://plus.google.com/share?url="+document.URL, 'GooglePlus', 'width=660,height=500,scrollbars=no,resizable=no');
    return false;
}); 
$(".btn_ma").attr("href", "mailto:?subject=Webpage Subject&body=Webpage summary" + window.location);  </script>

//GA Code
    <script type="text/javascript">
          var _gaq = _gaq || [];
          _gaq.push(['_setAccount', 'UA-NNNNNN-1']);
          _gaq.push(['_trackPageview']);
          (function() {
            var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
            ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
          })();
        </script>

我的HTML中有以下内容:

      <div class="btn_fb"></div>
      <div class="btn_tw"></div>
      <div class="btn_li"></div>
      <a class="btn_ma"></a>
      <div class="btn_go"></div>

链接功能正常但由于它们不是标签(.btn_ma除外)我无法根据文档添加Google Analytics事件跟踪:https://support.google.com/analytics/answer/1136920?hl=en-GB

有关最佳方法的建议吗?

1 个答案:

答案 0 :(得分:2)

您可以将事件跟踪添加到jQuery点击事件中。

// required to activate social bookmarks
$(".btn_fb").click(function() {
    window.open("");
    try { 
    _gaq.push(['_trackEvent', "{category}", "{action}"]); 
    } catch(err){}
    return false;
});  

$(".btn_tw").click(function() {
    window.open("http://twitter.com/home?status=Webpage Title"+document.URL+"", "Twitter", "width=660,height=400,scrollbars=no;resizable=no");
    try { 
    _gaq.push(['_trackEvent', "{category}", "{action}"]); 
    } catch(err){}
    return false;
});

$(".btn_li").click(function() {
    window.open("http://www.linkedin.com/shareArticle?mini=true&amp;url="+document.URL+"&amp;title=Webpage Title;summary=Webpage summary", "LinkedIn", "width=660,height=400,scrollbars=no;resizable=no");
    try { 
    _gaq.push(['_trackEvent', "{category}", "{action}"]); 
    } catch(err){}
    return false;
});         

$('.btn_go').click(function() {
    window.open("http://plus.google.com/share?url="+document.URL, 'GooglePlus', 'width=660,height=500,scrollbars=no,resizable=no');
    try { 
    _gaq.push(['_trackEvent', "{category}", "{action}"]); 
    } catch(err){}
    return false;
}); 

$(".btn_ma").click(function() {
    window.location = "mailto:?subject=Webpage Subject&body=Webpage summary" + window.location
    try { 
    _gaq.push(['_trackEvent', "{category}", "{action}"]); 
    } catch(err){}
    return false;
});

您会注意到我已将点击事件跟踪添加到每个点击事件,因此当用户点击div时,将采取操作,然后跟踪分析。我还编辑了您的邮件点击事件,以便跟踪它。另一个想法是使用内置分析的Share ThisAddThis等服务,也可以链接到Google Analytics