php - 多作者 Google Analytics(将 PHP 变量从 DB 组合到 JS)

标签 php javascript wordpress google-analytics analytics

我试图让我网站上的作者通过在 Analytics 中设置一个 channel 并通过他们的个人资料提交来检索他们自己的 Google 分析数据。我已经尝试了很多方法来做到这一点,但这是迄今为止我所拥有的。

window.google_analytics_uacct = "UA-xxxxxxx-xx";
<script type="text/javascript">//<![CDATA[

var _gaq = _gaq || [];
_gaq.push(['_setAccount','UA-xxxxxxx-xx']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setAccount', 'UA-xxxxxxx-xx']);
_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>

我试图将以下 PHP 合并到推送到的第二个帐户数据中。

    <?php if ( get_the_author_meta( 'analytics' ) ) { ?>
        <?php the_author_meta( 'analytics' ); ?>
    <?php } ?>

我在第二组周围使用了 if 语句

_gaq.push(['_setAccount', 'UA-xxxxxxx-xx']);
_gaq.push(['_trackPageview']);

仅当网站作者提交了其 Analytics ID 时才显示此集。

不幸的是,仅仅从数据库获取值本身就给我带来了很多问题!任何帮助将不胜感激:)

亲切的问候

奥利

最佳答案

-试试这个。作者元“分析”必须类似于 UA-1234567-12

<script type="text/javascript">//<![CDATA[

var _gaq = _gaq || [];
_gaq.push(['_setAccount','UA-xxxxxxx-xx']);

<?php if($ganal = get_the_author_meta('analytics')): ?>
_gaq.push(['_setAccount', '<?php echo $ganal; ?>']);
<?php endif; ?>

_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>

关于php - 多作者 Google Analytics(将 PHP 变量从 DB 组合到 JS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6013311/

相关文章:

php - MySQL 从搜索结果中获取列名

php - Codeigniter 密码验证问题

javascript - 如何将具有相同键的对象数组分组为单个数组中的对象?

html - 透明背景使我的文字也透明

php - 带有 '&' 的 PHP 中的 MySQL 查询返回与 phpMyAdmin 不同的结果

javascript - 检测 DIV 的 HTML 和 CSS 并进行更改以使其与同级 DIV 一起滚动

javascript - 从登录页面重定向到下一页

wordpress - 为什么 $wp_query->post 在 404 期间未设置?

javascript - 在 Wordpress 中运行 Java 脚本

php - 如何使用 Mink 在 behat.yml 中正确设置 desiredCapabilities