How to add Google Analytics code to Drupal 7

kagat-kagat picture kagat-kagat · Jan 21, 2013 · Viewed 8.8k times · Source

I want to add my Google Analytics code to my Drupal site without using the module. I read threads related to this but I can't make it on my website. I want to put my code inside the <head></head> tag. Here's my code:

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

Answer

Muhammad Reda picture Muhammad Reda · Jan 21, 2013

Open the folder modules/system in your Drupal install, then copy html.tpl.php file to your theme's directory. Add the code you like to the file and save.

Don't forget to clear the cache.