【问题标题】:Issue with Canonical URL in Google AnalyticsGoogle Analytics 中的规范 URL 问题
【发布时间】:2014-01-23 22:47:54
【问题描述】:

我只是对谷歌分析有疑问。我有一个使用 php 开发的网站。现在我想让我的网站变成非规范格式。我遵循以下编码。但谷歌分析仍然无法识别规范网址。

编码

<html>
<head>
<link rel="canonical" href="<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; echo $url;?>">
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'Tracking ID']);
  _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>
</head>
<body>
<?php
if(isset($_REQUEST['id']))
{
echo $url;
}
?>
</body>
<html>

当我尝试打印 $url 时,它会以规范格式打印 url。但它不被谷歌分析识别。谁能告诉我这个编码有什么问题?

【问题讨论】:

  • 你能显示&lt;link rel="canonical" href="&lt;?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; echo $url;?&gt;"&gt; 生成的HTML 吗?也许用 example.com 替换 TLD
  • 我将 id 传递给这个文件。如果我将 url 指定为 example.com/canonical.php?id=10,它将提供规范形式为 example.com/canonical.php..
  • 抱歉,直到现在我才完全意识到你在做什么,但看起来 Elke Pierstorff 知道发生了什么
  • 我想将我的 URL(example.com/file.php?id=10)转换为规范格式 example.com/file.php.. 就是这样。如果你知道该怎么做,请告诉我兄弟..
  • 我明白了,请看我的回答

标签: php google-analytics seo


【解决方案1】:

您实际上并没有将您的规范 url 传递给 Google Analytics - 为此您需要一个“虚拟 url”,通过将您想要的值作为第二个参数传递给 pageview 调用:

[...]
  _gaq.push(['_trackPageview', <?php echo $url ?> ]);
[...]

【讨论】:

  • 认为这可以解决我的问题。谷歌分析仍然只识别非规范格式..
  • 是的,Ur 编码现在可以工作了.. _gaq.push(['_trackPageview', '' ]);
【解决方案2】:

根据https://support.google.com/webmasters/answer/139394?hl=en

Google 目前支持网页搜索的这些链接标题元素 仅限。

所以听起来你的分析仍然会告诉你完整的链接,而谷歌搜索你的网站会显示你想要的。

如果这是最近的更改,那么我建议您在 1-2 周内查看 Google 搜索结果。

如果您正在运行 Apache,那么 mod_rewrite 可能就是您正在寻找的:

http://www.sitepoint.com/apache-mod_rewrite-examples/

特别是mod-rewrite Regular Expressions 部分

【讨论】:

    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2019-08-05
    • 1970-01-01
    相关资源
    最近更新 更多