7

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> 
3
  • 2
    any particular reason why do you want to do that without using the module? Commented Jan 21, 2013 at 9:46
  • I am curious too. I can't see a reason not to use the module yet there are reason good reasons to use a module (e.g. A module can update the embedded script if there is any change in Google analytics script) Commented May 15, 2013 at 11:51
  • 1
    If you want to be verified as an owner of a site in some other Google services (eg. Search Console), you need to have Google Analytics code inside head tag. Commented Apr 27, 2016 at 13:37

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The file will be copied just inside MYTHEME/ directory? Or inside MYTHEME/Templates? Cause I found my page.tpl.php right there.
Anywhere is fine. It's up to you :)
I can see now the script from the source. How would I know that my javascript works? Thasnk!
Open your website from different browsers, then open your Google Analytics account and see the data there. It should record your visits.
7

On a Drupal site, you want to insert additional javascript using the drupal_add_js function inside the THEME_preprocess_html function in the template.php file. Doing so allows you to properly cache your site. Specifically, this is how it would look:

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

2 Comments

+1. But why getting the code from html.tpl.php is not so good?
google analytics give a javascript code, maybe this code (javascript) will not working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.