首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >YepNope (现代)和谷歌分析

YepNope (现代)和谷歌分析
EN

Stack Overflow用户
提问于 2011-06-27 16:58:39
回答 2查看 2.3K关注 0票数 4

我正在使用伟大的异步脚本加载器yepnope.js (在Modernizr2中)。

我的问题是,将latset Google Analtics异步代码整合到yepnope (如果有的话)的最好方法是什么?

谷歌建议对实际的分析代码这样做:

代码语言:javascript
复制
<html>

<head>
  <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-X']);
    _gaq.push(['_trackPageview']);
  </script>
</head>

<body>
  <p>Page Content</p>

  <script src="some_random_script.js"></script>

  <p>Page Content</p>

  <script type="text/javascript">  (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>
</body>
</html>

但在现代的文档中,他们提到了:

代码语言:javascript
复制
// Give Modernizr.load a string, an object, or an array of strings and objects
Modernizr.load([
  // Presentational polyfills
  {
    // Logical list of things we would normally need
    test : Modernizr.fontface && Modernizr.canvas && Modernizr.cssgradients,
    // Modernizr.load loads css and javascript by default
    nope : ['presentational-polyfill.js', 'presentational.css']
  },
  // Functional polyfills
  {
    // This just has to be truthy
    test : Modernizr.websockets && window.JSON,
    // socket-io.js and json2.js
    nope : 'functional-polyfills.js',
    // You can also give arrays of resources to load.
    both : [ 'app.js', 'extra.js' ],
    complete : function () {
      // Run this after everything in this group has downloaded
      // and executed, as well everything in all previous groups
      myApp.init();
    }
  },
  // Run your analytics after you've already kicked off all the rest
  // of your app.
  'post-analytics.js'
]);

注意底线re:发布分析。我不想要一个新的js文件,因为那是另一个HTTP请求。

我是不是应该把这个放在yepnope外面?把它放在yepnope框架中有什么好处吗?

阿迪

EN

回答 2

Stack Overflow用户

发布于 2011-08-23 03:26:11

我在Ignited-HTML5-Boilerplate上找到了这个。

代码语言:javascript
复制
<script>
    window._gaq = [['_setAccount','UAXXXXXXXX1'],['_trackPageview'],['_trackPageLoadTime']];
    Modernizr.load({
        load: ('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js'
    });
</script>
票数 7
EN

Stack Overflow用户

发布于 2011-12-07 06:12:01

我不确定这是否是将最新的Google Analytics代码集成到yepnope中的“最佳方式”,但是将google代码集成到yepnope中的一种方法是:

代码语言:javascript
复制
<script type="text/javascript">
Modernizr.load([
  {
    // WEB ANALYTICS loaded by yepnope (beta)
    test: Boolean(SITEID = ''), // TODO: Fill the site ID to activate analytics
    complete: function() {
        if (SITEID) {
            var _gaq=[['_setAccount',SITEID],['_trackPageview']]; 
            (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;
            g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
            s.parentNode.insertBefore(g,s)}(document,'script'));
        }
    }
  }
]);
</script>

将此代码放在</body>标记之前的It should be OK

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6490578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档