【问题标题】:Add Google Analytics Click (event) tracking code to Javascript window.open将 Google Analytics 点击(事件)跟踪代码添加到 Javascript window.open
【发布时间】:2013-05-08 03:58:12
【问题描述】:

我脑子里有以下 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

关于这里的最佳方法有什么建议吗?

【问题讨论】:

    标签: javascript google-analytics


    【解决方案1】:

    您可以将事件跟踪添加到 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(分析)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      相关资源
      最近更新 更多