7

我想在不使用该模块的情况下将我的 Google Analytics 代码添加到我的 Drupal 站点。我阅读了与此相关的主题,但无法在我的网站上发布。我想把我的代码放在<head></head>标签里。这是我的代码:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
  _gaq.push(['_setDomainName', 'example.com']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
4

2 回答 2

11

打开modules/systemDrupal 安装中的文件夹,然后将html.tpl.php文件复制到主题目录。将您喜欢的代码添加到文件中并保存。

不要忘记清除缓存。

于 2013-01-21T08:59:50.663 回答
7

在 Drupal 站点上,您想在 template.php 文件的 THEME_preprocess_html 函数中使用 drupal_add_js 函数插​​入额外的 javascript。这样做可以让您正确缓存您的网站。具体来说,这就是它的外观:

<?php
    function THEME_preprocess_html(&$variables) {

      $ga  = "var _gaq = _gaq || [];\n";
      $ga .= "_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);\n";
      $ga .= "_gaq.push(['_setDomainName', 'example.com']);\n";
      $ga .= "_gaq.push(['_trackPageview']);\n";
      $ga .= "(function() {\n";
      $ga .= "  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n";
      $ga .= "  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n";
      $ga .= "  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n";
      $ga .= "})();\n";

      drupal_add_js($ga, array('type' => 'inline', 'scope' => 'header'));
    }
?>

Be sure to replace the UA ID and website name with your own. Also, be sure to rename THEME to your theme and clear the cache when complete.

于 2014-06-19T22:13:58.963 回答