选择性地将Google Analytics添加到页脚

时间:2011-07-01 作者:Tony Hue

    // add google analytics to footer
function add_google_analytics() { ?>
    if ( !is_user_logged_in() ) {

    <!--Google Analytics-->
    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push([\'_setAccount\', \'UA-XXXXXXXX-X\']);
      _gaq.push([\'_trackPageview\']);
      _gaq.push([\'_trackPageLoadTime\']);

      (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>
}
<?php }

add_action(\'wp_footer\', \'add_google_analytics\');
尝试创建一个功能,根据用户是否登录WordPress添加google analytics跟踪代码。现在,跟踪代码一直在添加。

1 个回复
SO网友:petermolnar

您有?>和php在错误的地方,这样用户登录测试就是一个简单的文本。尝试以下操作:

// add google analytics to footer
function add_google_analytics() {
    if ( !is_user_logged_in() ) 
    {
    ?>
        <!--Google Analytics-->
        <script type="text/javascript">
            var _gaq = _gaq || [];
            _gaq.push([\'_setAccount\', \'UA-XXXXXXXX-X\']);
            _gaq.push([\'_trackPageview\']);
            _gaq.push([\'_trackPageLoadTime\']);
            (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>
    <?php
    }
}

add_action(\'wp_footer\', \'add_google_analytics\');

结束

相关推荐

获取在Functions.php中设置的变量,并在我的Custom Post模板中回显它们

在我的函数中设置了以下函数。php文件,以允许我的自定义帖子类型“Slideshow”工作。add_action( \'the_post\', \'paginate_slide\' ); function paginate_slide( $post ) { global $pages, $multipage, $numpages; if( is_single() && get_post_type() == \'lom_s