需要多站点Google Analytic的帮助

时间:2016-03-29 03:00:49

标签: wordpress google-analytics

我需要在IF条件下编写代码,但我知道如何操作。

我有一个多站点WordPress网站(使用域名映射),我试图添加谷歌分析个人单站点跟踪器更改,如果网址更改。

e.g。如果域名为“mydomain.com”,我编写代码来注入GA代码

<?php if (bloginfo('mydomain.com')) { ?>

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-7567xxxx-x', 'auto');
ga('send', 'pageview');

<?php } ?>

但是结果不是我所期望的,如果我需要在域名更改时需要更改结果,任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

bloginfo将使用您博客的标题。

要获取网址,您可以尝试以下代码:

<?php 

  // Set your domain
  $siteURL = "http://www.example.com/";

  // Match with the get_site_url function
  if($siteurl == get_site_url()) { ?>

  <!-- your JS/HTML goes code here -->

  <?php  } ?>

通过这种方式,您可以使用php varible设置网址或博客ID,并使用if / else语句执行GA代码。你也可以使用php数组进行多种设置。

答案 1 :(得分:0)

好的,我发现您的代码中没有任何错误: $ siteURL

    <?php 

  // Set your domain
  $siteURL = "http://www.example.com/";

  // Match with the get_site_url function
  if($siteURL == get_site_url()) { ?>

  <!-- your JS/HTML goes code here -->

  <?php  } ?>

非常感谢Flux寻求帮助

相关问题